JTree vertical scrollbars not working - java

I have a JTree embedded in a JScrollPane. When the JTree grows bigger than the display can show, the remainder of the tree is located at the bottom outside of the visible range. I expected the JScrollPane to start displaying vertical scrollbars if this happens so that you can scroll the remainder into view. The vertical scrollbars do not appear at all if the JTree is expanded and doesn't fit. Here is how it looks like:
I have both, the horizontal and vertical scrollbarPolicy set to as needed. I also tried embedding the JScrollPane containing the JTree into a JPanel but it didn't help. I'm using the UI designer in IntelliJ IDEA to build the GUI by the way.
Any help on making the vertical scrollbars behave as expected? Forcing them to be shown at all times also doesn't work: They cannot be dragged down regardless.

not use preferredSize.
in UI designer choos JTree element, find property preferredSize and in context menu select Restore default value

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.

form components disappear in NetBeans

Very strange occurrence, am in need of a quick way to make my panel components (labels and textboxes) visible again on the form in NetBeans. As soon as I added the Table to the right of the panel, the panel seems to have disappeared. Strangely enough, the components continue to be available in the left side, in the Navigator box, so they are not completely gone, just seem to be hidden. I was unable to find any Visible property, that I could to set to true. Any help is much appreciated. Also, what exactly triggered this behaviour, is this a bug? Many thanks in advance.
Its a focus issue, it makes things easier when you have lots of components.
At the moment you have the scrollPane selected/focused, so you will only be able to see that and any child components.
If you want to see sister or parent components you need to set your focus to your jFrame (or whatever component you want to see).
You can do this from the box on the bottom left, just double click on jFrame, its the top item and also parent to both the scrollPane you have selected now and also parent of your jPanel that contains all the other buttons and labels.
This will have been caused when you double clicked the scrollPane.

Java JSplitPane update Components directly after resizing

Im trying to update myJSplitPane` components directly after I drag the divider. I enabled instant updating of the components when I drag the divider.
My problem is that the components getting resized a bit later than I drag the divider; that looks a littlebit confusing.
Image of my Window at resizing
You can see the JScrollPane outside the window.
I want to avoid that problem.
I tried:
- Property change listener
- Thread with 60fps
Nothing helps.
//SOLVED
Added a BorderLayout to my Panel and set the setPreferredSize as I need it.

JPanel adjust to JScrollpane and children

I'm trying to make a ToDoManager in java. For now I have about what I want it to be for a basic version. But I'm having a problem with the size of a panel.
I have a main JFrame. This contains a JPanel, say jPanel1.
jPanel1 has 2 buttons (add and remove) and another JPanel (say jPanel2).
jPanel2 contains a JScrollPane, which contains a modified version of JTable.
The thing I want is to tell the JTable to stretch out, so i can view everything in the JTable, and then tell the JScrollPane and jPanel2 to "Pack", or resize, so the JTable is completely vissable (if not possible the JScrollPane should do its work and draw the scrollbars).
This is what I have got at the moment:
So maybe you can see 2 problems:
1) The horizontal scroll bar does not appear. (But I did set the scroll bar: HORIZONTAL_AS_NEEDED)
2) I did not set any preferred size for the main JFrame, nor for the jPanel1, but it packs always as the same size. So I would like to stretch the jPanel2 to the full JTable, and if that would exceed the screen size, draw the scroll bars.
Using another layout manager, it's a lot easier to comprehend the usage of the JPanels and this concludes the problem.

How to make JTextPane wrapped in JScrollPane shrink to fit the text

I have a JTextPane with HTML text.
I used GroupLayout (using WindowBuilder).
I've set the minimum size of my JFrame to 800x600 so the user cannot make it smaller than that.
The app has a big scrolling JPanel the size of the entire window. The top part of the panel is taken up by a JTextPane wrapped in JScrollPane. I have disabled the scroll bars and sized the JScrollPane to make the entire text visible.
In group layout the JScrollPane is set to stay constant vertically, but size horizontally.
My issue is that when the user makes the window larger the JScrollPane also expands, but now there is a big white space left at the bottom of the text pane. Is there a way that I can make JTextPane shrink to fit its contents.
Also if you suggest a different layout, I would be willing to try it.
I used this TextPanePerfectSize example from #camickr to solve a similar problem. The example uses validate() and pack() to adjust to the preferred size. You might be able to adapt it to your situation.
Take a look at SpringLayout. It gives you far more control over the positioning of components. Look at the SpringLayout tutorial if you get stuck.
The trick in your case is to bind the bottom (south) of your JScrollPane to the top (north) of the screen.

Categories