Center JButtons inside a JPanel in a JScrollPane - java

I am trying to add JButton components to a JPanel that I added to a JScrollPane. I want that they are vertically aligned. I already found the solution with the BoxLayout(panel, BoxLayout.Y_AXIS) in the panel, but after I did that the buttons had different widths.
I also would like to add some space between the buttons.

You need to use a different layout. Grid or GridBag is what you're looking for, although GridBag is overkill for something this simple:
panel.setLayout(new GridLayout(3, 1));
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));

Use a gridbaglayout , and apply vertical spacers at the button of each button. Also the text in button is not equal to the second and third button dats why it has a different size. So you can adjust it using the preferred size tool

Related

Align JButtons in center of nested JPanel (BoxLayout)

I have a JFrame's child class and have the followiwng layout inside it. I have one big panel and one small buttonsPanel with two JButtons. I add buttons to the smaller panel and add that panel to the first one. Buttons are supposed to be centered, but it doesn't happen.
panel=new JPanel();
add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button1=new JButton("button1");
JButton button2=new JButton("button2");
buttonsPanel=new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(button1, CENTER_ALIGNMENT);
buttonsPanel.add(button2, CENTER_ALIGNMENT);
panel.add(buttonsPanel, BorderLayout.CENTER);
What should I do?
You really need to read the Swing tutorial on Layout Managers. You need to understand what a "constraint" is and when to use it.
buttonsPanel.add(button1, CENTER_ALIGNMENT);
The buttons panel uses a BoxLayout. It does not support any constraint, so the CENTER_ALIGNMENT makes no sense.
panel.add(buttonsPanel, BorderLayout.CENTER);
Again, panel uses a BoxLayout. You can't just use a BorderLayout constraint.
The easiest way to center a component (vertically and horizontally on a frame is to use a GridBagLayout.
So the basic code might be something like:
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(button1);
buttonsPanel.add(button2);
frame.setLayout( new GridBagLayout() );
frame.add(buttonsPanel, new GridBagConstraints());
If you want to try to use a BoxLayout then you need to use "glue" before and after the panel:
Box vertical = Box.createVerticalBox();
vertical.add(Box.createVerticalGlue());
vertical.add(buttonsPanel);
vertical.add(Box.createVerticalGlue());
Again, read the tutorial for more basic information about the BoxLayout.

How to put buttons sticked to borders?

I mean with the standard flow layout I get this:
But I need something like this instead
Is there a way to put buttons like this in JPanel? Which layout should I choose?
Yes, Use BoxLayout and add Box.createHorizontalGlue() in between 2 buttons.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("Left"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("Mid"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("Right"));

Swing display JPanels in a row to the right corner

I am creating a custom decoration for my first customized Swing program window, I just started with layout managers, and it looks like I am doing something wrong, first I used BorderLayout and BorderLayout.EAST or WEST to display on the corner, but it only allows one panel to be displayed on a corner, like it won't display in a row.
Looks like this:
(source: gyazo.com)
With that code:
this.panel.setLayout(new BorderLayout());
this.panel.add(this.createToolButton("X"), BorderLayout.EAST);
But if I add another panel, the newest panel will be on the previous one (Note I used panels because JButton hates me, with it's default styles doesn't let me make it flat)
Now I used GridBagLayout
this.panel.setLayout(new GridBagLayout());
Box panels = new Box(BoxLayout.X_AXIS);
panels.add(this.createToolButton("X"));
this.panel.add(panels, BorderLayout.EAST);
But on run I get
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)
What am I doing wrong? how can I have the panels floated to right one by one?
EDIT:
this.panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.WEST;
this.panel.add(this.createToolButton("X"), gc);
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)
Use GridBagConstraint with GridBagLayout.
this.panel.setLayout(new GridBagLayout());
GridBagConstraint gc = new GridBagConstraint();
// set different properties of GridBagConstraint as per your need
this.panel.add(panels, gc);
Read more How to Use GridBagLayout read more about properties of GridBagConstraint.
Here is The Example to learn more about it.
EDIT
You can try with FlowLayout with right alignment:
JPanel titlePanel=new JPanel(new GridLayout(1,2));
titlePanel.setBorder(new LineBorder(Color.BLACK));
titlePanel.setBackground(Color.LIGHT_GRAY);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
panel.setBackground(Color.LIGHT_GRAY);
titlePanel.add(new JLabel("Title",JLabel.LEFT));
panel.add(new JButton("X"));
titlePanel.add(panel);
frame.add(titlePanel, BorderLayout.NORTH);
// add panel in the north section of the undecorated JFrame
// that by default uses BorderLayout
snapshot:
If you are using
this.panel.add(panels, BorderLayout.EAST);
then you should use BorderLayout not GridBagLayout.
this.panel.setLayout(new BorderLayout());
You can read more in the documentation How to Use BorderLayout.

Prevent JLabels from Growing in MigLayout

I am trying to create a form in MigLayout. I want for any text fields to be labeled with a small JLabel preceding it, with the text field growing as space is available. I am successfully able to do this in the following code:
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new MigLayout("", "fill", ""));
panel.add(new JLabel("Testing"));
panel.add(new JTextField(), "growx, pushx, wrap");
frame.setContentPane(panel);
frame.pack();
frame.setMinimumSize(new Dimension(400, 100));
frame.setPreferredSize(new Dimension(400, 100));
frame.setVisible(true);
The result looks like this, which is as expected (the JLabel is the minimum necessary size, the JTextField takes up the rest of the area):
However, if I put a JEditorPane below this and have it span the length of the whole window, the JLabel becomes larger and grows if I enlarge the window. The code change looks like this:
...
panel.add(new JTextField(), "growx, pushx, wrap");
//New line of code here:
panel.add(new JEditorPane(), "growx, pushx, span");
frame.setContentPane(panel);
...
Which causes a result that I do not expect (the JLabel has grown):
I've tried to fix this by adding MigLayout parameters for the JLabel, like "growx 0" and "shrink", but this doesn't seem to have any effect on the size of the label.
How can I prevent the JLabels from growing in a situation like this?
I figured it out! The problem is with the parameter on the JEditorPane:
panel.add(new JEditorPane(), "growx, pushx, span");
It turns out that when using span, growx, pushx is unnecessary (because it already grows in size) and when used in conjunction with this, it creates the effect shown above. My guess is that growx applies to all cells marked by span, but pushx only applies to the first cell.
So span makes the component take up multiple cells, they all were assigned the default growx weight, but pushx only makes the first cell grow.
So the proper way to fix the line is simply:
panel.add(new JEditorPane(), "span");

Using BorderLayout for adding 4 in-line Components to a JPanel

In my application, there are 4 panels. And i need to insert them into the main panel, which uses BorderLayout. The 4 panels are...
A thin Image strip.
4 buttons just below above
A TextField covering the complete page.
An about at end.
This is my code...
add(imageLabel, BorderLayout.NORTH);
add(buttonPanel,BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
add(about, BorderLayout.PAGE_END);
When I do this, the buttonPanel disappears. How can I achieve what I need?
I usually try to keep a maximum of 3 components in any BorderLayout, so I would do it like this...
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel innerPanel= new JPanel(new BorderLayout());
innerPanel.add(buttonPanel,BorderLayout.NORTH);
innerPanel.add(logScrollPane, BorderLayout.CENTER);
innerPanel.add(about, BorderLayout.SOUTH);
outerPanel.add(imageLabel, BorderLayout.NORTH);
outerPanel.add(innerPanel,BorderLayout.CENTER);
As long as you keep the 'maximum-stretched' component in the CENTER (in this case, your logScrollPane) then it'll always work. If you want to use the panel, such as setting it on a JFrame, just use add(outerPanel).
Don't be afraid of BorderLayout - the ability of this layout to auto-expand the CENTER component to fill the available space make it a very powerful and very important LayoutManager!

Categories

Resources