Search code examples
javajoptionpane

I'm trying to use JOptionPane to first make a choice of its either a Person or a Firm, then move on to input


I want to add a Person/Firm to a type of Vehicle. The point is to make a choice between a person or a Firm, and then move on to input the details about them. Then move on to making a choice of what kind of type of vehicle it is and then more details. I don't know what I have to do, to get the program working correctly, I hope anyone can help me on my way.

private void addVehicle() {
    System.out.println("Add Vehicle");
    String[] options = {"Private person", "Firm"};
    String[] vehicle = {"Car", "Truck", "MC"};
    String[] personDetails = {"Firstname: ","Lastname: ","Date of Birth: ",
                              "Address: ","Phone Number: "};

    int chooseOwnerType = JOptionPane.showOptionDialog(this, "Private person/firm",
        "Choose an option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "");
    int chooseVehicleType = JOptionPane.showOptionDialog(this, "What kind of vehicle is it?",
        "Choose an option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, vehicle, "");
    int numPairs = personDetails.length; 

    if(chooseOwnerType == 0) {
        JPanel p = new JPanel(new SpringLayout());
        for (int i = 0; i < numPairs; i++) {
            JLabel l = new JLabel(personDetails[i], JLabel.TRAILING);
            p.add(l);
            JTextField textField = new JTextField(10);
            l.setLabelFor(textField);
            p.add(textField);
        }
    }

    if(chooseOwnerType == 1) {
    }

Solution

  • There are some things you need to use for the specific example. First off, I would suggest you read on how to use the SpringLayout as you need to specify "Springs" in order for the textbox to display correctly (see: http://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html )

    "Obviously, we have some problems. Not only does the frame come up way too small, but even when it is resized the components are all located at (0,0). This happens because we have set no springs specifying the components' positions and the width of the container. One small consolation is that at least the components are at their preferred sizes — we get that for free from the default springs created by SpringLayout for each component."

    Personally, I would use a grid layout with two columns. So, based on your initial post I made changes and came up with the following code. I would strongly suggest you read some swing and java tutorials.

    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SpringLayout;
    
    public class Test1 {
    
        public static void main(String args[]) {
            new Test1().addVehicle();
        }
    
        private void addVehicle() {
            System.out.println("Add Vehicle");
            String[] options = { "Private person", "Firm" };
            String[] vehicle = { "Car", "Truck", "MC" };
            String[] personDetails = { "Firstname: ", "Lastname: ",
                    "Date of Birth: ", "Address: ", "Phone Number: " };
    
            int chooseOwnerType = JOptionPane.showOptionDialog(null,
                    "Private person/firm", "Choose an option",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    options, "");
            int chooseVehicleType = JOptionPane.showOptionDialog(null,
                    "What kind of vehicle is it?", "Choose and option",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    vehicle, "");
            int numPairs = personDetails.length;
    
            System.out.println("Owner Type:" + chooseOwnerType);
            System.out.println("Vehicle Type:" + chooseVehicleType);
            JPanel p = null;
    
            GridLayout gridLayout = new GridLayout(0,2);
    
                //Private Person
            if (chooseOwnerType == 0) {
                p = new JPanel(gridLayout);
                for (int i = 0; i < numPairs; i++) {
                    JLabel l = new JLabel(personDetails[i], JLabel.TRAILING);
                    p.add(l);
                    JTextField textField = new JTextField(10);
                    l.setLabelFor(textField);
                    p.add(textField);
                }
            }
    
            if (chooseOwnerType == 1) {
            }
    
            if (p != null) {
                JFrame window = new JFrame("GUI Test");
                window.setContentPane(p);
                window.setSize(250, 100);
                window.setLocation(100, 100);
                window.setVisible(true);
                window.pack();
            }
    
        }
    
    }
    

    And below the custom resulting JFrame when user selects first button for owner. enter image description here