Java Swing JPanel that doesn't stretch the width - java

Okay so I have a JPanel that is going to have two JLabels inside it, I can't seem to figure out a Layout or a way to make it so both the labels are aligned to the left and only take up the space needed for the content, most layouts I try only show one of the objects or show both space out evenly across the entire width, any help would be appreciated!
I've tried setting a max and preferred size on the panel with no luck =/ Both labels are on the same "row"

..so both the labels are aligned to the left
Put it in a panel with layout:
new FlowLayout(FlowLayout.LEFT)
Or (localized)
new FlowLayout(FlowLayout.LEADING)

You could use a GridBagLayout, for example...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(shrished, gbc);
gbc.gridx++;
gbc.weightx = 1;
add(shrishing, gbc);
"Wow", you say, "that looks complicated, why would I want to do that when other methods look easier"...good question, you could also do this...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
add(shrished, gbc);
gbc.gridx++;
gbc.weightx = 1;
gbc.weighty = 1;
add(shrishing, gbc);
or this...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.SOUTHWEST;
add(shrished, gbc);
gbc.gridx++;
gbc.weightx = 1;
gbc.weighty = 1;
add(shrishing, gbc);
With flexibility comes complexity, but it really comes down to exactly what you want to achieve...

Related

GridBagLayout with buttons

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.

JFrame center components

I was wondering how to center my components, I found the gridBagLayout but I couldn't find a way to have it bigger than it is : my buttons are too small.
Part of my code :
private void addMainButtons() {
JPanel container = new JPanel() {
private static final long serialVersionUID = -424395301619105440L;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension dim = this.getSize();
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
g.setColor(Color.BLACK);
String s = format.format(date);
g.drawString(s, (int) ((dim.getWidth() - s.length() - 80) / 2), 20);
}
};
container.setBackground(new Color(109, 69, 60));
JButton productsButton = new JButton("Produits");
productsButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
cl.show(frame, pages[1]);
}
});
productsButton.setAlignmentY(Component.CENTER_ALIGNMENT);
productsButton.setAlignmentX(Component.CENTER_ALIGNMENT);
// Creating grid
container.setPreferredSize(new Dimension(400, 600));
container.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
container.add(productsButton, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
container.add(new JButton("Entrée"), gbc);
gbc.gridx = 1;
container.add(new JButton("Sortie"), gbc);
mainPage.setLayout(new BorderLayout());
mainPage.add(container, BorderLayout.CENTER);
// End of main page
}
GridBagLayout is really difficult to use, is there another way to center components ? I could use NestedLayouts but I don't know how.
You can use weightx and/or weighty to effect the amount of space the components will occupy, for example, if I do
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.weightx = 1;
gbc.weighty = 1;
container.add(productsButton, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
container.add(new JButton("Entrée"), gbc);
gbc.gridx = 1;
container.add(new JButton("Sortie"), gbc);
I can generate this...
You could use two containers, one holding a JLabel which shows the date and one which contains the buttons, but I doubt that's what you're really after.
You can use ipadx and ipady which adds the amount to the components preferred size
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.ipadx = 40;
gbc.ipady = 40;
container.add(productsButton, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
container.add(new JButton("Entrée"), gbc);
gbc.gridx = 1;
container.add(new JButton("Sortie"), gbc);
which, when included with your existing constraints, allows you to "grow" the buttons a bit.
Try below code for increasing size of button while adding every components to gridbagconstraints. Change the value of weightx/weighty as you want.
gbc.weightx = 1.0;
gbc.weighty = 1.0;
To give more spaces between components, use below code while adding every components to gridbagconstraints.
gbc.insets = new Insets(2, 2, 2, 2);
According to my knowledge, you used correct way to center components. It can also be done by other layout but it may be little bit difficult.

Java GridBagLayout components centering when background filled

I'm trying to create a GridBagLayout, I have got the panels set out exactly how I want them. However I cannot set the background colour of each panel without also forcing all of the components to center.
Here the panels are coloured but all of the components center
http://i.imgur.com/BattBdw.png
Here all of the components snap to NORTHWEST as specified in the code but the background color does not fill the pane.
http://i.imgur.com/lvuEy4u.png
Sorry I cannot embed the pictures, my reputation is not high enough :(
GridBagConstraints GBC = new GridBagConstraints();
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor = GridBagConstraints.NORTHWEST;
//green panel
GBC.gridx = 0;
GBC.gridy = 0;
GBC.weightx = 0.1;
GBC.weighty = 0.1;
JPanel panelGreen = new JPanel();
panelGreen.setBackground(Color.green);
//add button to green panel
JButton button = new JButton("Button");
panelGreen.add(button, GBC);
contentPane.add(panelGreen, GBC);
//blue panel
GBC.gridx = 1;
GBC.gridy = 0;
GBC.weighty = 0.1;
GBC.weightx = 0.9;
JPanel panelBlue = new JPanel();
panelBlue.setBackground(Color.blue);
contentPane.add(panelBlue, GBC);
//red panel
GBC.gridx = 0;
GBC.gridy = 1;
GBC.weighty = 0.8;
GBC.weightx = 0.1;
JPanel panelRed = new JPanel();
panelRed.setBackground(Color.red);
contentPane.add(panelRed, GBC);
//black panel
GBC.gridx = 0;
GBC.gridy = 2;
GBC.weighty = 0.1;
GBC.weightx = 0.1;
GBC.gridwidth =2;
JPanel panelBlack = new JPanel();
panelBlack.setBackground(Color.black);
contentPane.add(panelBlack, GBC);
//yellow panel
GBC.gridx = 1;
GBC.gridy = 1;
GBC.weighty = 0.8;
GBC.weightx = 0.9;
GBC.gridwidth =1;
JPanel panelYellow = new JPanel();
panelYellow.setBackground(Color.yellow);
contentPane.add(panelYellow, GBC);
Firstly, creating a JPanel() with the default constructor means it gets a FlowLayout - you need to tell your panels to use GridBagLayouts:
JPanel panelGreen = new JPanel(new GridBagLayout());
Other than that, you just need to have the "fill" property set to NONE when you add your JButton or it will expand to fill the whole pane. After you've added the button you can set it to BOTH for the panels themselves:
GBC.fill = GridBagConstraints.NONE;
JButton button = new JButton("Button");
panelGreen.add(button, GBC);
GBC.fill = GridBagConstraints.BOTH;
contentPane.add(panelGreen, GBC);

Trouble With GridBagLayout

I am having trouble with GridBagLayout.
A user selects a file, and if that file is bigger than the JTextArea that I display it in, all the Swing components in the shared GridBagLayout go haywire. If it is able to fit in the JTextField, all the Swing components are fine.
Here is a screenshot of the JFrame layout I desire:
And here is the JFrame layout I get when the JTextArea containing the file path is bigger than its allotted size.
All the other JTextFields are affected as well as the buttons below. I don't care if the text in the JTextArea is bigger than its size, I just want all the swing components to stay the same.
Here is the code where I add the Swing components into the File info panel:
//Setup layout.
gbc = new GridBagConstraints();
//File info panel.
gbc.insets = new Insets(3, 10, 3, 10);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
info_panel.add(first_name_jta, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
info_panel.add(first_name_tf, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
info_panel.add(last_name_jta, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
info_panel.add(last_name_tf, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
info_panel.add(frame_type_jta, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
info_panel.add(frame_type_cb, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
info_panel.add(eye_size_jta, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
info_panel.add(eye_size_tf, gbc);
//Frame panel.
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.BOTH;
frame_panel.add(file_path_tf, gbc);
gbc.gridy = 1;
frame_panel.add(info_panel, gbc);
gbc.gridy = 2;
gbc.insets = new Insets(10, 60, 10, 60);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
frame_panel.add(save_button, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
frame_panel.add(cancel_button, gbc);
Any ideas?
Okay, that took a bit of effort.
Basically, when you build the frame_panel, you use gridwidth = 2 for the file_path_tf and info_panel, which is all fine an good, but you never reset the attribute when you add the buttons.
This means that save_button can occupy columns 0 and 1 and cancel_button can occupy 1 and 2
Reset the gridwidth attribute after you've added the info_pane and before you add the buttons
gbc.gridwidth = 1;
Also, I think you'll find JLabels much easier to deal with then JTextAreas for labeling fields, but that's just me.

Setting JButton below TextArea using GridBagLayout

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.

Categories