Re: Question: jFranme and jDialog



WhoWhatWhen wrote:
Given a jFrame that also contains a checkbox and a jDialog.

The jDialog has been made visible and is modal.

Is there a way to access the value of the checkbox on the parent
jFrame from the jdialog?


There are many many ways


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FrameDialog implements ItemListener {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FrameDialog();
}
});
}

private JCheckBox checkbox;

private JFrame frame;

FrameDialog() {
checkbox = new JCheckBox("Fries", false);
checkbox.addItemListener(this);
JPanel p = new JPanel();
p.add(checkbox);

frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(p);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} // constructor

public void itemStateChanged(ItemEvent e) {

final JDialog d = new JDialog(frame, "Dialog");

JButton b = new JButton("True");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
d.dispose();
}
});

JPanel p = new JPanel();
p.add(new JLabel(checkbox.isSelected() ? "Fatso" : "Beanpole"));
p.add(b);

d.add(p);
d.pack();
d.setLocationRelativeTo(frame);
d.setVisible(true);

}

}
.