I meet a relayout problem that is in my swing application I add four components (two JLabels, one JTextField and one table, each occupies one row) to a panel by using GridBagLayout, for some logic, I need to make one label visible true or false based on some conditions, but when I invoke setVisible(false) the components below this label will come up a little and when I invoke setVisible(true) they come down a little which makes the GUI flashing. Are there any methods which can make other components stick on the panel? Thanks.
Instead of setting the label visible or not, you could just change the text between the default and "". The Label would still stack the same height but no text would be visible.
How about TableLayout with fixed columns and rows?
Related
i'm trying to make a UI panel that serves like a form (Im not using GUI designer). Left side contains labels, and the right side contains text field, etc. I want to make it, so that when resized, only the right size (the text fields, change shape).
Currently, i am using GridLayout(x, 2) with one JLabel and JTextField per row.
So on resize, i wanna make it so that this goes
from [(JLabel)|(JTextField)]
to [(JLabel)|( JTextField )]
not [( JLabel )|( JTextField )].
Plus, of course, i need to make sure that all of the rows always match.
If you want to use a single layout manager you could use a GridBagLayout.
For the components in the second column you would set the weightx constraint to 1.0f. This means any extra space will only be allocated to this component.
Read the section from the Swing tutorial on How to Use GridBagLayout. The topic on Specifying Constraints will explain in more detail how this constraint works.
GridLayout can not be used for this. Use GridBagLayout or a easier to use layout manager, like MigLayout.
I have a JPanel that uses a horizontal Box layout and contains a JLabel that I would like to keep in the exact same position as other components within the JPanel are setVisible(false). Currently, the JLabel moves to the left as other components become invisible.
What's the easiest way to go about this?
EDIT: Pics added
So this is what the JPanel look like with all components visible
When I set the three JTextFields on the right to invisible, the JLabel set to text X moves to the left like this:
But I would like it to stay where it was like this:
EDIT2: I'm actually using Netbeans GUI editor's Free Design for this particular JLabel. I'm sorry for the mistake - I've been using a lot of BoxLayouts recently and I got confused!
Currently, the JLabel moves to the left as other components become invisible.
Yes, layout managers are designed to only work with visible components. I'm not sure if any of the default layout manager will work, but I would look into using the GridBagLayout, since this layout is based on a grid structure so as long as you have components in that grid on another row the label should not shift.
Otherwise, you could dislay the "other components" in a panel using a CardLayout. Then instead of making the components invisible, you swap the panel with an empty panel.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Edit:
Based on your picture the easiest solution is to use "glue":
panel.add(Box.createHorizontalGlue);
panel.add(xLabel);
Now the label will always be displayed at the far right of the panel. Read the tutorial on How to Use BoxLayout for more information about "glue".
I would hide a JButton in a JApplet. I'm using setVisible() method but I've a problem: it works but my GUI is shifted because of the absence of the component. Is there a way to hide a component and make its space occupied???
I know that is possible in Android, but in Java?
ps. To insert component in my JPanel I'm using GridBagLayout!
There are several ways to achieve this in general.
Most proper way is to layout other components in a way that they remain correctly attached at their current positions.
Since for complex layouts the proper way can be hard to get and especially hard to change afterwards, you can apply some layout 'hacks'. For example, instead of adding the button to the panel directly, you could add the button to a separate panel of its own (let's name it buttonPanel), and then add that panel together with the button to the panel containing the other components. That way when you remove the button, buttonPanel will stay to fill the gap.
However, depending on the way how you specified constraints, buttonPanel may shrink when you remove the button. To prevent this, just before removing the button, take the buttonPanel's width and set it as its minimum/preferred width; most LayoutManagers will respect this property.
Of course, you can always resort to hardcoding dimensions to avoid dynamic size calculations, but keep in mind issues with L&F and i18n.
Try using the setOpaque() method. Just do button.setOpaque(false); and that should do the trick. Does that work?
Disclaimer: I am new to swing.
TLDR:
I am dynamically adding JPanels to an "enclosing" JPanel with a button. All panels use MiGLayout. The outer panel contains one cell only, and more rows are created as inner panels are added, top to bottom ("flowy" constraint). The inner panels are made of two rows --but the second one is revealed dynamically ("hidemode 3" layout constraint, with setVisible(false) until a condition is met.)
What's happening, is when adding an inner panel, and causing its second row to be visible, it overlaps with the panel below it. I have tried to call revalidate and repaint everywhere possible. To me, it would seem that the outer/enclosing panel simply has to revalidate its child components, but this doesn't seem to be happening.
Background
I am using MiGLayout and Swing to create a GUI for importing to my database.
Users should be able to hit a button which adds panels to an enclosing panel.
Specifically, the [portion of the] GUI [in question] is made of...
an enclosing JPanel(black), containing a:
JToolbar(blue) and
JScrollPane(red), which has as its Viewport
another JPanel(green)
this last JPanel, #4, is a container for dynamically added panels via a button (see the second picture below).
The bottom most JPanel, #4, uses MiGLayout. Itself, and the scroll pane that contains it, are hidden from view until a user hits that button next to the "File" comboBox. (not shown in picture above, as that wasn't runtime).
Once pressed, a subclass of JPanel is added to this panel:
(runtime screenshot)
The panels are stacked on top of eachother as more are added.
This inner panel is made of two rows, but the second one is invisible and does not affect the layout until it is visible. It is made visible via listeners once a condition is met within the text field..
The issue
If the second row of an inner panel becomes visible, it overlaps the panel in the row under it. Here's what I mean....
User adds three panels:
Second panel triggers setVisible(true), so now is made of two rows, but hides the third panel:
For dramatic effect (same runtime):
The problem is obvious. How do I prevent this overlap?
Ideally, I'd want the out panel to reorganize itself appropriately to account for the larger size.
Say I add one panel, then trigger the hidden row, THEN add another panel --- there is still overlap. It was my understanding that revalidate() should take care of these things but I have called it every place possible (perhaps too much, if possible??) to no avail..
I have tried many different constraints on the enclosing panel as well as the inner panels, with no luck. I have tried both creating the second row and show/hiding it dynamically, or simply creating it dynamically, with no luck. It consistently overlaps the same way, and I cannot get it to re-layout these inner panels.
I can think of some work arounds, but this seems like it should be possible!
My longest question ever, sorry for that. Any help is appreciated.
Thanks.
It turns out that setting row constraints on the (green) outer panel was what was causing the issue.
I had set min/preferred row size to 30 pixels, with a maxiumum size of 60 pixels (60 being the size of a two row inner panel). Clearly this did not do what I thought it should.
I had heard to be wary using pref/max/min bounds in swing---but I ignored it and this is what happened!
By removing the row constraints completely, the outer (green) panel shifts the rows and what not appropriately.
jframe.getContentPane().revalidate();
I'm using a borderLayout to arrange my components in a JFrame and a Box(BoxLayout.X_AXIS) to put buttons next to each other. But it creates two problems:
I want the buttons to have the same size, but it automatically resizes them to fit the text within them (especially annoying when I change the text inside a button at runtime)
I want the buttons to have a little bit of space between them (let's say 10 px)
Is this possible using the borderLayout, or do I need to use the setLayout to null? And if so, wouldn't this screw up the original placement of the buttons in the frame? Or would this still be dealt with by the Box which is placed with the borderLayout?
A couple of suggestions
Try setting the preferredSize to a suitable Dimension value
If that doesn't work, try also setting the maximumSize and minimumSize to this same Dimension value
If that still doesn't work, change the buttons' layout manager to a GridBagLayout. The advantage of this layout manager is that it lets you control the layout's behaviour in minute detail. The disadvantage is that you usually need to configure a large number of properties on the GridBagLayout in order to get the desired behaviour. I'd advise checking out a GridBagLayout tutorial first, as it's a reasonably complex beast.
If you want them to have the same size then just add the buttons to a GridLayout and they will automatically be sized to the largest text string. You can also specify a gap between components.