0

I want to have a JLabel and a JButton next to each other, in the next row an other Jlaben and in the next row a JButton that is in the center.

The leftPanel is 350 wide.

The first JButton has an ImageIcon and that should be in the right top corner, that image is 25x25.

This code returns a panel to get then shown

JPanel leftPanel = JPanel();

leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

JLabel userLabel = new JLabel();
userLabel.setPreferredSize(new Dimension(170, 25));
userLabel.setMaximumSize(new Dimension(170, 25));
leftPanel.add(userLabel);

// class that creats a JButton with an ImageIcon and removes border
JButton updateButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/update.png"));
updateButton.setPreferredSize(new Dimension(170, 25));
updateButton.setMaximumSize(new Dimension(170, 25));

leftPanel.add(updateButton);

timeStampLabel.setPreferredSize(new Dimension(350, 25));
timeStampLabel.setMaximumSize(new Dimension(350, 25));
leftPanel.add(timeStampLabel);

unlockButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/unlock.png"));
unlockButton.setPreferredSize(new Dimension(350, 25));
unlockButton.setMaximumSize(new Dimension(350, 25));

leftPanel.add(unlockButton);

panel.add(leftPanel();

Now it's just in the center and I don't know what to do with the blue button. image

Updated Code:

JPanel panel = DefaultLayouts.panel();
panel.setLayout(new GridLayout(0, 2));

JPanel leftPanel = DefaultLayouts.marginPanel(0, 10, 0, 10);
leftPanel.setLayout(new GridBagLayout());

GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;

gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
leftPanel.add(userLabel, gridBagConstraints);

JButton updateButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/update.png"));
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
leftPanel.add(updateButton, gridBagConstraints);

gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
leftPanel.add(timeStampLabel, gridBagConstraints);

unlockButton = DefaultLayouts.imageButton(new ImageIcon("src/ressources/images/icons/unlock.png"));
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
leftPanel.add(unlockButton, gridBagConstraints);

panel.add(leftPanel);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RHo
  • 3
  • 3
  • 1
    Can you please add images with what do you have and what do you want? – josivan Mar 04 '16 at 14:07
  • Sorry for bad quality. I want the blue box next to the green one and the image should be in the top right corner. The color are removed in the code above. Also the `leftPanel` has a margin that can be ignored. [image](http://i.imgur.com/GPbSe3X.jpg) – RHo Mar 04 '16 at 14:19
  • Take a look in this [link](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html). Let me know if you understand the explanation. – josivan Mar 04 '16 at 14:27
  • I'll look in to it and try to it. – RHo Mar 04 '16 at 14:43
  • I've updated my code but it still doesn't do what i want, it doesnn't help to set a preferred and max size. @josivan – RHo Mar 04 '16 at 15:26
  • Actually, isn't mandatory to define the preferred size.Try to define de size in panel that have the layout. – josivan Mar 04 '16 at 15:43
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 4) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Mar 04 '16 at 16:06

1 Answers1

0

You can use a FlowLayout or GridLayout.

JPanel panel = new JPanel();

panel.setLayout(new FlowLayout());
//or panel.setLayout(new GridLayout(rows, cols));

panel.add(new JLabel("Label"));
panel.add(new JButton("Click"));
Code_Bugs
  • 5
  • 2