How do i set the scrollbar in JScrollPane to the top? Using borderlayout. the bar always scroll to the bottom of my content. - eclipse

//created a JScrollPane and when i run it the scrollbars will automatically go to the most bottom of my textarea, but i need it to be on top.
thank you in advance! =)
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 86, 262, 57);
content.add(scrollPane);
//the text area used
JTextArea txtrTryingTryingTrying = new JTextArea();
scrollPane.setViewportView(txtrTryingTryingTrying);
txtrTryingTryingTrying.setText("-------");

I've been trying as well, and found a solution.
There is a useful method called: setCaretPosition(int)
After you use
component.setText("Text here");
You must use
component.setCaretPosition(0); //0 will set it to the top
In your case, you must add setCaretPosition() after setText
JTextArea txtrTryingTryingTrying = new JTextArea();
scrollPane.setViewportView(txtrTryingTryingTrying);
txtrTryingTryingTrying.setText("-------");
txtrTryingTryingTrying.setCaretPosition(0);

Related

Why scrollbar of JtextArea doesn't navigate?

I use one JTextArea and JScrollBar. It has this problem when I run it and it will fill by text, I can't navigate it. i.e. when I click on arrowkey on scrolbar, nothing happen. although I write the update code for it (by using API), but it doesn't work well. What shall i do? Thanks in advance.
I show my code as bellow:
JTextArea Result_field = new JTextArea(5,10);
Result_field.setPreferredSize(new Dimension(5, 10));
JScrollPane scrollPane = new JScrollPane( Result_field );
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
DefaultCaret caret = (DefaultCaret)Result_field.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
PrintStream printStream = new PrintStream(new CustomOutputStreamm(Result_field));
System.setOut(printStream);
MainPanel.add(scrollPane,gbc);
I found out my problem, although we define one JTextArea or ... by the size, But after that we should determine the prefer size again.

How to set a Popup to be always visible on the top in GWT

I have a loading popup that I need to display on the top of the page, even if the user scroll down.
What I tried so far is to set the popup position as follows
setPopupPosition(Window.getClientWidth()/2 , 0);
The popup shows up on the absolut top.
The situation can be resolved easily if you view it from a different angle: Not the popup position should adjust to the page - instead, the page should scroll behind the centering popup, e.g.:
final ScrollPanel scrollPanel = new ScrollPanel();
RootLayoutPanel.get().add(scrollPanel);
pagePanel = new FlowPanel();
scrollPanel.setWidget(pagePanel);
pagePanel.add(...);
Now add the entire page contents to pagePanel (instead of adding them directly to rootPanel).
Then you can create popups like this:
final PopupPanel popupPanel = new PopupPanel();
popupPanel.add(...);
popupPanel.center();
You'll still have to re-center the popup when the window resizes, but apart from that, the popup will always be at the center in front of the scrolling page.
To achieve this you can implement Window.addWindowScrollHandler. It will always be on top whatever you do.
DialogBox dialog = new DialogBox();
dialog.setWidget(...);
Window.addWindowScrollHandler(new ScrollHandler() {
#Override
public void onWindowScroll(ScrollEvent event) {
dialog.setPopupPosition((Window.getClientWidth() - widthOfDialog) / 2, event.getScrollTop());
}
});
Hope this helps.. Thanks..
The solution that worked for me is this
setPopupPosition(Window.getClientWidth()/2 , Window.getScrollTop());

GWT - Can you nest FormPanels inside FormPanels?

I'm trying to nest a FormPanel inside another FormPanel. It seems that any field in the nested panel is never rendered.
This screenshot is produced by the code below it:
TabItem tabItem = new TabItem("Tab Item");
FormPanel formPanel = new FormPanel();
formPanel.setHeading("Form Panel");
formPanel.setFrame(true);
TextField textField = new TextField();
textField.setFieldLabel("Text Field");
FormPanel nestedPanel = new FormPanel();
nestedPanel.setHeading("Nested Panel");
TextField nestedField = new TextField();
nestedField.setFieldLabel("Nested Field");
nestedPanel.add(nestedField);
TextField anotherField = new TextField();
anotherField.setFieldLabel("Another Field");
formPanel.add(textField);
formPanel.add(nestedPanel);
formPanel.add(anotherField);
tabItem.add(formPanel);
tabPanel.add(tabItem);
Can anyone explain why the nested field does not show in the nested panel?
I've also tried using a CaptionPanel instead of a FormPanel as the nested panel, but the caption panel does not show the field label.
Any suggestions as to how I can get this to work would be most welcome. Thank you :)
As Jason mentioned, <form> cannot be nested. The GXT FormPanel draws a form as part of how it works, so consider drawing this layout in another way.
To emulate the appearance of the FormPanel, there are two basic steps.
To get the header, border, create a ContentPanel, and add the content to that
To get the GXT 2 layout of drawing the field labels, use a FormLayout in the content panel.
This will look something like this (from your example)
//...
ContentPanel nestedPanel = new ContentPanel(new FormLayout();
nestedPanel.setHeading("Nested Panel");
TextField nestedField = new TextField();
nestedField.setFieldLabel("Nested Field");
nestedPanel.add(nestedField);
//...
The outer field will still manage any binding, and the nested field will look as if they were in a FormPanel. If not using other features of the FormPanel, it may in general make more sense to use a ContentPanel (or LayoutContainer, if you don't want the border/header) with a FormLayout.

GWT: How do I create a TabsLayoutPanel with horizontal scroll bars?

I'm using GWT 2.4. I'm trying to create a tabs panel that has multiple tabs, so many that sometimes they exceed the allotted horizontal boundaries. How can I make a horizontal scroll bar appear? Here is how I'm creating my tabs panel now ...
tabsPanel = new TabLayoutPanel(BAR_HEIGHT_IN_EM, Style.Unit.EM);
final List<ScrollPanel> tabs = new ArrayList<ScrollPanel>();
for (final Node tabNode : documentNode.getChildren()) {
// Get the tab's child widgets and add them
// into the tab panel.
final ScrollPanel childpanel = new ScrollPanel();
childpanel.setHeight(SCROLL_PANEL_HEIGHT_PCT);
...
tabsPanel.add(childpanel, tabName);
} // for
tabsPanel.setWidth("100%");
tabsPanel.setHeight("100%");
As you can see, I tried width="100%", but no scroll panels appear that allow me to see the excess tabs.
Thanks, - Dave
There is no built in way to do this with the TabLayoutPanel, but looking at it's code it is possible to do this:
SimplePanel tab = (SimplePanel) getTabWidget(0).getParent();
FlowPanel tabBar = (FlowPanel) tab.getParent();
LayoutPanel container = (LayoutPanel) tabBar.getParent();
Element tabBarContainerLayer = container.getWidgetContainerElement(tabBar);
tabBarContainerLayer.getStyle().clearOverflowX();
DISCLAIMER: untested

Hiding cursor in Text component in Eclipse RCP application

In my eclipse RCP application there are a few buttons & few input boxes & this below Text component. My problem is as soon as I press one of the buttons a cursor starts blinking in the below test component. Can you please let me know how to solve this.
I tried:
setting focus to false for Text.
SWT.READ_ONLY for Text .
Code:
Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_NO);
protocolFilterDescription.setCursor(cursor);
Nothing seems to get rid of this unnecessary cursor.
protocolFilterDescription = new Text(parent, SWT.NONE | SWT.READ_ONLY );
FormData protocolFilterDescriptionLData = new FormData();
protocolFilterDescriptionLData.left = new FormAttachment(0, 1000, 650);
protocolFilterDescriptionLData.top = new FormAttachment(0, 1000, 290);
protocolFilterDescriptionLData.width = 450;
protocolFilterDescriptionLData.height = 12;
protocolFilterDescription.setLayoutData(protocolFilterDescriptionLData);
protocolFilterDescription.setForeground(new Color(parent.getDisplay(),
204, 153, 0));
protocolFilterDescription.setBackground(Display.getCurrent()
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
protocolFilterDescription.setFont(new Font(parent.getDisplay(),"Verdana",
6, 1));
protocolFilterDescription
.setText("captured");
You have to set the focus of some other SWT component to true to remove the focus from the Text component.
You'll probably have to do this in an ActionListener.
If you want to completely remove the cursor from the Text control (which implies inability to perform a selection there, etc), try calling setEnabled(false) on it.
Also, such requirement suggests that you maybe don't need Text component at all, and could use Label instead.