Set size of JLabel in FlowLayout - java

I have a JPanel that uses FlowLayout. I add a number of JLabels to the JPanel, use setPreferedSize() to adjust their size and save them in a list, label_list. Everything works fine. Then I want to change their size:
for(JLabel c:label_list){
c.setPreferedSize(new Dimension(10,10));
}
And it doesn't work.
c.setBackground(Color.red)
and similar stuff works. Why can't I use setPreferedSize here?
c.setBounds(1,1,10,10) and c.setSize(10,10)
Works, but after i update the UI (resizeing the panel) every size goes back to normal.

Then I want to change their size:
for(JLabel c:label_list){ c.setPreferedSize(new Dimension(10,10)); }
And it doesn't work.
You need to call revalidate() on the parent of the labels so that it reperforms layout and enforces their preferred size.
c.setBounds(1,1,10,10) and c.setSize(10,10) Works, but after i update
the UI (resizeing the panel) every size goes back to normal.
Setting bounds/size/location manually conflicts with the LayoutManager of the parent container. The job of the LayoutManager is to position and size the child components.
Either set the layout to null and call yourself setSize-setLocation/setBounds, or use a LayoutManager (recommended) and never call setSize-setLocation/setBounds. At most, you can call setPreferred/setMaximum/setMinimum size but try to avoid this as it can causes cross L&F problems.

Related

java JScrollPane with JPanel with BoxLayout resizes elements

I have a collection of JPanels for different elements: JPanel showPane, seasonsPane, episodesPane, airingsPane
all of which have setLayout set to null and are of fixed size 304x416. I added those JPanels to a JPanel called showViewPanel of size 1280x416 and set up a BoxLayout like so (below is the code in the JFrame):
showViewPanel.setLayout(new BoxLayout(showViewPanel, BoxLayout.X_AXIS));
showViewPanel.setSize(1280, 416);
showViewScroll = new JScrollPane(showViewPanel);
add(showViewScroll);
setSize(304, 416);
setVisible(true);
What I can't figure out is why it does not produce a scrollable view of all the components and instead resizes showViewPanel to match the size of the window. What am I doing wrong?
all of which have setLayout set to null and are of fixed size 304x416.
Don't use a null layout!!! Don't manually set the size of a panel!!! Your panels should be using a layout manager so the preferred size will be calculated automatically.
If you want all your panels to be the same size then maybe use a GridLayout for your main panel (instead of the BoxLayout) and then add your child panels to this panel.
Scrollbars will automatically appear when needed if you let the layout managers do their job.
Override the preferredSize of the subpanels to 304x416 instead. The scrollpane relies on the preferred size of the content (which depends on the preferred size of its contents).
The layout manager will set the size of the subpanels depending on their preferred sizes, so your custom ones get overridden. Generally, you should get rid of null layouts and learn to use the layout managers. Absolute placement leads to trouble all the time, and is not worth the hassle even when it does not.

How do I programatically resize a component in a BorderLayout?

I have a JPanel that is located at the BorderLayout.SOUTH position of a parent JPanel. I would like to programatically change the height of this child JPanel.
I am calling childPanel.setPreferredSize(new Dimension(getWidth(), newHeight));, but it has no effect. (I have tried setting the Maximum and Minimum sizes as well.)
What (probably very simple thing) am I missing?
After changing the preferred size, you need to call revalidate() on that childPanel.
You may want to read this Oracle Tutorial on LayoutManagers

JPanel ActionMethod when it is shown

I search for a method, which gets called if the jpanel is shown on the display, because i have to fetch the real size of the panel.
Any suggestions?
Have you tried adding a ComponentListener to the JPanel? That would be where I would start with my code in the componentShown(...) method. For this to work, I think that you must call setVisible(true) on the JPanel after adding it to the display.
The other option is to simply query its size after calling pack(), or setVisible(true) on your GUI.
Edit
You state:
I added the panel to the gui designer.. when the window pops up, i wanna now the real size of the jpanel, because it can change it.
If you want to know the size of a component held by a window "when the window pops up", then add a WindowListener to the window and check the size of the component from the windowOpened(...) method.
Edit 2
Then you state:
after i have the real size, i add some subpanels, in relation to the size of this panel. so e.g. size/6 & the subpanel has now the size-height of size/6.
One Solution: Better not to set the sizes of anything but instead to use the right combination of layout managers and then let them set the proper sizes based on their rules.
Set the visibility using function setVisible(true);

Change preferredSize of a JLabel after setVisible the JFrame

I have a problem when I try to resize a JLabel. In my application appears the next strucutre. Understand every list item like something inside the previous list item.
JFrame (Layout null, fixed size, not resizeable, used by different people).
JPanel (Layout null, with a size of all the window, the place were I put my work).
various JPanel with different Layouts (the areas of content inside the main panel, you can think about it like html divs...).
Inside one of this "divs" with Layout null there are extended Classes of JPanel with Flow Layout.
Inside every one of this extedend Classes are labels with preferredSizes.
The thing is that when after create all of this i call theFrame.setVisible(true); and all works propertly.
But in a moment of the flow of my application I have to change the size of one of the labels. Then, I simply call label.setPreferredSize(d) and the change doesn't change. The function works propertly if I call it before set visible the frame, but not after.
I have the feeling that the problem is that I don't use nothing like pack(), validate(), repaint(), etc. Because I don't know what it works. I try call repaint and validate to the jlabel, and repaint the main panel, but doesn't works.
I'm relative new with awt and swing, and this is for homework. Sorry for my bad use of English language, and thank you for your help.
After you change the size of your JLabel, call revalidate(). This will cause the JLabel to be resized without waiting for an event that triggers a re-layout (such as resizing the parent Frame, etc...).
JLabel lbl_test;
lbl_test.setPreferredSize(new Dimension(100, 100) );
lbl_test.revalidate();
One other thing to keep in mind, as I'm not certain which class you're having problems with. Null layouts (absolute positioning) mixed with layout managers are going to cause some strange things to happen.

The contents of my JComponent only refresh after a manual resize

I am trying to figure out why my JComponent refreshes when I manually drag my window, but it doesn't refresh when I call repaint or revalidate. The data is ready to be displayed, but it just won't show until I manually resize. Can anybody give some suggestions about what I can do or does this sound like it isn't a Swing problem since I tried repaint and revalidate?
One weird things I've noticed is that if I have this code:
sp.setSize(sp.getSize().width, sp.getSize().height+1);
sp.setSize(sp.getSize().width, sp.getSize().height-1);
If the first line is used, then the JComponent will refresh itself. If I use none or both of these lines it will not, which seems bizarre to me.
I am basically just putting a JPanel in a JInternalFrame in a JDesktopPane. There are two main functions for what I am trying to do. One adds the new JPanel and the other tries to refresh it so the new data will show:
public void addNewSP()
{
sp = new JInternalFrame("SP");
sp.setClosable(true);
sp.setLocation(700, 400); //this should be changed to something based on screen size
sp.setResizable(true);
sp.add(popUp);
this.parentContainer.add(sp, JLayeredPane.DRAG_LAYER);
sp.pack();
sp.show();
sp.setSize(500, 500);
sp.setPreferredSize(new Dimension(500, 500));
}
public void refreshSP()
{
sp.repaint();
sp.validate();
sp.repaint();
sp.validate();
parentContainer.validate();
parentContainer.repaint();
sp.setSize(sp.getSize().width, sp.getSize().height+1);
sp.setSize(sp.getSize().width, sp.getSize().height-1);
}
}
BTW parentContainer is the JDesktopPane
When changing the container's content, you have to call both:
revalidate() to make it recompute the layout for its content
repaint() to request a repaint for this container
but it just won't show until I manually resize.
We don't know the context of your question, which is why a SSCCE should always be posted as suggested earlier.
In general a JComponent, does not have a preferred size, so I'm guessing Swing doesn't think it needs to paint the component. When you resize the frame, chances are the component was added to the center of a BorderLayout so it automatically gets sized to fill the entire space of the frame.
The solution is to give your component a "preferred size" so that any layout manager can use this information to display the component properly.
if your are modifying container's subcomponents you should call jcomponent.validate();
I assume parentContainer is the JDesktopPane?
What kind of changes are you making to sp that are not showing up?
Changing the size of sp will cause Swing to repaint from scratch. That's why the setSize() is fixing the display.
Most likely, the changes you are making are either not happening on the EDT, or are not invalidating the right container. For example, if you change the visibility of a component in sp, you'll need to call sp.invalidate() to rerun the layout manager.
Have you checked that you're only changing components (or their models) on the EDT?
A quick test for that is to run with the Substance LAF as it will complain if you change things on another thread.

Categories

Resources