What methods get called when you resize a JFrame? - java

I'm using a JFrame in which the CENTER portion of the BorderLayout is occupied by a JScrollPane that wraps around a JPanel. What I'm finding is that when I initiate the action that actually causes the JPanel to be displayed, the display doesn't change. But when I resize the JFrame, the new JScrollPane has now magically appeared.
So what methods are called when you resize a JFrame? If I know, then I can call it in the code and avoid having to resize the frame just to see the results of the operation.

Its been a little bit since I've done swing, but from memory, calling validate() on the panel should do the trick. This will cause it and its children to have their layout calculated which is when the scrollbars decision is made. If that doesn't work, try calling validate on the frame's content pane. This is a little more costly, but may be needed if other components are being considered.

Related

How can I create responsive JList in Java Swing

I want to create a shop by having a main JPanel that each component inside it is a JPanel with an image, label and button.
I did tried using a JList but the problem with the JList its only holds the rendering of the component and because of that the button isn't working and its only an image. I can walk around and use MouseEvent but it feels wrong for me and I am sure that there is a better solution for it.
I want that the components will change their positions depend on the frame size, like in the JList.
For example, if I change from the width of the screen the positions of the components will change from this:
to this:
I do have an idea by using GridLayout or GridBagLayout in the paintComponent (because it calls every rendering. If you know another method that calls every rendering int the JPanel I would like to know) and changing the positions of the components by changing the layout variables inside the paintComponent.
I did surfed the internet to find a solution but I only found that people used JTable but I don't see it working here.
each component inside it is a JPanel with an image, label and button.
Makes sense.
changing the positions of the components by changing the layout variables inside the paintComponent.
The paintComponent() method has nothing to do with changing the layout of the panels. You should not be playing with the paintComponent() method.
I do have an idea by using GridLayout or GridBagLayout
You are correct to use a layout manager, but unfortunately, none of the default layout managers will wrap automatically at a random number of components.
The layout managers are invoked automatically as the frame is resized.
So you can use the Wrap Layout which is an extension to the FlowLayout that will allow random wrapping.

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.

Overriding JTextPane to prevent auto-resizing

I'm trying to make a JTextPane inside a GridBagLayout JPanel. The program adds the the JTextPane in the right place, and everything is fine and dandy until I try to add text to the JTextPane. Then everything goes to hell. No matter what I do or try the JTextPane won't stop resizing itself by a width equal to the longest row and a height equal to the number of rows I'm adding.
This is extremely annoying because it causes all the rest of the Layout to go to hell. (The rest of the Layout is a Grid of rectangles on the main JPanel and another JPanel on the right, contained by the main one containing another grid of rectangles). I COULD use preferred size to stop the culprit from auto-resizing, but that would also prevent it from resizing on a JFrame resize (tip: this == bad).
I was wondering if there is any method involved in the auto-resizing of the JTextPane when text is added I could override to prevent this behaviour.
If it is needed I'll post a snippet later.

How does a JLabel or JButton tell a JScrollPane that view size has changed?

How does a JLabel or JButton notify a JScrollPane that the view size has changed (for example when an icon has been set) so it can determine whether showing scrollbars are necessary?
How could I implement similar behaviour to display an image with a simple JPanel without resorting to the aforementionned components?
P.S: I've looked through the source code and so far all I see is that a Component is referred to as "view" and is passed on to a JView or JViewport which registers some listeners. From there on things seem unclear.
As noted in the JScrollPane API, unless you change the policy, "both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view." Once pack() has sized the Window "to fit the preferred size and layouts of its subcomponents," any subsequent changes are seen by the scroll pane when the container is validated and repainted. See Painting in AWT and Swing for more.

JAVA: Ways to fill a Frame. add(), setContentPane(), getContentPane()

I found three ways to fill my JFrame frame = new JFrame("...")
createContentPanel returns a JPanel and createToolBar returns a ToolBar.
frame.add(this.createToolBar(), BorderLayout.PAGE_START); //this works and puts the ToolBar above and the ContentPanel under it<br>
frame.add(this.createContentPanel(), BorderLayout.CENTER);
frame.setContentPane(this.createContentPanel()); //this lets the JToolBar hover over the ContentPanel
frame.getContentPane().add(this.createToolBar());
frame.getContentPane().add(this.createContentPanel()); //this only puts the last one into the JFrame
frame.getContentPane().add(this.createToolBar());
And now I am wondering why should i use the getContentPane()/setContentPane() method if i could just use a simple frame.add(...) to fill my frame.
You are right that it doesn't matter which you use (JFrame#add(...) vs. JFrame#getContentPane().add(...)) since they both essentially call the same code, however there will be times in the future when you'll need access to the contentPane itself, such as if you want to change its border, set its background color or determine its dimensions, and so you'll likely use getContentPane() at some point, and thus getting to know it and be familiar with it would be helpful.
//this only puts the last one into the JFrame
You need to understand how layout managers work. The default content pane is a JPanel that uses a BorderLayout. When you add a component and don't specify a constraint, then it defaults to the CENTER. However you can only has a single component in the center so the layout manager only knows about the last one added. When the layout manager is invoked it sets the size() and location() of that component. The other component has a size of 0, so it is never painted.
In Java 1.6, you can just use the add method of JFrame:
http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html
(It will be delegated to the contentPane.)
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html
Which says:
The JFrame class is slightly
incompatible with Frame. Like all
other JFC/Swing top-level containers,
a JFrame contains a JRootPane as its
only child. The content pane provided
by the root pane should, as a rule,
contain all the non-menu components
displayed by the JFrame. This is
different from the AWT Frame case. For
example, to add a child to an AWT
frame you'd write:
frame.add(child);
However using JFrame you need to add the child
to the JFrame's content pane instead:
frame.getContentPane().add(child);
The same is true for setting layout
managers, removing components, listing
children, and so on. All these methods
should normally be sent to the content
pane instead of the JFrame itself. The
content pane will always be non-null.
Attempting to set it to null will
cause the JFrame to throw an
exception. The default content pane
will have a BorderLayout manager set
on it.

Categories