I have a GridBagLayout, and for some reason my JTextArea has decided that it doesn't want to listen to gridbag's ratio for with, and when create the panel which contains the layout my textbox grows till it takes up 1/2 of the screen. Heres some code:
tp = new JTabbedPane();
tp.setFont(Main.f);
//Adds tabs with several buttosn to my tabbed pane
for(Menu menu : FileManager.menus){
JPanel tmp = new JPanel();
int s = (int) Math.ceil(Math.sqrt(menu.products.size()));
tmp.setLayout(new GridLayout(s,s));
for(Product p : menu.products){//Act as
p.setFont(Main.f);
p.addActionListener(this);
tmp.add(p);
}
tp.addTab(menu.name,null,tmp,null);
}
receipt.setBorder(BorderFactory.createEtchedBorder());
//starting up with GridBag
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.ipadx = 0;
gbc.ipady = 0;
//sets up and adds the JTabbedPane
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.8;
gbc.weighty = 0.8;
add(tp,gbc);
//sets up and adds receipt - The one that takes up half the screen
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.2;
receipt.setEditable(false);
receipt.setText("Ticket number: 0\n" +
"Transaction number: 0\n");
receipt.setLineWrap(true);
add(receipt,gbc);
//sets up and adds a JPanel that has a bunch of buttons on it(Uses gridlayout)
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 0.2;
add(buttons,gbc);
buttons.setLayout(new GridLayout(1,8));
createButton(newtable);
createButton(remove);
for(JButton a : quicks)
createButton(a);
createButton(pay);
createButton(exit);
revalidate();
repaint();
When I change the TextArea to a blank JButton it doesn't take up much space at all. Basically the space is filled by the object on the right. How do I tell gridbag that I want my left object to span 4/5ths of the screen, and the right object to span the last 1/5th? In this version I attempt to do so using weighting, in my original version I attempted to do so using cells, so the left object was at gridx=0 gridwidth = 4, and the right object was gridx = 4 gridwidth = 1
Neither way worked. I am also open to alternate ideas(like better layouts, or a JTable?)
Thanks for your help,
Chase
What it's doing
Dimensions for what it should do
If you are not fixed on precisely 4/5 and 1/5 ratio, you can use BorderLayout and place buttons to the center and text area to the East.
Border layout will display the text area as wide as you supply - specify preferred size (width, you can set height to whatever number you want, border layout will ignore it). The rest of the space is then taken up by buttons panel.
You can learn more about border layout here: http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
Related
I am trying to draw a panel with buttons, that looks something like this:
I create the "buttonPanel" like this:
buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridx = 0; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A0"), gbc);
gbc.gridx = 1; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A1"), gbc);
gbc.gridx = 2; gbc.gridy = 0;
gbc.gridwidth = 2; gbc.gridheight = 2;
buttonPanel.add(new JButton("A2"), gbc);
gbc.gridx = 0; gbc.gridy = 2;
gbc.gridwidth = 3; gbc.gridheight = 2;
buttonPanel.add(new JButton("A3"), gbc);
gbc.gridx = 3; gbc.gridy = 2;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A4"), gbc);
But the result looks like this:
As button "A3" has a gridwidth=3, it should reach the middle of button "A2"?
Any help?
Edit1:
Increasing the panel size, or setting a longer text on the buttons, does not change anything:
Edit2:
Setting the weightx=1 for button "A2" and "A3" helps:
This is acceptable for me, although it would be nice if button "A4" would have the same width as button "A0" and "A1"
Set for A2 and A3 the weightx to 1.0.
although it would be nice if button "A4" would have the same width as button "A0" and "A1"
You can't have a cell take up a partial space of another cell.
So you need to "fake" it by creating 4 (invisible) dummy components. This allows you to define a grid of 4 columns.
Then A2 would have a gridwidth of "2" and A3 a gridwidth of "3". The others would have a gridwidth of 1. So now each row has a total cell widths of 4 to match the dummy cells.
Check out: Why does this GridBagLayout not appear as planned? for an example of this approach.
Or an easier option is to use the Relative Layout. It allows you to give components a relative size. So you would need two panels. In the first the components would have relative sizes of 1, 1, 2 and the second 3, 1.
Having small issues on the positioning of my JLabel components.
Code:
public class delete {
public static void main(String[] args){
GridBagConstraints gbc = new GridBagConstraints();
//Adding the JPanels. Panel for instructions
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
//JLabel for the Instructions.
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.weighty = 1;
panel.add(label, gbc);
//JLabel1 for Assingment/Grade/Weight(Percent)
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>");
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(label1, gbc);
//New frame set
JFrame frame = new JFrame("Grade Calculator");
frame.setVisible(true);
frame.setSize(750,500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(panel);
}
}
Now right now this positions JLabel (label) at the top of the frame which is what I want. The problem is JLabel (label1) I want label1 to be Slightly below label.
If I set gbc.gridy = 0, label1 shows on top of label making both of the separate collide together.
When I set gbc.gridy = 1, label1 goes all the way to the bottom of the frame.
Now I want label1 just to be slightly under label however I cannot use floats with gridy. How can I fix this?
gbc.weighty = 1; is causing the component to be provided with all the remaining space left over after all the components are laid out. Instead of providing label with this constraint you should give it to label1 only
When you don't provide a gridx/gridy to component, GridBagLayout places the component next to the last component, since no other components have been added, label is placed at the top, but you're also supplying a gridx/gridy of 0 to label1, which is placing at the same location.
Instead, place label at 0x0 and label1 at 0x1
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 0;
panel.add(label, gbc);
//JLabel1 for Assingment/Grade/Weight(Percent)
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
panel.add(label1, gbc);
You can also effect the position that a component will align to within it's cell through the use of the anchor property.
Have a look at How to Use GridBagLayout for some more details
Without knowing "exactly" what you're doing, I might suggest also having a look at How to Use Tables
//JLabel for the Instructions.
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(label, gbc);
//JLabel1 for Assingment/Grade/Weight(Percent)
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>");
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(label1, gbc);
I want to create an application with buttons arrayed into invisible table. The buttons should fill all the available space inside the imaginary cells even when I´ll resize the frame. I´m using Swing, GridBagLayout. I've read some articles and the solution was to add .weightx=1 and .weighty=1. Weightx works perfect and it fill the gaps but weighty doesn't. Buttons don´t extend into height of cell. Is there a problem with my code or is there something to add that solve my problem? Or should I use absolutely another layout?
public class NewClass {
public void NewClass(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
JButton B11 = new JButton("11");
panel.add(B11,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
JButton B12 = new JButton("12");
panel.add(B12,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
JButton B21 = new JButton("21");
panel.add(B21,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
JButton B22 = new JButton("22");
panel.add(B22,gbc);
frame.pack();
frame.setSize(800,400);
frame.setVisible(true);
}}
You're using the wrong GridBagConstraints#fill field value, since the value you're using is telling the layout manager to fill the buttons horizontally only.
You need to change
gbc.fill = GridBagConstraints.HORIZONTAL;
to
// gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.BOTH;
if you want the buttons to fill both directions, both horizontally and vertically.
I have a TabbedPane inside of which there are Panels.
I am dealing now with the first panel.
Inside this panel there should be another small panel on the left(I just placed a textfield instead for reference) a textarea in the center and below it a row of buttons. But the buttons are placed far too low below from the textarea. Besides, if I resize the frame, the textarea just goes away, vanishes. The textfield is set ok, top left, the textarea more or less yes in the center, but it should not go away when resizing (it would make a weird effect for the user).
I have tried all variants south, west, whatever, no changes or it goes up too far anyways.
I have read somewhat similar questions but I dont see how their answers solve my case.
So, here is the code:
super("Proyecto");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1200, 800);
/* 1. CREATE PANEL*/
/*MAIN PANEL BOOK READER*/
JPanel panelbookreader = new JPanel();
/*CREATE TEXTAREA*/
JTextArea areatextobookreader = new JTextArea(20, 80);
/* 2. ADD THAT COMPONENT TO ITS PARENT*/
panelbookreader.add(areatextobookreader);
/* 3. SET A LAYOUT FOR THE PANEL THAT WILL ORGANIZE COMPONENTS INSIDE*/
panelbookreader.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextField hold = new JTextField(20);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
panelbookreader.add(hold, gbc);
//TEXT AREA
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 0;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.CENTER;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
panelbookreader.add(areatextobookreader, gbc);
//BUTTONS
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.SOUTH;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
panelbookreader.add(openbook, gbc);
/*ADD THE PARENT TO ITS PARENT OR TO GRANPARENT OF COMPONENT*/
JTabbedPane tabulado = new JTabbedPane();
tabulado.addTab("BOOK READER",panelbookreader);
Your weightx for JTextArea is 1.0 which is fine.
But when you specify weighty also as 1.0, then there is no space for JButton. Make some changes in weighty of textarea and also specify weighty for jbutton.
I would like to create a layout: 2 rows, 1 column. 1st row should occupy 70% height of the window and 2nd row 30% of the window. I achieve this by using weighty attribute of GridBagConstraints.
However I have problem with the width of my component, because when I resize application window the component remain in the center, its width is constant and I get a white spaces in the left and right of the component (even if I set fill to BOTH). This problem does not occur when I change the height of the window (components resize very well and fill full height of the window).
Below my constraints:
firstConstraints.gridx = 0;
firstConstraints.gridy = 0;
firstConstraints.weighty = 0.7;
firstConstraints.fill = GridBagConstraints.BOTH;
secondConstraints.gridx = 0;
secondConstraints.gridy = 1;
secondConstraints.weighty = 0.3;
secondConstraints.fill = GridBagConstraints.BOTH;
I think you also need:
gbc.weightx = 1.0;
See the secton from the Swing tutorial on How to Use a GrigBagLayout that talks about the weightx, weighty constraints.
simple example
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderPanels extends JFrame {
private static final long serialVersionUID = 1L;
public BorderPanels() {
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel1 = new JPanel();
Border eBorder = BorderFactory.createEtchedBorder();
panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 70;
getContentPane().add(panel1, gbc);
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
gbc.gridy = 1;
gbc.weightx = 30;
gbc.weighty = 30;
gbc.insets = new Insets(2, 2, 2, 2);
getContentPane().add(panel2, gbc);
pack();
}
public static void main(String[] args) {
new BorderPanels().setVisible(true);
}
}