Link to home
Start Free TrialLog in
Avatar of ksfok
ksfok

asked on

Java window-like GUI application

A window displays with a JButton, a JLabel, and a JTextField. How can those components be lined up vertically on the window? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for e.g.,

            JPanel pnlMain = new JPanel() ;
            pnlMain.setLayout( new BoxLayout( pnlMain, BoxLayout.Y_AXIS ) );
            
            pnlMain.add( new JLabel( "Label" ) ) ;
            pnlMain.add( new JTextField( "Text" ) ) ;
            pnlMain.add( new JButton( "Label" ) ) ;
            
            JFrame frame = new JFrame( "Test Box Layout" ) ;
            frame.getContentPane().add( pnlMain ) ;
            frame.pack() ;
            frame.setVisible( true ) ;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ksfok
ksfok

ASKER

Please advise how to code the following
panel.setLayout
with the attached sample.
Thanks.

windowSample.jpg
          JPanel pnlMain = new JPanel() ;
            pnlMain.setLayout( new BoxLayout( pnlMain, BoxLayout.Y_AXIS ) );
           
            pnlMain.add( new JButton( "JButton" ) ) ;
            pnlMain.add( new JLabel( "Enter Your First Name" ) ) ;
            pnlMain.add( new JTextField( "" ) ) ;
           
            JFrame frame = new JFrame( "Test Box Layout" ) ;
            frame.getContentPane().add( pnlMain ) ;
            frame.pack() ;
            frame.setVisible( true ) ;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class LayoutSample
{


    public static void main (String args[])
    {


        JFrame frame = new JFrame("Windows Asgn");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        frame.setLayout(new GridLayout());


        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JButton button = new JButton("JButton");
        JLabel label = new JLabel("Enter Your First Name Below:");
        JTextField textField = new JTextField();
        textField.setColumns(10);
        textField.setAlignmentX(SwingConstants.CENTER);
        panel.setLayout(new GridLayout(3, 1));
        panel1.add(button);
        panel2.add(label);
        panel3.add(textField);
        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);

        frame.add(panel);
        frame.setSize(250, 150);
        frame.setVisible(true);
    }
}
you don't have to create a separate panel for each component, its not efficient!
Ya right, but if we don't create a panel then the controls will take up the complete space.
>>but if we don't create a panel then the controls will take up the complete space.

that is what GridLayout meant for, you need to choose the right Layout fit for your requirement and not the other way!
Avatar of ksfok

ASKER

ksivananth,
Thanks for  your code.  But how can I move the button more toward the center, put some blank between the label and the textfield, and pad the textfield with some space around it?
Thanks again.