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.