JScrollPane in a popup window does not scroll up programmatically - java

I have a popup menu that displays dynamically created custom JPanel objects in a JPanel in a JScrollPane. The popup menu displays recommendations to the user and the topmost element is the most relevant recommendation. I am using JPopupMenu to display the window:
JPanelTemplatePopup jptep = new JPanelTemplatePopup();
JPopupMenu popup = new JPopupMenu();
popup.add(jptep);
popup.show(this, 500, 100);
The problem is, I can't make the JScrollPane scroll to the topmost element to display it first. I have tried:
.getViewPort().setViewPosition(new
Point(0,0));
.scrollRectToVisible(firstelement.getBounds());
before and after validate()s. No matter what I do, when the window pops up, the scroll pane always stays at the same place.
I have even suspected that the operations that took place before displaying the window were ignored, so I created and called a public method from the class to make the window scroll it up after being displayed. Nothing changed.
Please help,
Emre

Yeah, well, found a workaround for this.
I have narrowed the problem down to a JEditorPane that was in the custom JPanel objects. Its contents were dynamically updated by the program. While generating the objects, I was setting JEditorPanes' contents by their setText methods. Updating the caret position by setting the text to a string forced the scroll pane to scroll below.
I just inserted this to the constructor and the problem was fixed:
DefaultCaret caret = (DefaultCaret) jEditorPaneContents.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

Related

How to make a JScrollPane scrollable after a component has been added at runtime

Unfortunately, I have seen this question multiple times but unfortunately I do not know what I am doing well enough to be able to interpret what I have to implement in my own code.
I have used the GUI editor in netbeans to create a JScrollPane and a JPanel on top of this. I am aware I can create these components at runtime along with everything else but this proved problematic and simply adding the scroll pane and panel in the editor and adding components to them during runtime has worked for me thus far.
So far, creating components and adding them to the panel is no problem. The problem I face is that the scroll pane will not update itself to enable the user to scroll further down to view the created components at the bottom. I have been generating ‘entries’ each time a button is pressed, that currently creates a JTextField and adds this component to the panel, more components are going to be added later but for now this is just experimentation.
Once the ‘list’ of ‘entries’ exceeds the limit of the window, the window has to be resized in order to view components at the bottom but after a certain number of entries (around 25-30) the components are no longer viewable as the scroll bar does not scroll down the panel.
This is how I am adding components at runtime...
JTextField txtName1 = new JTextField();
txtName1.setLocation(10, 90);
txtName1.setSize(135, 25);
pnlContainer.add(txtName1);
The 'pnlContainer' is attached on top of the 'jspContainer' which is my JScrollPane and I am using the 'repaint()' method to get the components visible on the panel.
So far, adding components at runtime has worked, all of the components are visible and interact-able.
I am fairly new to programming in general with only a few years experience, any documentation that may help, tutorials or anything else is greatly appreciated. Documentation is always welcome as I still have a lot to learn.
JTextField txtName1 = new JTextField();
txtName1.setLocation(10, 90);
txtName1.setSize(135, 25);
pnlContainer.add(txtName1);
So far, adding components at runtime has worked, all of the components are visible and interact-able.
In you above code example you are setting the size/location of each component which implies you are using a null layout. Don't use a null layout!!!
Swing was designed to be used with layout managers and scrolling will work properly when you use panels with a layout manager.
The layout manager is responsible for determining the "preferred size" of the panel. The panel will then display scrollbars automatically when the preferred size of the panel is greater than the size of the scroll panel.
When you use a null layout the preferred size of the panel is 0, so the scrollbars will never appear.

JTextArea forces the parent JScrollPane to scroll down

I have a panel inside a JScrollPane and I dynamically populate the panel with components as the data gets received from the service. The panel uses GridBagLayout (but that should be irrelevant). For each record that comes back from the service, I create several components dynamically and append them to the bottom of the panel. Everything works fine, but the problem is that JTextArea that gets created for each record forces the main JScrollPane to scroll down and show the last added JTextArea, as shown here:
I tried to disable everything I could think of to dumb down the JTextArea, but still doesn't help
JTextArea descriptionArea = new JTextArea(project.getDescription().replace("<br>", "\n"));
descriptionArea.setEditable(false);
descriptionArea.setFont(thumbnailLabel.getFont());
descriptionArea.setLineWrap(true);
descriptionArea.setWrapStyleWord(true);
descriptionArea.setFocusable(false);
descriptionArea.setRequestFocusEnabled(false);
DefaultCaret caret = (DefaultCaret) descriptionArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
How can I prevent it from moving the scrollbar? I tried replacing JTextArea with JLabel and that works, but I can't get the JLabel to word wrap the text as good. Any ideas would be highly appreciated.
You could do something like:
Point p = srcollPane.getViewport().getViewPostition();
// add the components to the panel in the viewport of the scrollpane
scrollpane.getViewport().setViewPosition( p );
Now the scrollpane should be reset to its original position before you added the components.

GWT: Draggable Popup with Scrollpanel

Designed a popup which is draggable, works as designed. Now Popup panel contains the ScrollPanel it is having vertical scroll bars, but while trying to scroll with dragging is not working
The following link gives u a window box implementation which is draggable. You can add scroll panel to it. I have tried it. It works. Have a look at it.
http://svn.clazzes.org/svn/gwt/branches/gwt-extras-0.1-branch/src/main/java/org/clazzes/gwt/extras/dialog/WindowBox.java

Overlapped JLabel in Frame

I am writing a code in Java Swing in which a label is added in frame on the click of the button. And the Labels are added such that it overlap each other to create a stack like view.
I am creating my own layout using label.setbouds(x,y,100,100) function.
And each time button is clicked in its action listener a new label is added.
For example
if(e.getSource()==button){
label.setBounds(x+5,y+5,100,100);
frame.add(label);
frame.repaint();
}
Now the problem is when these labels are painted the first label added always remain on the top and newly add Labels are overshadowed by odd label.
I need a help if someone could explain how i can paint the frame such that newly added frames comes on the top and old labels get overlapped.Any suggestion is appreciated
Regards
ACoder
Insert your labels in a JLayeredPane with the proper index. Also make sure to set your JLabel as opaque (setOpaque(true)) so that it will actually obscure the content of the labels below. However, if a label below is bigger than the label on top, you will still see some part of it

Java Swing Maximize button of JFrame disappearss

I have created a java frame. It contains a JSplitPane. In the right partition of the splitpane , I have put JTextPane. Now I have kept JButton somewhere on the frame. When I click the button, a new JTextArea is inserted in the JTextPane as follows
JTextArea newTextBox = new JTextArea(1, 1);
StyleConstants.setComponent(style, newTextBox);
My problem is as soon as I insert JTextArea in JTextPane, the maximize button of my JFrame disappears. Can anyone tell me what could be the reason of happning this.
Maxmize button is unavailable on two conditions:
For a Dialog, in which case only close(X) button is availabe.
If frame.setResizable(false) has been called(i.e diabled resize of window)
So applying this to your case, my best guess is that somewhere in your button's action you may have made it unresizable.

Categories