Java Swing

I am looking for sample code in swing, frame displays
4 rows, 2 columnts.
1 -  name - text field
2 - password - password field
3 - domain - dropdown control - first, second, thrid
4 -  buttons OK, CANCLE

ACTION FOR CLOSING THE FRAME.

size frame to have 2 columns.

Thanks
vittalmareddyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ajaycvCommented:
HonorGodSoftware EngineerCommented:
I know it's not complete, but this is a start...
import java.awt.*;
import javax.swing.*;

class Demo {

  Demo() {

    // Create a JFrame container, and provide a title
    JFrame frame = new JFrame( "Q_26572459" );

    // Create the grid bag
    GridBagLayout gbag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();

    // Set the grid bag as the layout manager for the the content pane.
    frame.getContentPane().setLayout( gbag );

    // Give the frame an initial size.
    frame.setSize( 240, 240 );

    // Terminate the program when the user closes the application.
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    // Make the labels
    JLabel lName   = new JLabel( "Name:"      );
    JLabel lPwd    = new JLabel( "Password: " );
    JLabel lDomain = new JLabel( "Domain: "   );

    // Make the text input fields
    JTextField tName   = new JTextField( 10 );
    JTextField tPwd    = new JTextField( 10 );
    JTextField tDomain = new JTextField( 10 );

    // Make the buttons.
    JButton bOK     = new JButton( "OK" );
    JButton bCancel = new JButton( "Cancel" );

    Dimension btnDim = new Dimension( 100, 25 );
    bOK.setPreferredSize( btnDim );
    bCancel.setPreferredSize( btnDim );

    // Define the grid bag.

    // By using a weightx of 1.0, the components divide the
    // horizontal space. However, because weighty uses its
    // default value of 0.0, the components remain centered
    // in the vertical space.
    gbc.weightx = 1.0;

    // Define the grid location for each component.

    // Place the labels and data fields
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbag.setConstraints( lName, gbc );

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbag.setConstraints( tName, gbc );

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbag.setConstraints( lPwd, gbc );
  
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbag.setConstraints( tPwd, gbc );

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbag.setConstraints( lDomain, gbc );
  
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbag.setConstraints( tDomain, gbc );


    // Add some space around the buttons.
    gbc.insets = new Insets( 4, 4, 4, 4 );

    // Put the buttons at locations 0,3 and 1,3
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbag.setConstraints( bOK, gbc );

    gbc.gridx = 1;
    gbc.gridy = 3;
    gbag.setConstraints( bCancel, gbc );

    // Have the last label and the two check boxes
    // use the remaining space.
    gbc.gridwidth = GridBagConstraints.REMAINDER;

    // Add everything to the content pane.
    frame.getContentPane().add( lName );
    frame.getContentPane().add( tName );
    frame.getContentPane().add( lPwd );
    frame.getContentPane().add( tPwd );
    frame.getContentPane().add( lDomain );
    frame.getContentPane().add( tDomain );
    frame.getContentPane().add( bOK );
    frame.getContentPane().add( bCancel );

    // Display the frame.
    frame.setVisible( true );
  }

  public static void main( String args[] ) {
    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater( new Runnable() {
      public void run() {
        new Demo();
      }
    } );
  }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HonorGodSoftware EngineerCommented:
Wow, thanks for the grade & points.

Good luck & have a great day.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.