?
Solved

Java window-like GUI application

Posted on 2009-04-15
10
Medium Priority
?
475 Views
Last Modified: 2013-11-23
A window displays with a JButton, a JLabel, and a JTextField. How can those components be lined up vertically on the window? Thanks.
0
Comment
Question by:ksfok
  • 5
  • 3
  • 2
10 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 1000 total points
ID: 24154903
use BoxLayout and BoxLayout.Y_AXIS!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24154949
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 ) ;
0
 
LVL 4

Assisted Solution

by:aman123_123
aman123_123 earned 1000 total points
ID: 24154952


import java.awt.GridLayout;
import java.awt.LayoutManager;

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
{


    static JButton smallButton = new JButton();

    static JButton mediumButton = new JButton();

    static JButton largeButton = new JButton();


    public static void main (String args[])
    {


        JFrame frame = new JFrame("Overlay Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        LayoutManager overlay = new GridLayout(3, 1);
        panel.setLayout(overlay);
        frame.add(panel);

        JButton button = new JButton("Button");
        JLabel label = new JLabel("Label", SwingConstants.CENTER);
        JTextField textField = new JTextField("Text Field");

        panel.add(button);
        panel.add(label);
        panel.add(textField);


        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ksfok
ID: 24165205
Please advise how to code the following
panel.setLayout
with the attached sample.
Thanks.

windowSample.jpg
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24165408
          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 ) ;
0
 
LVL 4

Expert Comment

by:aman123_123
ID: 24165485

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);
    }
}
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24165847
you don't have to create a separate panel for each component, its not efficient!
0
 
LVL 4

Expert Comment

by:aman123_123
ID: 24165907
Ya right, but if we don't create a panel then the controls will take up the complete space.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24165914
>>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!
0
 

Author Comment

by:ksfok
ID: 24172611
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.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question