This is what I've layed out using a GridBagLayout:
But, when I select a file a couple of labels are supposed to be populated. This is what it looks like after I make a file selection using those small "..." buttons:
As you can see , the entire layout gets messed up. All I am doing in the actionlistener is this:
fileTxt = fileChooser.getSelectedFile();
fileTxtField.setText(fileTxt.getAbsolutePath());
labels = getLabels();
lbl1.setText(labels[0].toUpperCase());
Dimension size = lbl1.getPreferredSize();
lbl1.setMinimumSize(size);
lbl1.setPreferredSize(size);
lbl2.setText(labels[1]);
lbl2.setToolTipText(longLbl);
size = lbl2.getPreferredSize();
lbl2.setMinimumSize(size);
lbl2.setPreferredSize(size);
button1.setPreferredSize(new Dimension(20,25));
button2.setPreferredSize(new Dimension(20,25));
So, basically, the buttons are going to their original sizes and not preferred sizes.and that messes up the entire layout. How do I fix this? All components are set not to fill with the gridbagconstraint of gridBagConstraints.fill set to GridBagConstraints.NONE - however, the layout still gets messed up :(
UPDATE
As per your suggestions, I removed the code that was calling setPreferredSize() method, and this is what I get:
Obviously, this is what I want to avoid - a button, that is bigger than its text, that was reason for setting setPreferredSize on the button. Now what do I do?
Don't call setPreferredSize(). Let LayoutManager do this.
In your GridBagConstraints define fill and weightx parameters to define how extra space should be distributed.
There is a simple solution to this. The extra width of the buttons is due to the default margins on the left and right of the button text. By default it is 14
you can set btn.setMargin(new Insets(2, 0, 2, 0)); and that extra gap will go.
Related
I'm trying to center the JLabel text and to change the background and foreground color. I tried to use HTML:
JLabel listHeader = new JLabel("<html><span style='background-color:#555;color:White;text-align:center;'>Tables</span></html>");
But it doesn't seem to work as you can see in the image below.
Image of the components
The label is inside a panel that has the layout manager set to grid bag layout with the following constraints:
c.gridy=0;
c.gridx=0;
c.weighty=0;
c.weightx=1;
c.fill=GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
I tried with fill BOTH as well, same result.
How can I make the label take all the width space?
I tried to use HTML:
Don't use HTML.
I'm trying to center the JLabel text
To center the text in the space available to the label you can use:
label.setHorizontalAlignment(JLabel.CENTER);
and to change the background and foreground color.
You need to make the label opaque:
label.setOpaque( true ):
label.setBackground( Color.RED );
I tried with fill BOTH as well, same result.
Well, I don't think you want "BOTH" since you are only concerned with the width. You may also need to use the weightx constraint. Read the section from the Swing tutorial on How to Use GridBagLayout for more information on using the constraints.
Without your MCVE/SSCCE we can't see exactly what you are doing.
You could try to anchor at the center, not on the start of the line.
I am attempting to put a JGoodies panel into a JScrollPane with only a vertical scroll bar; any elements larger than the current JScrollPane width should be truncated. However I can't figure out a way to make this work
Example of the effect I'm going for
What I don't want to happen
My current code is essentially:
FormLayout locationsLayout = new FormLayout("15dlu, pref, 5dlu, pref, 5dlu, pref:grow", "");
locationsBuilder = new DefaultFormBuilder(locationsLayout)
.background(Color.WHITE)
.lineGapSize(Sizes.ZERO);
locationsPane = new JScrollPane(locationsBuilder.getPanel());
locationsPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
locationsPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//...Sometime later, the user adds a folder...
FormLayout headerLayout = new FormLayout("pref, pref", "pref");
DefaultFormBuilder headerBuilder = new DefaultFormBuilder(headerLayout)
.background(Color.WHITE)
.lineGapSize(Sizes.ZERO);
headerBuilder.add(curContainer.getGuiHeader(), CC.xy(1, 1));
headerBuilder.add(curContainer.getGuiTablePrefix(), CC.xy(2, 1));
locationsBuilder.leadingColumnOffset(0);
locationsBuilder.append(headerBuilder.getPanel(), 6);
Things I've tried
Various permutations of min, pref, grow, fill, etc. Nothing changed this behavior
Passing a custom JPanel that implements Scrollable to the locationsBuilder DefaultFormBuilder constructor as documented here, here, or here
Trying the other vertical scroll bar options on JScrollPane
I don't know what else I can try. Does anybody have any suggestions?
I never could find an exact answer to this specific setup. My guess is that JGoodies dies bit handle nested layouts very well.
I ended up "fixing" this by using only one single panel for the entire locations scroll pane. This made the layout a bit complicated: Multiple cells now had to span columns and I had to manually adjust the column offset. But in the end it works
I have a JButton that is much wider than the text I put into it. I've researched this, and I keep finding the suggestion that I use Jbutton.setMargin(new Insets(0,0,0,0)); But this just does not seem to work. Also, setMaximumSize has no effect, although if I also set a minimum size, it does change the size of the button. But I don't want to set the size manually. I just want it to be less wide. What am I missing?
Here's my code to create the button:
plusminus = new JButton("+");
plusminus.setMargin(new Insets(0,0,0,0));
And here's what it looks like:
Thanks.
I'm manually making my GUI. In this case, the layout is GroupLayout
Then that may be part of your problem. Your JButton's size is constrained by the layout of the container that holds it. One possible solution if you absolutely need to use GroupLayout (which I hate by the way), is to place your JButton inside of a JPanel that uses FlowLayout or some other layout that allows flexible sized components, and place this JPanel into the container that's currently holding your button. Beware though if your button's bigger than the JPanel.
On a lark i tried negative left & right insets & unbelievably it worked. I did not then need to mess w/ the min/max/pref sizes. Btw my buttons are in one column of a JTable.
I have got a window that looks like window1 and I would like it to look like window2:
This is my code:
String q = "Have you used GUI before?";
JLabel textLabel2 = new JLabel(
"<html><div style=\"text-align: center;\">" + q + "</html>", SwingConstants.CENTER);
add(textLabel2, BorderLayout.NORTH);
JPanel radioPanel = new JPanel();
add(radioPanel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
add(btnPanel, BorderLayout.SOUTH);
For the radio-buttons, I tried to use GridLayout, but it broke the position of "Yes" and "No". And for the "back" and "next" buttons, horizontal alignment did not work (btnPanel.setAlignmentX(RIGHT_ALIGNMENT);), apparently. Any solutions will be highly appreciated, I'm stuck with this bit way too long. Thanks
--EDIT--
That is working perfectly fine:
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS));
btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
btnPanel.add(Box.createHorizontalGlue());
so the buttons problem is solved.
However, still can't get the radio-buttons fixed.
--EDIT 2--
Fixed the background for the radio-buttons using setOpaque(false);
What do you mean by it "broke" the position of "yes" and "no" as a GridLayout should work just fine. I'd give it 1 column and 2 (or 0 for variable number of) rows via new GridLayout(0, 1). Be sure that its opaque property is set as false by doing radioPanel.setOpaque(false);. This way it will show the background color of the container that it sits in. You may need to make the JRadioButtons non-opaque as well, I'm not sure.
Your btnPanel could use a BoxLayout and use Box.createGlue() to push the buttons over to the right side.
Most importantly -- if you haven't yet done so, please read the tutorials on use of the Swing layout managers which you can find here.
A couple of things you can do about this. You need to change your LayoutManager. This is not a great task for BorderLayout. You could do nested BoxLayouts. A vertical box that has the vertical fixed height strut, label, vertical fixed height strut, yes radio, vertical fixed strut, no radio, Vertical glue, and the final button panel. Then use your edit in the button panel to horizontally align them. That's one option, but the nesting of the panels is annoying.
Another option go get TableLayout and learn how to use it. TableLayout is one of the best LayoutManagers. It's easy to use, solidly tested, and it makes Swing fun again. You'll never use GridBagLayout ever ever ever again.
http://java.sun.com/products/jfc/tsc/articles/tablelayout/
The final option is use the new GroupLayout. I'm not terribly familiar with it, but it looks pretty easy. And, it doesn't take as much code or nesting unnecessary panels like Box does.
I want to leave the default border on my JButtons, but put empty space around them as well. I'm using a vertical BoxLayout.
I originally said nothing about the borders, and got single pixel LineBorders, which I want, but the buttons all butted up against each other.
I then tried button[i].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)). Rather than adding blank space around the button, it made the buttons' areas expand. It also removed the LineBorder.
I then tried: button[i].setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5), button.getBorder()))
This gave me back the LineBorder, but rather than adding blank space outside the line, it just extended the buttons' areas beyond the line!
I realise I can add blank boxes to space my buttons out, but I want space on the sides of them as well, which is why I want to add an EmptyBorder. I'm new to Swing, so maybe there's an entirely better way of doing this that I don't know about :)
I'm using Jython, but the API should be the same as from Java.
Conceptually, the "empty borders" you want to add are not really part of the button (for example, they should not be clickable).
This is actually a matter of layout, so you should probably check the documentation of the layout manager you are using. For example:
Some layout managers, such as FlowLayout, BorderLayout, or GridLayout, have hgap and vgap properties to specify the horizontal and vertical gaps between components.
With GridBagLayout you would set the insets of a GridBagConstraints object.
With BoxLayout you would add "rigid areas", "glue", and "fillers" (see the Box class).
And so on.
I think it's simpler to add the button to a panel and set the empty border to the panel.