I just want a GridLayout which has the change to merge cells. So I found the GridBagLayout. It seems that this layout is very flexible. I don't need this flexibility. Is there a way to tell the GridBagLayout that it should use, for example, 20 columns and 10 rows over the whole width and height?? It should look like a GridLayout, but with merging cells.
Thx
As far as I know, you can use the same height and width for more than one JComponent. But you'll have to change them, where you want a merged cell.
Here is an example from how to use GridBagLayout
protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("SansSerif", Font.PLAIN, 14));
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row
.........
}
So you don't need to specify the height and width all the time.
I wish that this was possible, as it would make my project much easier. However, this is the answer:
GridBagLayout offers no methods to explicitly define the number of rows or columns in the grid, nor does it have methods to define the size of each cell in the grid. Instead, GridBagLayout calculates the number of rows and columns in a grid by the number of Components placed on the screen. If a container has five Components lined up horizontally, then the grid consists of five columns and one row. If the Container has five Components lined up vertically, then the grid consists of one column and five rows.
I got it from: http://www2.sys-con.com/itsg/virtualcd/java/archives/0304/tabbone/index.html
For my project, I'm thinking of creating the top row and left column of my GridBagLayout with x and y number of 1 pixel by 1 pixel transparent panels to manually create an excel-like grid and put objects within that grid structure, using gridwidth and gridheight to merge the cells as desired. This should allow me an easy way to create my complicated layout and have everything resize correctly (knock on wood).
Related
I'm using a GridBagLayout Manager to define clearly where (for now) thow labels and a JScrollPane should be.
I use gridbagconstraint to tell the first JLabel named "label1" (with text) he should be 3 unit wide * 1 units tall.
I tell another JLabel named "blank" (empty) he should be 9 units wide * 1 unit tall.
Then I declare a JScrollPane, and tell it it should be on y=1, height = 11, and x=0 width = 12.
I set gridbag's contraint's fill to BOTH, because want each component to fill the rectangle I describe.
label1 should be a quarter of the whole width available, but since label1 has some text, label1 takes 2/3 of the space.
Is there a way to just ignore the PreferredSizes of the components, just like a GridLayout (kind of) does ?
I've read the documentation about this, but I don't find any clue.
Some source code :
public class JPanelMain {
//gridconstraints
private GridBagConstraints gbc;
private static final int min = 1;
private static final int maxSize= 12;
private static final int forth =maxSize/4;
private static final int third =maxSize/3;
public JPanelMain(JFrame frame) {
JPanel pane = new JPanel();
pane.setLayout(new GridBagLayout());
gbc = new GridBagConstraints(
0,//gridx
0,//gridy
0,//gridwidth
0,//gridheight
1,//weightx
1,//weighty
GridBagConstraints.CENTER,//anchor
GridBagConstraints.BOTH,//fill
new Insets(0,0,0,0),//insets
0,//padx
0//pady
);
JLabel label1 = new JLabel ();
label1.setBorder(BorderFactory.createTitledBorder("Label1"));
JLabel blank = new JLabel();
blank.setBorder(BorderFactory.createTitledBorder("blank"));
label1.setOpaque(true);
gbc.gridwidth =(third);
gbc.gridheight = (min);
pane.add(label1,gbc);
label1.setText("Label1");
blank.setOpaque(true);
gbc.gridx = third;
gbc.gridwidth = maxSize -third;
pane.add(blank,gbc);
gbc.gridy = min;
gbc.gridheight = maxSize - min;
gbc.gridx= 0;
gbc.gridwidth=maxSize;
DefaultListModel<String> patients = new DefaultListModel<>();
fill(patients);
JList<String> list = new JList<>(patients);
pane.add(new JScrollPane(list),gbc);
frame.add(pane);
}
private void fill (DefaultListModel<String> model) {
/**/
}
}
the result is as described (and it is the same in height):
I've read (and I believe mostly understood) the example.
There is no such concept as "units" in GridBagLayout. A GBL uses cells.
If you have two components on the first row then you have two cells. Each cell is sized by the component in the cell.
If you then have a component on the second row you have a couple of options. The component could be displayed in the first or second column. Or you could give the component a "gridwidth" of 2, which means it will fill the width of the two columns.
You could use the "weightx" constraint. It indicates how space should be allocated to a component when extra space is available. So if one component could be .20 and the other .80. This is the best way to assign space proportionally however this is only for the "extra" spaces. Each component will originally be sized at its preferred size.
If you want truly relative sizes then you can use the Relative Layout. It allows you to specify the size of components relative to the total space available, so the preferred size can be ignored.
So you would need to create a panel using the RelativeLayout for the first row containing the two labels and then add that panel to the panel using the GridBagLayout.
I want to set the size of my JButtons that are at the center of my screen to become larger but I can't seem to find how to do that using GridBagLayouts.
Here is how it looks like :
Here is my code :
// Client
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
c.gridy = 5;
c.gridx = 5;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(10, 1, 1, 10);
p.add(b[0], c);
// Server
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
c.gridy = 10;
c.gridx = 5;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(10, 1, 1, 10);
p.add(b[1], c);
I want the buttons to take up a larger portion of the empty space around them.
More information was added: Buttons have 50% of the width and [about] 20% of the height of parent [together 50% height including the space in between]. (Slightly rewritten to match the suggestion.)
Solution
Combination of simple Layouts Layouts. Although if you do it like this you will have 3 columns or 3 rows which can't be joined, the rest can easily be changed later:
// row variation
JPanel parent = new JPanel();
parent.setLayout(new GridLayout(3, 1));
parent.add(new JPanel()); // placeholder for 1st row
JPanel row = new JPanel(); // 2nd row
row.setLayout(new GridLayout(1, 3)); // create 3 cells of equal size
row.add(new JPanel()); // 2nd row, 1st cell placeholder
// now you have a 33% x 33% (oops) rectangle in the middle
JPanel controls = new JPanel();
controls.setLayout(new GridLayout(2, 1, 10, 10));
controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10);
controls.add(new JButton("Client"));
controls.add(new JButton("Server"));
row.add(controls); // add 2nd row, 2nd cell
row.add(new JPanel()); // 2nd row, 3rd cell placeholder
parent.add(row); // add 2nd row
parent.add(new JPanel()); // placeholder for 3rd row
Easy, but you won't be able to join the cells later:
JPanel parent = new JPanel();
parent.setLayout(newGridLayout(9, 9));
Bottom line: combine different layout managers, put your 2 buttons inside a panel and put some placeholders inside, then it should also work fine with GridBagLayout. That said, I would try to stay flexible by writing reusable components which can easily be combined with any layout manager. Then you don't have to use placeholders superfluous code in order to display the components correctly.
Old Answer
Alternative Solution: Use BoxLayout
BoxLayout is more intuitive and easier to understand when looking at code (of course this is only an opinion).
Decide how your window is structered (is it more like big horizontal components on top of each other PAGE_AXIS or big vertical components next to each other LINE_AXIS) and use this as the outer BoxLayout:
JPanel content = new JPanel(); // or frame
content.setLayout(new BoxLayout(content, BoxLayout.LINE_AXIS));
Add the components along the axis, where you have more than one component along the other axis use a 2nd BoxLayout. You can space components by creating rigid areas (empty rectangles always having the same size) or by adding glue (expanding like gum together with the components).
content.add(BoxLayout.createHorizntalGlue());
JPanel col = new JPanel();
col.setLayout(new BoxLayout(col, BoxLayout.PAGE_AXIS));
JButton clientBtn = new JButton("Client");
JButton serverBtn = new JButton("Server");
col.add(BoxLayout.createVerticalGlue());
col.add(clientBtn);
col.add(BoxLayout.createRigidArea(new Dimension(1, 10)));
col.add(serverBtn);
col.add(BoxLayout.createVerticalGlue());
content.add(col);
content.add(BoxLayout.createHorizontalGlue());
I can't imagine what do you want, but if you want your button to fill around, you can add
c.weightx = ...; //Specifies how to distribute extra horizontal space.
or c.weighty = ...; //Specifies how to distribute extra vertical space.
button.setMargin( new Insets(50, 50, 50, 50) );
This will add extra space to the button and allow the layout managers to do their job based on the preferred size of the button.
I'm trying to display two buttons next to a text field:
I gave up on the text already, but I really need the two buttons to remain small while the text field should expand with the window:
Currently, I use this layouts:
The text field and the two buttons are both in JPanel.
//JPanel group - the container
//List<JComponent> - the conponents added to JPanel
//int[] weights - weights of components
GridBagLayout lay = new GridBagLayout();
for(int i=0,l=fields.size(); i<l; i++) {
InputDef field = fields.get(i);
GridBagConstraints c = new GridBagConstraints();
if(weights.length<i) {
c.weightx = weights[i];
}
c.fill = GridBagConstraints.HORIZONTAL;
lay.setConstraints(field.getField(), c);
}
group.setLayout(lay);
In the documentation I see that for the components to fill their area horizontally, you should set the GridBagConstraints.HORIZONTAL to the constraints of the components.
Further, I read that you should set different GridBagConstraints#weightx to make the elements take different amount of space.
However running the code above doesn't do anything - the JPanel looks exactly the same with no layout manager whatsoever.
How do I add the JDesktopPane to JFrame using GridBagLayout and set its height and width. If I add JDesktopPane that contains JInternalFrame I don't get anything. But works well in case of GridLayout but the problem is I can't set my desired size in it as GridLayout splits equal space among each component added.
You will probably need to set the fill and weight attributes of the GridBagConstraints...
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
This will cause the component to want to push to the limits of the container and will cause the component to fill it's cell within the grid
This override the components preferred size (for the most part)
Take a look at How to Use GridBagLayout for more details...
In my Java application, I'm writing a component that is used to view PDF files. I had a pretty slick implementation where the user could click on the PDF and drag it to view the areas that didn't fit on the screen. But my boss didn't like it, so now I have to use scroll bars. So I did the obvious thing and just put it into a JScrollPane, but almost no matter what I do it refuses to work.
The PDF just gets converted to a BufferedImage and then I convert it to an ImageIcon so I can add it to a JLabel which gets added to a JScrollPane.
I have a PDFViewer class which subclasses JScrollPane, and the important code is here:
private void drawPDF() {
PDFRenderer renderer = new PDFDrawer(pdfFile);
BufferedImage image = renderer.makeImage(page);
JLabel img = new JLabel(new ImageIcon(image));
this.setViewportView(img);
}
Now I have a separate class which subclasses JFrame that I need to add my PDFViewer to.
It works as long as I don't use a layout and add the PDFViewer directly to the JFrame. If I even just add the JScrollPane to a JPanel and then add the JPanel to the JFrame the scroll bars disappear and it looks like I just added the JLabel directly. The image is too big for this, and it gets cut off easily.
I need to add some controls to the frame as well, so I set up a really basic GridBagLayout with the PDFViewer as the only component being added. And with the following code I get a window that looks like this.
GridBagLayout glayout = new GridBagLayout();
GridBagConstraints c;
setLayout(glayout);
PDFViewer viewer = new PDFViewer("foo.pdf");
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
add(viewer, c);
setVisible(true);
Why does the JScrollPane get smooshed like that when I just simply add it to a layout instead of directly to the JFrame? I found out that it works with GridLayout, but a GridLayout is not what I want.
You need at least ONE component with the weightx/y set to a non zero value for GridBagLayout to work.
You need to specify
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
This means that it will take all available space that is not used by other components. I suggest reading up on GridBagLayout for more information.
Try adding:
c.fill = GridBagConstraints.BOTH;
This should ensure that your panel is resized in both directions when you resize. Incidentally if this is the only component then consider using BorderLayout and adding the component to BorderLayout.CENTER.
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
before adding the viewer.
You need to set the preferredSize(), minimumSize() and maximumSize() of the component you are adding to the JScrollpane. Or you can set the cell to expand horizontally and vertically as far as possible by adding
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
to the GridBagConstraints.
Try setting prefferedsize(setPrefferedSize()) to the component which you are adding to ScrollPane.