0

I have two Components in my GUI, one JButton and the other one is JCheckBox. On Button Click event, a new JFrame will be created, having same two Components.

So Coming to my Question :

How to Assign a Unique ID to a dynamically created JCheckBox in dynamically created JFrame ?

Here is my Code.

    


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

    public class HandlingDynamicJCheckBoxes extends JFrame implements ActionListener{

        public JFrame frame;
        public Container container;
        public JPanel panel;
        public JCheckBox chkBox;
        public JButton createThreads;

        public HandlingDynamicJCheckBoxes(){

            init();    
        }

        public void init(){

            frame = new JFrame("Dynamic JCheckBoxes");
            container = frame.getContentPane();
            frame.setLayout(new FlowLayout());
            chkBox = new JCheckBox("Mute", true);
            createThreads = new JButton("Create Threads");
            panel = new JPanel();
            panel.setLayout(new BorderLayout());
            panel.add(chkBox,BorderLayout.CENTER);
            panel.add(createThreads,BorderLayout.SOUTH);
            container.add(panel);

            chkBox.addActionListener(this);
            createThreads.addActionListener(this);

            frame.setSize(300,300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

        }

        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable(){

                @Override
                public void run() {

                    HandlingDynamicJCheckBoxes obj = new HandlingDynamicJCheckBoxes();

                }      
            });     
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            if(e.getActionCommand() == "Create Threads"){

                Thread thread = new Thread(){

                    HandlingDynamicJCheckBoxes obj = new HandlingDynamicJCheckBoxes();

                };
                thread.start();

            }

            else if(e.getActionCommand() == "Mute"){

                JOptionPane.showMessageDialog(null, "Hello");

            }      
        }      
    }


Now If JFrame1 JcheckBox is Clicked so Message will display

 JOptionPane.showMessageDialog(null,"JFrame1 Mute 1 is clicked") 

Now If JFrame2 JcheckBox is Clicked so MessageBox will display

 JOptionPane.showMessageDialog(null,"JFrame2 Mute 2 is clicked") 

and so on to Manage JCheckBoxes


I have google a lot about this particular scenario and search in stackoverflow too, to find if any previous asked question satisfied my need but i have not found anything helpful.

There was already a question asked on stackoverflow, link provided below but that was not gui base.

previous question

Community
  • 1
  • 1
noman
  • 37
  • 2
  • 14
  • 1
    *"..a new JFrame will be created.."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Feb 19 '16 at 08:57
  • Yes somehow the use of Multiple JFrames is not good practice but in some scenarios it best suits to your application. – noman Feb 19 '16 at 10:14

1 Answers1

0

Maybe the following will work:

// Add a 'global' variable within your frame
private static int last_generated_id = 0;

// Add a method that generates and returns a new unique ID
private static synchronized int createUniqueID()
{
    last_generated_id++;
    return (last_generated_id);
}
Robert Kock
  • 5,795
  • 1
  • 12
  • 20
  • yes It worked :) adding the above code in my program with just modifying one line of code in my program `JOptionPane.showMessageDialog(null, "Mute"+createUniqueId())` – noman Feb 19 '16 at 08:13