I have a JPanel with BorderLayout.
I want to have a label (icon) in the west area, and a row of buttons
in the center.
When the user stretches the component vertically, the icon remains
centered vertically, but the button row stays pegged to the top of the
center area. How can I get the buttons to remain centered vertically?
I am placing a JPanel in the center, with flow layout. Then I add the
buttons to that center panel:
JPanel centerPanel = new JPanel();
centerPanel.setAlignmentY( Component.CENTER_ALIGNMENT );
centerPanel.setAlignmentX( Component.CENTER_ALIGNMENT );
// ... add buttons to centerPanel
JLabel Label= new JlLabel();
Label.setIcon(myIcon);
JPanel p = new JPanel( new BorderLayout() );
p.setAlignmentX( Component.CENTER_ALIGNMENT );
p.setAlignmentY( Component.CENTER_ALIGNMENT );
p.add( centerPanel, BorderLayout.CENTER );
p.add( Label, BorderLayout.WEST );
--
Fred K
|
|
0
|
|
|
|
Reply
|
Fred
|
9/30/2010 7:41:01 PM |
|
On 9/30/2010 12:41 PM, Fred wrote:
> I have a JPanel with BorderLayout.
> I want to have a label (icon) in the west area, and a row of buttons
> in the center.
> When the user stretches the component vertically, the icon remains
> centered vertically, but the button row stays pegged to the top of the
> center area. How can I get the buttons to remain centered vertically?
>
> I am placing a JPanel in the center, with flow layout. Then I add the
> buttons to that center panel:
>
> JPanel centerPanel = new JPanel();
> centerPanel.setAlignmentY( Component.CENTER_ALIGNMENT );
> centerPanel.setAlignmentX( Component.CENTER_ALIGNMENT );
> // ... add buttons to centerPanel
>
>
> JLabel Label= new JlLabel();
> Label.setIcon(myIcon);
>
> JPanel p = new JPanel( new BorderLayout() );
> p.setAlignmentX( Component.CENTER_ALIGNMENT );
> p.setAlignmentY( Component.CENTER_ALIGNMENT );
> p.add( centerPanel, BorderLayout.CENTER );
> p.add( Label, BorderLayout.WEST );
>
> --
> Fred K
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.gridx = c.gridy = 0;
JButton b1 = new JButton("One");
JButton b2 = new JButton("Two");
JButton b3 = new JButton("Three");
JButton b4 = new JButton("Four");
add(b1,c);
++c.gridy;
add(b2,c);
++c.gridy;
add(b3,c);
++c.gridy;
add(b4,c);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t,BorderLayout.CENTER);
JLabel l = new JLabel("Label");
f.add(l,BorderLayout.WEST);
f.pack();
f.setVisible(true);
}
});
}
}
--
Knute Johnson
email s/nospam/knute2010/
|
|
0
|
|
|
|
Reply
|
Knute
|
9/30/2010 8:34:15 PM
|
|
On 9/30/2010 12:41 PM, Fred wrote:
> How can I get the buttons to remain centered vertically?
>
> I am placing a JPanel in the center, with flow layout. Then I add the
> buttons to that center panel:
>
> JPanel centerPanel = new JPanel();
> centerPanel.setAlignmentY( Component.CENTER_ALIGNMENT );
You can use a Box layout to do this, and insert stretchy "glue" bits
before and after the component to get a centered effect.
In the example below, I make two Boxes. One is a vertical layout, and
holds the stretchy glue bits. It has just one component, which is the
entire row of buttons.
Then the row of buttons itself is just a box with a horizontal
configuration (and no glue).
For more one Box and glue, see the Swing tutorial:
<http://download.oracle.com/javase/tutorial/uiswing/layout/box.html>
package test;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BorderLayoutTest {
public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable() {
public void run()
{
createAndShowGui();
}
} );
}
private static void createAndShowGui()
{
JFrame frame = new JFrame();
Box centerPanel = Box.createVerticalBox();
Box buttonRow = Box.createHorizontalBox();
buttonRow.add( new JButton( "Button 1" ) );
buttonRow.add( new JButton( "Button 2" ) );
buttonRow.add( new JButton( "Button 3" ) );
buttonRow.add( new JButton( "Button 4" ) );
centerPanel.add( Box.createVerticalGlue() );
centerPanel.add( buttonRow );
centerPanel.add( Box.createVerticalGlue() );
frame.add( centerPanel );
frame.add( new JLabel( "West" ), BorderLayout.WEST );
frame.pack();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
|
|
0
|
|
|
|
Reply
|
markspace
|
9/30/2010 11:04:47 PM
|
|
On Sep 30, 4:04=A0pm, markspace <nos...@nowhere.com> wrote:
> On 9/30/2010 12:41 PM, Fred wrote:
>
> > How can I get the buttons to remain centered vertically?
>
> > I am placing a JPanel in the center, with flow layout. Then I add the
> > buttons to that center panel:
>
> > =A0 =A0 =A0 =A0 =A0 JPanel centerPanel =3D new JPanel();
> > =A0 =A0 =A0 =A0 =A0 centerPanel.setAlignmentY( Component.CENTER_ALIGNME=
NT );
>
> You can use a Box layout to do this, and insert stretchy "glue" bits
> before and after the component to get a centered effect.
>
> In the example below, I make two Boxes. =A0One is a vertical layout, and
> holds the stretchy glue bits. =A0It has just one component, which is the
> entire row of buttons.
>
> Then the row of buttons itself is just a box with a horizontal
> configuration (and no glue).
>
> For more one Box and glue, see the Swing tutorial:
>
> <http://download.oracle.com/javase/tutorial/uiswing/layout/box.html>
>
> package test;
>
> import java.awt.BorderLayout;
> import java.awt.Component;
> import javax.swing.Box;
> import javax.swing.JButton;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.SwingUtilities;
>
> public class BorderLayoutTest {
>
> =A0 =A0 public static void main( String[] args )
> =A0 =A0 {
> =A0 =A0 =A0 =A0SwingUtilities.invokeLater( new Runnable() {
>
> =A0 =A0 =A0 =A0 =A0 public void run()
> =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0createAndShowGui();
> =A0 =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0} );
> =A0 =A0 }
>
> =A0 =A0 private static void createAndShowGui()
> =A0 =A0 {
> =A0 =A0 =A0 =A0JFrame frame =3D new JFrame();
> =A0 =A0 =A0 =A0Box centerPanel =3D Box.createVerticalBox();
> =A0 =A0 =A0 =A0Box buttonRow =3D Box.createHorizontalBox();
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 1" ) );
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 2" ) );
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 3" ) );
> =A0 =A0 =A0 =A0buttonRow.add( new JButton( "Button 4" ) );
>
> =A0 =A0 =A0 =A0centerPanel.add( Box.createVerticalGlue() );
> =A0 =A0 =A0 =A0centerPanel.add( buttonRow );
> =A0 =A0 =A0 =A0centerPanel.add( Box.createVerticalGlue() );
> =A0 =A0 =A0 =A0frame.add( =A0centerPanel );
> =A0 =A0 =A0 =A0frame.add( new JLabel( "West" ), BorderLayout.WEST );
>
> =A0 =A0 =A0 =A0frame.pack();
> =A0 =A0 =A0 =A0frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
> =A0 =A0 =A0 =A0frame.setLocationRelativeTo( null );
> =A0 =A0 =A0 =A0frame.setVisible( true );
> =A0 =A0 }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
That works great. It also works better than other things I tried when
you add elements - it resizes properly on revalidate().
--
Fred K
|
|
0
|
|
|
|
Reply
|
Fred
|
10/5/2010 6:22:56 PM
|
|
|
3 Replies
303 Views
(page loaded in 0.068 seconds)
Similiar Articles: Labeled button row using BorderLayout - comp.lang.java.gui ...I have a JPanel with BorderLayout. I want to have a label (icon) in the west area, and a row of buttons in the center. When the user stretches the ... Error Using cardLayout.show with Java 1.4 - comp.lang.java.gui ...Labeled button row using BorderLayout - comp.lang.java.gui ..... You can use a Box layout to ... Show quoted text ... Java 2 Platform SE v1.4.2) Here is an example of ... how can i get the text of entry in TK? - comp.lang.rubyLabeled button row using BorderLayout - comp.lang.java.gui ... how can i get the text of entry in TK? - comp.lang.ruby... input the value "test" in the entry,but click the ... Spin button inside group box - comp.compilers.lccLabeled button row using BorderLayout - comp.lang.java.gui ... Spin button inside group box - comp.compilers.lcc Radio Buttons Don't use radio button labels as group box ... JScrollPane and FlowLayout - comp.lang.java.guiIf I add the JScrollPane to the JPanel using BorderLayout ... It looks really nice. When the buttons reach the right side of the panel they start out on a new row ... add row names to JTable + multiple Jtable instances - comp.lang ...Iam wondering if there is a way to add row ... SCROLLBAR_ALWAYS); add(scrollPane, BorderLayout ... databases.filemaker... the names, then a button on teh portal row ... JPanel in JTable cell - comp.lang.java.gui//we create a File selecter cell editor //this has a label & a button, when user wants to ... java.gui JTable and input methods (or, and: focus in ... Make a simple 4 row ... Replacing an existing jPanel with a new jPanel - comp.lang.java ...... project and basically I've developed a swing GUI using ... Also on this form, I have a button that when pressed ... add(b,BorderLayout.NORTH); b = new JButton ... Two x axis and two y axis on imagesc - comp.soft-sys.matlab ...... vectorX = X(1:column); vectorY = zeros(row,1 ... left - comp.soft-sys.matlab ... tclsh, using left-right buttons ... ... So, the graph would only show the points & labels ... Make a simple 4 row by 4 column scratch pad. - comp.lang.java.help ...... uses JToggleButton: > > <http://sites.google.com/site/drjohnbmatthews/buttons ... left pad a column - comp.databases.mysql... with row, column labels + alignment? - comp ... Labeled button row using BorderLayout - comp.lang.java.gui ...I have a JPanel with BorderLayout. I want to have a label (icon) in the west area, and a row of buttons in the center. When the user stretches the ... How to Use BorderLayout (The Java™ Tutorials > Creating a GUI ...The following figure represents a snapshot of an application that uses the BorderLayout class. Click the Launch button to run BorderLayoutDemo using Java™ Web Start ... 7/24/2012 12:25:05 AM
|