Java JSplitPane update Components directly after resizing - java

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.

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.

Java gridbaglayout using scrollpane with draggable jpanel

Im using a gridbaglayout on a jpanel with a scrollpane, all that works fine. Later in my code i add another jpanel using the constraints(x,y) to the same panel on top of everything else thats already there using the index, this also works fine. This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?
This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?
Because you are using a layout manager. When the frame is resized the layout manager is invoked and components are assigned a size/location based on the rules of the layout manager.
Check out the Drag Layout. It will allow you to drag components around a panel without resetting the location of the components.

How to make a JFrame scrollable?

I would like to scroll through the contents of my JFrame up and down, preferably with a scroll bar. I don't want to wrap the contents inside a JPanel or JScrollPane, because this causes some visual glitches with my application.
Any idea on how to do this?
JScrollPane would be the easiest way; you say there are glitches, but that probably indicates a problem in your code that will still be a problem even without using a JScrollPane.
If you're absolutely set on not using a JScrollPane, you should create a JPanel using BorderLayout, add a JPanel (call it 'center') with BorderLayout.CENTER and layout set to null. Add your content within 'center', and add another JScrollBar to BorderLayout.EAST, add an AdjustmentListener to the JScrollBar. When the adjustmentListener triggers, you need to move your content (Component.setLocation(...)) that's in center to the relative y offset of the JScrollBar and call repaint on 'center'

How to force-refresh/repaint a JScrollPane?

I am adding lots of components (JPanels, JLabels etc.) into a JScrollPane programagically at the start of my program based on some stuff from a database.
It seems that this procedure is too fast for the GUI(?), so the JScrollPane does not always update correctly, i.e the scroll bars are not visible even though the inner JPanel is bigger than the visible area.
Resizing the Window (JFrame) fixes the problem, as I assume Java is re-printing the components when they are resized.
As a test, I have added a debug-button that I can click after the startup of the program has finished. I am trying to force the JScrollPane to "refresh" itself.
I have tried doing:
scrollpane.repaint();
scrollpane.validate();
scrollpane.revalidate();
None of them seems to work. However, if I change the border (or any other layout related to the JScrollPane), it refreshes correctly.
scrollpane.setBorder(new LineBorder(Color.RED));
So I basically have 2 questions.
What is the command for forcing the scrollpane to "refresh"? Obviously it is doing some kind of "repaint" thing when I am adding the border. How can I run that only?
Is there a way of "pausing" the printing of components as they are added and resume it again after I added all the wanted components? As it is now, I basically "see" the components being added on the screen (even though it is really fast). It would be better if I can add all the components I want and THEN tell the program to print it to the screen/JFrame.
The basic code for adding components to a visible panel is:
panel.add(...);
panel.add(...);
panel.revalidate();
panel.repaint();
Adding a component does nothing because the component still has a zero size so there is nothing to paint. When you invoke the revalidate() method the layout manager gets invoked so components will now have a location/size. The repaint() will then paint the components. The revalidate() will also cause the scrollbars to show when required. This of course assumes you are using layout managers.
The components are added to the panel so you invoke the methods on the panel, not the scrollpane.
In my case only
frame.pack();
helped to get the scrollbars on the JScrollPane, when the enclosed JPanel was resized dynamically.

How to automatically resize an AWT component after changing it's contents?

I have an AWT Label inside a Panel with FlowLayout. I want to dynamically change the Labels text and resize it to the required width.
I have only found answers to the Swing version of this problem (setPrototypeDisplayValue()), but I have to stick with AWT since this is a homework.
You should be able to call invalidate(), which will then tell the parent container (your Panel) to redraw itself.
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Container.html#invalidate()

Categories