Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to align swing buttons vertically ?

Reply
Thread Tools

How to align swing buttons vertically ?

 
 
Olivier Scalbert
Guest
Posts: n/a
 
      11-13-2011
Hello !

I want to create a swing application with a main frame and a panel
containing vertically aligned buttons at the right side. Here is the code:

import java.awt.*;
import javax.swing.*;

public class TestViewer {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new ViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(800, 600);
frame.setVisible(true);
}
});
}
}

class ViewerFrame extends JFrame {

public ViewerFrame() {
getContentPane().add(new JPanel(), BorderLayout.CENTER);
getContentPane().add(createBtnPanel(), BorderLayout.EAST);
}

private JPanel createBtnPanel() {
JPanel btnPanel = new JPanel();

btnPanel.setLayout(new GridLayout(0, 1));

btnPanel.add(new JButton("Button 1"));
btnPanel.add(new JButton("Button 2"));
btnPanel.add(new JButton("Long Button 3"));
btnPanel.add(new JButton("Button 4"));
btnPanel.add(new JButton("Button 5"));

// Trick here !
// I put the btnPanel at the NORTH of a "dummy" panel to have
// the correct button sizes !!!
// Other solutions ??
JPanel dummyPanel = new JPanel();
dummyPanel.setLayout(new BorderLayout());
dummyPanel.add(btnPanel, BorderLayout.NORTH);

return dummyPanel;
}
}

It works, but is there an other way to avoid the dummyPanel trick ?

Thanks a lot !

Olivier
 
Reply With Quote
 
 
 
 
Martin Gregorie
Guest
Posts: n/a
 
      11-13-2011
On Sun, 13 Nov 2011 11:30:40 +0100, Olivier Scalbert wrote:

> It works, but is there an other way to avoid the dummyPanel trick ?
>

Use a BoxLayout specifying the axis as Y-AXIS.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
 
 
 
Olivier Scalbert
Guest
Posts: n/a
 
      11-13-2011
On 11/13/2011 01:50 PM, Martin Gregorie wrote:
> Use a BoxLayout specifying the axis as Y-AXIS.
>
>

Thanks !
But with the BoxLayout they are aligned but they have not the same size!
 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      11-13-2011
On Sun, 13 Nov 2011 14:21:55 +0100, Olivier Scalbert wrote:

> On 11/13/2011 01:50 PM, Martin Gregorie wrote:
>> Use a BoxLayout specifying the axis as Y-AXIS.
>>
>>

> Thanks !
> But with the BoxLayout they are aligned but they have not the same size!
>

That's nothing to do with the layout. It aligns the objects you're laying
out by doing its best with the sizes of the objects you created.

JButton inherits size setting methods (setMaximumSize, setMinimumSize,
setPreferredSize) from JComponent. Use them to control button size.



--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-13-2011
On Sun, 13 Nov 2011 11:30:40 +0100, Olivier Scalbert
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>I want to create a swing application with a main frame and a panel
>containing vertically aligned buttons at the right side. Here is the code:


How Components align is decided by the Layout Manager. It might take
clues from fields in the Components, or it may not. Usually when you
start having trouble intimidating your LayoutManager into ever higher
levels of aesthetic perfection, it is time for a more advanced
LayoutManager, or cannibalising ideas from other users of your current
LayoutManager. It is amazing how much source code you can find with
Google with some package, class or method names.


See http://mindprod.com/jgloss/layout.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
I can't come to bed just yet. Somebody is wrong on the Internet.
 
Reply With Quote
 
Olivier Scalbert
Guest
Posts: n/a
 
      11-13-2011
On 11/13/2011 03:38 PM, Martin Gregorie wrote:
> That's nothing to do with the layout. It aligns the objects you're laying
> out by doing its best with the sizes of the objects you created.
>
> JButton inherits size setting methods (setMaximumSize, setMinimumSize,
> setPreferredSize) from JComponent. Use them to control button size.
>
>
>


Perhaps I have not understand, but with the following code, buttons are
not well aligned:

import java.awt.*;
import javax.swing.*;

public class TestViewer {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new ViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(800, 600);
frame.setVisible(true);
}
});
}
}

class ViewerFrame extends JFrame {

public ViewerFrame() {
getContentPane().add(new JPanel(), BorderLayout.CENTER);
getContentPane().add(createBtnPanel(), BorderLayout.EAST);
}

private JPanel createBtnPanel() {
JPanel btnPanel = new JPanel();

btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));

btnPanel.add(new JButton("Button 1"));
btnPanel.add(new JButton("Button 2"));
btnPanel.add(new JButton("Long Button 3"));
btnPanel.add(new JButton("Button 4"));
btnPanel.add(new JButton("Button 5"));

return btnPanel;
}
}

Olivier
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      11-13-2011
On 11/13/2011 9:28 AM, Olivier Scalbert wrote:
> On 11/13/2011 03:38 PM, Martin Gregorie wrote:
>> That's nothing to do with the layout. It aligns the objects you're laying
>> out by doing its best with the sizes of the objects you created.
>>
>> JButton inherits size setting methods (setMaximumSize, setMinimumSize,
>> setPreferredSize) from JComponent. Use them to control button size.
>>
>>
>>

>
> Perhaps I have not understand, but with the following code, buttons are
> not well aligned:
>
> import java.awt.*;
> import javax.swing.*;
>
> public class TestViewer {
>
> public static void main(String[] args) {
> EventQueue.invokeLater(new Runnable() {
> public void run() {
> JFrame frame = new ViewerFrame();
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
> frame.setSize(800, 600);
> frame.setVisible(true);
> }
> });
> }
> }
>
> class ViewerFrame extends JFrame {
>
> public ViewerFrame() {
> getContentPane().add(new JPanel(), BorderLayout.CENTER);
> getContentPane().add(createBtnPanel(), BorderLayout.EAST);
> }
>
> private JPanel createBtnPanel() {
> JPanel btnPanel = new JPanel();
>
> btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
>
> btnPanel.add(new JButton("Button 1"));
> btnPanel.add(new JButton("Button 2"));
> btnPanel.add(new JButton("Long Button 3"));
> btnPanel.add(new JButton("Button 4"));
> btnPanel.add(new JButton("Button 5"));
>
> return btnPanel;
> }
> }
>
> Olivier


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel {
public test() {
super(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;

String labels[] = {"Button 1","Button 2",
"Long Button 3","Button 4","Button 5"};

for (int i=0; i<labels.length; i++) {
add(new JButton(labels[i]),c);
++c.gridy;
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new test(),BorderLayout.EAST);
f.setSize(700,500);
f.setVisible(true);
}
});
}
}

--

Knute Johnson
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-13-2011
Olivier Scalbert wrote:
> Perhaps I have not understand, but with the following code, buttons are
> not well aligned:
>
> import java.awt.*;
> import javax.swing.*;
>
> public class TestViewer {
>
> public static void main(String[] args) {
> EventQueue.invokeLater(new Runnable() {
> public void run() {
> JFrame frame = new ViewerFrame();
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
> frame.setSize(800, 600);


Shouldn't you call 'pack()' right here? Layouts often don't work well without it.

> frame.setVisible(true);
> }
> });
> }
> }
>
> class ViewerFrame extends JFrame {
>
> public ViewerFrame() {
> getContentPane().add(new JPanel(), BorderLayout.CENTER);
> getContentPane().add(createBtnPanel(), BorderLayout.EAST);
> }
>
> private JPanel createBtnPanel() {
> JPanel btnPanel = new JPanel();
>
> btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
>
> btnPanel.add(new JButton("Button 1"));
> btnPanel.add(new JButton("Button 2"));
> btnPanel.add(new JButton("Long Button 3"));
> btnPanel.add(new JButton("Button 4"));
> btnPanel.add(new JButton("Button 5"));
>
> return btnPanel;
> }
> }


--
Lew
 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      11-13-2011
On Sun, 13 Nov 2011 18:28:44 +0100, Olivier Scalbert wrote:

> On 11/13/2011 03:38 PM, Martin Gregorie wrote:
>> That's nothing to do with the layout. It aligns the objects you're
>> laying out by doing its best with the sizes of the objects you created.
>>
>> JButton inherits size setting methods (setMaximumSize, setMinimumSize,
>> setPreferredSize) from JComponent. Use them to control button size.
>>

> Perhaps I have not understand, but with the following code, buttons are
> not well aligned:
>

They aligned as I'd expect:
- btnPanel is sized to contain the buttons and nothing else
and is placed as you asked, on the east side of the content pane.

- The buttons have their centres aligned.

I don't understand what you mean by "not well aligned":

- If you want the buttons to appear as other than a close-packed block,
take a look at "How to use BoxLayout" in the Java tutorial. It is also
referenced from the the class description in the BoxLayout javadocs
entry.

- If you want all buttons to be the same size, use a custom class in
place of JButton:

import java.awt.*;
import javax.swing.*;

class MyButton extends JButton
{

public MyButton(String label)
{
super(label);

Dimension d = new Dimension(120, 30);
setMinimumSize(d);
setMaximumSize(d);
setPreferredSize(d);
}
}


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
Olivier Scalbert
Guest
Posts: n/a
 
      11-13-2011
On 11/13/2011 08:17 PM, Martin Gregorie wrote:

> They aligned as I'd expect:
> - btnPanel is sized to contain the buttons and nothing else
> and is placed as you asked, on the east side of the content pane.
>
> - The buttons have their centres aligned.
>
> I don't understand what you mean by "not well aligned":
>
> - If you want the buttons to appear as other than a close-packed block,
> take a look at "How to use BoxLayout" in the Java tutorial. It is also
> referenced from the the class description in the BoxLayout javadocs
> entry.
>
> - If you want all buttons to be the same size, use a custom class in
> place of JButton:
>
> import java.awt.*;
> import javax.swing.*;
>
> class MyButton extends JButton
> {
>
> public MyButton(String label)
> {
> super(label);
>
> Dimension d = new Dimension(120, 30);
> setMinimumSize(d);
> setMaximumSize(d);
> setPreferredSize(d);
> }
> }
>
>


Hi,

With "my version", I have (at least on my machine):
-----------------
| button 1 |
-----------------
| button 2 |
-----------------
| Long Button 3 |
-----------------
| button 4 |
-----------------
| button 5 |
-----------------

With "yours" (at least the one I post with the BoxLayout):

------------
| button 1 |
------------
| button 2 |
-----------------
| Long Button 3 |
-----------------
| button 4 |
------------
| button 5 |
------------

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
align Swing JLabels and JTextFields vertically with albert kao Java 6 04-26-2011 05:23 PM
how can align the text vertically in a table cell atin45gupt@gmail.com HTML 1 01-17-2006 04:25 PM
vertically align absolutely positioned div elements Guybrush Threepwood HTML 1 12-14-2005 09:29 PM
div Wont Vertically Align homer HTML 6 01-28-2005 05:04 AM



Advertisments