While playing a bit to demonstrate how to easily fulfil a layout requirment with MigLayout, I was surprised by the outcome of:
MigLayout layout = new MigLayout("wrap 3, debug");
JComponent content = new JPanel(layout);
content.add(new JLabel("First:"));
content.add(new JScrollPane(new JTextArea(10, 20)), "skip, spany");
content.add(new JLabel("Second"));
content.add(new JTextField(10));
content.add(new JLabel("third"));
content.add(new JTextField(10));
//content.add(new JLabel());
The layout idea is simple enough:
three columns
last column spanning all rows
first two columns a bunch of label/component pairs
The unexpected is that the last row of the first two columns takes all the available vertical space which leads to positioning the last pair in its middle (top align is not an option, as they must be baseline aligned to each other)
uncommenting the last line above (adding a virtually invisible dummy) shows the expected layout, but a hack which shouldn't go into production code
The question is: how to achieve the expected layout without hacking?
might be a bug:
a less hacky way to workaround (applicable if number of rows is known at creation time of the form) is to explicitly define the row constraints
MigLayout layout = new MigLayout("wrap 3, debug", "", "[][][][]");
that is define one row more than actually needed for the components at the side of the spanning component
Related
I'm having problems with my GridLayout whereby a JTextField widens to fit an entry longer than the initial size. Another poster with this problem was told SpringLayout was the easiest solution, so that's what I'm trying. However I'm having problems getting this panel to even display.
I've top, bottom, left, and right panels each with different layouts that are set in a top-level panel (the only one in this frame). This SpringLayout I'm trying is to be in the right panel (added to the top panel as topPanel.add(springPanel, BorderLayout.EAST - this was how I did it when this panel was a GridLayout). The code below is modeled after the SpringLayout Oracle tutorial. Sorry I'm new to this layout and am only using it to use fixed widths for JTextFields.
JPanel testPanel = new JPanel();
SpringLayout layout = new SpringLayout();
testPanel.setLayout(layout);
JLabel label = new JLabel("First field: ");
JTextField field = new JTextField("enter text");
testPanel.add(label);
testPanel.add(field);
layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, testPanel);
layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, testPanel);
layout.putConstraint(SpringLayout.WEST, field, 5, SpringLayout.EAST, label);
layout.putConstraint(SpringLayout.NORTH, field, 5, SpringLayout.NORTH, testPanel);
...
topPanel.add(testPanel, BorderLayout.EAST);
Any guidance would be appreciated. Thanks.
EDIT: Adding the cols argument doesn't seem to work, but I'll keep trying. The same problem exists with GridBagLayout. Tabbing to the next field doesn't re-size the previous field, but clicking outside those fields elsewhere in the frame causes it to expand the length of its containing text which is what I need to avoid. And I don't have enough rep pts to post images of what I'm doing
EDIT2: Adding cols works - I tried that at some point. Not entirely sure how it's working, but any value (1, 10, 15 tested) seems to fix the length so leaving that panel's focus no longer causes a size change. Thanks!
#Andrew Thompson's answer worked - use col arg to specify # of columns (any number seems to work). Thank you!
i'm new here so go easy on me.
I've researched all the documentation on miglayout (which is pretty good btw) but I can't seem to properly display the grid that I want.
I need MigLayout() parameters to setup a grid as:
2 rows, being that the bottom row is split in half (or if you will: 1 top row adjacent to 2 bottom columns).
The top row will display the search textfield with an adjancent button.
The bottom left column will display 3 buttons stacked on top of each other.
The bottom right column will display an image label.
Here's some code to start with:
//main window panel setup
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
mainPanel.setLayout(new MigLayout(""));
//components insertion into panel (using MigLayout constraints)
mainPanel.add(searchText);
mainPanel.add(searchBtn, "wrap");
mainPanel.add(addBtn);
mainPanel.add(logoImage, "spany 3 , wrap");
mainPanel.add(randomBtn, "wrap");
mainPanel.add(getFileBtn);
(could not upload image)
With this code, Notice that the logo is being kept below the searchButton (i think it does this due to the layout being default-set as a grid) but I want it to fit adjacent-right to the buttons and below the textfield/searchButton.
First off I would say you are pretty close and you only need to play with both columns number and constraints (plus some missing component constraints). This can be done when you instantiate your layout:
MigLayout layout = new MigLayout("debug, fillx", "[][grow][]");
Note debug and fillx are layout constraints intended to enable debug feature and fill whole width, respectively. See this answer for more details.
Now you can think you need two columns, but you actually need three columns in order to expand the middle one, while first and last columns continue occupying the minimum possible width. That's what [][grow][] constraints mean.
Please consider this snippet:
MigLayout layout = new MigLayout("debug, fillx", "[][grow][]");
JPanel content = new JPanel(layout);
// First row
content.add(new JTextField(20), "spanx 2, growx"); // search text field
content.add(new JButton("Search"), "wrap");
// Second row
content.add(new JButton("Button # 1"), "growx");
content.add(new JLabel("Image here"), "span 2 3, grow, wrap"); // image label
content.add(new JButton("Button # 2"), "growx, wrap");
content.add(new JButton("Button # 3"), "growx, wrap");
This will produce something like this:
Please note that on horizontal resizing both search text field and image label will occupy the maximum available width, while search button and button's stack will occupy just the minimum possible width.
I have configured my layout like that:
panel.add(addButtons(), "wrap");
panel.add(showTable(), "growx, wrap");
So at first I am adding a button group and then I would like to grow my table as large as it can and then wrap the next component in the next "line".
However, my gui looks like that:
Here you clearly cannot see any values from the table. Therefore, how to grow the table so that each value can be seen?
I appreciate your answer!
Here you clearly cannot see any values from the table. Therefore, how to grow the table so that each value can be seen?
As I've said in my comment, I don't think your problem is about columns (preferred | min | max) sizes but the default behavior of your layout manager: MigLayout. As stated in this answer by default rows in MigLayout doesn't fill all available width but just the necessary to display the longest row (based on components width). You can see this fact if you enable "debug" feature when you instantiate your layout:
MigLayout layout = new MigLayout("debug");
As I understand your question you need a combination of both growx and fillx constraint. The first one is a component constraint and the other one is a layout constraint.
That being said, pelase consider the following progression.
1. Adding scroll pane without "growx" constraint
Snippet
MigLayout layout = new MigLayout("debug");
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane);
Screenshot
2. Adding scroll pane with "growx" constraint
Snippet
MigLayout layout = new MigLayout("debug");
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane, "growx"); // Note "growx" here
Screenshot
3. Adding scroll pane with "growx" and "fillx" contraints
Snippet
MigLayout layout = new MigLayout("debug, fillx"); // Note "fillx" here
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane, "growx"); // Note "growx" here
Screenshot
You can use javax.swing.table.TableColumn;
TableColumn custom_column = yourTable.getColumnModel().getColumn(1); // 1 means column 1
custom_column.setPreferredWidth(500);
As you want to widen all columns you can use loop :
TableColumn custom_column ;
int numberOfColumns = yourTable.getColumnModel().getColumnCount();
for(int y= 0;y<numberOfColumns;y++){
custom_column=table.getColumnModel().getColumn(y);
custom_column.setMinWidth(500);
}
I have for example 3x3 components in grid layout, and I would like all of them to be labels, but since I have some padding I want my labels to be in center of their cell. But I can't seem to manage it...
Part of the relevant code:
panel = new JPanel();
GridBagLayout gridBag = new GridBagLayout();
panel.setLayout(gridBag);
panel.setSize(new Dimension(30, 400));
GridBagConstraints c = new GridBagConstraints()
JLabel lab = new JLabel("proba");
lab.setBorder(outline);
c.fill = GridBagConstraints.BOTH;
c.gridx =2; c.gridy=2; c.ipady = 10; c.ipadx=10;
c.ipadx=100; panel.add(lab,c);
[update]
You really should post an SSCCE so we can all try it and not have to guess where the problem is. My last guess - and this is something you should do regardless of whether it fixes your current problem - is
label.setHorizontalAlignment(JLabel.CENTER);
If your label is centered and fills the entire grid cell, the text will still be left justified by default. The above change will cause the text to be centered within the label.
However if your label is not filling the cell, this won't help.
[original]
It's hard to say exactly what's wrong since this is not a full program, but here are a few comments that might get you on the right track.
First, you should be using setPreferredSize instead of setSize (see Java: Difference between the setPreferredSize() and setSize() methods in components)
When you make this change you will see that the panel is not quite what you want. It's very tall and thin. Perhaps a typo - did you mean (300,400) instead of (30,400)
Now I'm guessing all your labels will be clumped together. In order to get them to spread out you need to add:
c.weightx = .5;
c.weighty = .5;
(actually any non-zero value will work). This is described in http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
This should get you close(r) ...
In a Java project I am working on, my constraints were originally set to:
JPanel panel = new JPanel();
String layoutConstraints = "fill";
String columnConstraints = "[right]rel[grow]";
String rowConstraints = "[]10[]";
panel.setLayout(new MigLayout(layoutConstraints, columnConstraints, rowConstraints));
From what I understand of the documentation the last gap and constraint will be repeated for every row in excess of the specification. (cf. "If there are fewer rows in the format string than there are in the grid cells in that dimension the last gap and row constraint will be used for the extra rows.")
So for all I understand, this should mean the row constraints would in fact be []10[]10[]10[] and so on.
My problem comes when I have a JTextArea in a JScrollPane with rows set to 3:
JLabel label = new JLabel("Text area");
JTextArea text = new JTextArea("Test", 3, 40);
JScrollPane scroll = new JScrollPane(text);
panel.add(label);
panel.add(scroll, "grow,wrap");
When used with a row constraint of []10[] is only 1 row high, yet when I change
String rowConstraints = "[]10[]";
to
String rowConstraints = "[]";
it suddenly uses the full 3 rows height that I specified for the JTextArea.
Have I misunderstood how the row constraints work or is there something else at work here that I totally failed to see/understand?
In both cases you are using only one layout row (i.e. row managed by MigLayout) to which you put JLabel (first column) and JScrollPanel (containing JTextArea, second column).
You can easily visualize your MigLayout rows and columns structure by simply adding "debug" to layout constraints:
String layoutConstraints = "fill,debug";
And launching application. I really recommend using this setting while working on your layouts.
Rows property that you set for JTextArea have nothing to do with layout rows. This property only tells JTextArea component what size it will try to have, as defined in javadocs:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane (...)
But JTextArea rows doesn't correspond by any means to rows in container's (JPanel in your case) layout.
Please, learn more carefully (and experiment!) what exactly JTextArea row property means.