Home > Mobile >  Java Swing not showing components as it should
Java Swing not showing components as it should

Time:05-27

I am trying to code the GUI for a class in Java and some components appear but some don't. Precisely JTextBoxes and JButton appear but JLabels and JComboBoxes don't. Another problem is that I tried to remove the JComboBox and put JTextBox, but when I tried to type a blood type (es. O-) and register it in the object Donor d1, it gave me NumberFormatException. I checked Donor class and the attribute bloodType is a String so I don't see why would it give me this error

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DonationForm {
    DonorTableImpl donorDB;

    public DonationForm(DonorTableImpl donorDB){
        this.donorDB=donorDB;
        JFrame formFrame=new JFrame("Complete the form");

        //TextBox Fields
        JTextField id=new JTextField(10);
        id.setBounds(140,190,150,30);
        JTextField firstName=new JTextField(10);
        firstName.setBounds(140,160,150,30);
        JTextField lastName=new JTextField(10);
        lastName.setBounds(140,130,150,30);
        JTextField password=new JTextField(10);
        password.setBounds(140,100,150,30);
        JTextField address=new JTextField(10);
        address.setBounds(140,70,150,30);
        
        //Labels for TextBoxes
        JLabel idLabel=new JLabel("Id");
        JLabel firstNameLabel=new JLabel("First Name");
        JLabel lastNameLabel=new JLabel("Last Name");
        JLabel passwordLabel=new JLabel("Password");
        JLabel addressLabel=new JLabel("Address");
        JLabel bloodTypeLabel=new JLabel("Blood Type");

        //ComboBox
        String[] blood={"A ","A-","B ","B-","AB ","AB-","O ","O-"};
        JComboBox bloodType=new JComboBox(blood);
        bloodType.setSelectedIndex(0);

        //Submit button
        JButton submit=new JButton("SUBMIT");
        submit.setBounds(190,300,100,30);

        //Add all components on frame
        formFrame.add(idLabel);
        formFrame.add(id);
        formFrame.add(firstNameLabel);
        formFrame.add(firstName);
        formFrame.add(lastNameLabel);
        formFrame.add(lastName);
        formFrame.add(passwordLabel);
        formFrame.add(password);
        formFrame.add(addressLabel);
        formFrame.add(address);
        formFrame.add(bloodTypeLabel);
        formFrame.add(bloodType);
        formFrame.add(submit);
        formFrame.setSize(500,500);
        bloodType.setVisible(true);
        bloodType.setLayout(null);
        formFrame.setLayout(null);
        formFrame.setVisible(true);

        //Create an Object with data gotten from TextBoxes when SUBMIT button is clicked
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Donor d1=new Donor(Integer.parseInt(id.getText()),firstName.getText(),
                        lastName.getText(),password.getText(),
                        address.getText(), blood[bloodType.getSelectedIndex()]);
                donorDB.save(d1);
            }
        });

    }
}

CodePudding user response:

You need to set the frame's layout to something besides null in order to render all components.

Instead of

formFrame.setLayout(null);

Use this:

formFrame.setLayout(new FlowLayout(FlowLayout.CENTER, 1000, 10));

CodePudding user response:

By using formFrame.setLayout(null); you chose to not use a enter image description here

I recommend to learn more about how to layout components in Swing. See for example this tutorial. With the skills from there you may even want to further improve the appearance of your JFrame.

  • Related