How to create a button and locate it on a panel based on user input

660 Views Asked by At

In first frame, I have a panel with form layout, I have a "ADD" button. When user click ADD button, a second frame will shown. Second frame is shown to allow user to fill the information needed to create a button in A.java.

ADD button on frame of A.java:

JButton button_3 = new JButton("ADD");//ADD ROW
    button_3.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            addCHECKUP addC = new addCHECKUP();//note that addCHECKUP is B.java
            addC.setVisible(true);
            addC.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        }

    });

frame of A.java:

enter image description here

If user click ADD button at frame of A.java, frame of B.java will be shown.

frame of B.java:

enter image description here

After user finish in giving all the input at frame of B.java, ADD button will be clicked. Then a new button is created at frame of A.java based on user input at frame of B.java.

So far, when ADD button at frame of B.java is clicked, the information is saved into a text file. Below is the ADD button code.

JButton btnAdd = new JButton("ADD");
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            System.out.println(comboBox_1.getSelectedItem());   //getActivity

             if (chckbxNewCheckBox.isSelected()){//if complication
                System.out.println("*"+textField.getText());//setTaskName have * sign
                }
            else{
                System.out.println(textField.getText());    
                }
            System.out.println(comboBox.getSelectedItem());  //getDay

              String filename = "c:" + File.separator + "Text File from B.java Class.txt";
                File f = new File(filename);
                PrintWriter pw = null;
                try {

                    pw = new PrintWriter(f);                
                    pw.println(comboBox_1.getSelectedItem());
                    if (chckbxNewCheckBox.isSelected()){//if complication
                        pw.println("*"+textField.getText());

                        }
                    else{
                        pw.println(textField.getText());

                        }
                    pw.println(comboBox.getSelectedItem());
                    pw.flush();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }finally{

                    if(pw!=null){
                        pw.close();
                    }

            }

            JOptionPane.showMessageDialog(null,"Successfully added !");
        }
    }); 

Now, I intend to access the text file and create a new button based on the information from text file. I don't know how to make the new button based on the information from the text file. If there is any other method that more easier, please let me know.

I want to create button like this: I put this at B.java

JButton btn_34 = new JButton("Uptitrate BB");//Task Name
BigPanel.add(btn_34, "12, 11");  //12 and 11 is based on Activity and Day

But error in BigPanel.add

1

There are 1 best solutions below

6
Christian On

I would use a Properties read and write this.

So all you need are keys and values.

final Properties prop = new Properties();
prop.setProperty("Activity", comboBox_1.getSelectedItem());
prop.setProperty("Complication", chckbxNewCheckBox.isSelected());
prop.setProperty("Day", comboBox.getSelectedItem());
writePropertyIntoFile("c:" + File.separator + "Text File from B.java   Class.txt", prop);

And your write method may look like this

private static void writePropertyIntoFile(String file, final Properties prop)
        throws FileNotFoundException, IOException {
    try (final FileOutputStream f = new FileOutputStream(file);) {
        prop.store(f, null);
    }
}

Sincerely

Update-1

I assume I have a property File containing

B1.Day,1
B1.Complication,true
B1.Activity,Diagnose
B2.Day,2
B2.Complication,false
B2.Activity,Labs
B3.Day,5
B3.Complication,false
B3.Activity,Oxygon

Now, I only need to loop over all Bs an call createButton for each containing in this property file. All I may save is the last used index. So if the user adds a new one. We create a new Button Bn. Save this in the file and you are done.

Update-2

JButton button_3 = new JButton("ADD");//ADD ROW
button_3.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        String btName = getNextFreeButtonName();
        Button btnX = createNewButton("Oxy", "11","10", btName);
        mapContainingAllButtons.put(btName, btnX)
        addButtonToFram(btnX);
    }
});