Avatar of Zolf
Zolf
Flag for United Arab Emirates asked on

Java Swing Layout issue

Hello there,

I have this JFrame which displays some components using GridLayout but for some reason the components are not aligned.

1
my code looks like this

class NewSerialization extends JFrame
{
    private JButton ADD;
    private JButton CANCEL;
    private JComboBox productName;
    private JTextField order;
    private JTextField batch;
    private JTextField mfg;
    
    private JPanel jpanel1;
    private JPanel jpanel2;
    private JPanel jpanel3;
    private JPanel jpanel4;
    private JPanel jpanel5;
    
    private JLabel jlabell;
    private JLabel jlabel2;
    private JLabel jlabel3;
    private JLabel jlabel4;
    private JLabel jlabel5;
    
    //Database db = new Database();
    
    public void newSerial()
    {
        System.out.println("Serialization Creation");
        jlabell = new JLabel("Enter Serialization Information");
        jlabel2 = new JLabel("Product Name");
        jlabel3 = new JLabel("Order Quantity");
        jlabel4 = new JLabel("Batch No.");
        jlabel5 = new JLabel("Mfg Date");
        
        JDatePickerImpl datePicker;
        UtilDateModel model = new UtilDateModel();
        model.setSelected(true);
        Properties p = new Properties();
		p.put("text.today", "Today");
		p.put("text.month", "Month");
		p.put("text.year", "Year");
		JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
        datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
        
        /*Date now = new Date();
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT);
        String date = df1.format(now);*/
      
        ADD = new JButton("ADD");
        CANCEL = new JButton("CANCEL"); 
        productName = new JComboBox();
        order = new JTextField();
        batch = new JTextField(25);
        mfg = new JTextField();
        
        jpanel1 = new JPanel();
        jpanel1.setLayout(new GridLayout(1,1,1,1));
        jpanel1.add(jlabell);
        
        jpanel2 = new JPanel();
        jpanel2.setLayout(new GridLayout(4,1,20,20));
        jpanel2.add(jlabel2);
        jpanel2.add(jlabel3);
        jpanel2.add(jlabel4);
        jpanel2.add(jlabel5);
        
        jpanel3 = new JPanel();
        jpanel3.setLayout(new GridLayout(4,1,20,20));
        jpanel3.add(productName);
        jpanel3.add(order);
        jpanel3.add(batch);
        jpanel3.add(datePicker);
        
        jpanel4 = new JPanel();
        jpanel4.setLayout(new FlowLayout());
        jpanel4.add(ADD);
        jpanel4.add(CANCEL);
        
        jpanel5 = new JPanel();
        jpanel5.setLayout(new FlowLayout());
        jpanel5.add(jpanel2);
        jpanel5.add(jpanel3);
                               
        Container container1 = getContentPane();
        container1.setLayout(new FlowLayout());
        
        container1.add(jpanel1);
        
        Container container2 = getContentPane();
        container2.setLayout(new FlowLayout());
        
        container2.add(jpanel5);
        
        Container container3 = getContentPane();
        container3.setLayout(new FlowLayout());
        
        container3.add(jpanel4);
        
        setTitle("Serialization Creation");
        //setBounds(200,100,370,250);
        setBounds(200,100,450,300);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Open in new window

JavaJava EE

Avatar of undefined
Last Comment
zzynx

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
zzynx

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Zolf

ASKER
Thanjs
zzynx

You're welcome.
Thanx 4 axxepting
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23