A component can have two parent?

  • Thread starter andric(www.oksbt.com)
  • Start date
A

andric(www.oksbt.com)

in fact, i wish a JCheckBox displayed in two Container, and they have
same select status.

thanks in advance.

andric.
 
K

Knute Johnson

andric(www.oksbt.com) said:
in fact, i wish a JCheckBox displayed in two Container, and they have
same select status.

thanks in advance.

andric.

No.

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

public class test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JCheckBox cb1 = new JCheckBox("First");
final JCheckBox cb2 = new JCheckBox("Second");

cb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cb2.setSelected(cb1.isSelected());
}
});

cb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cb1.setSelected(cb2.isSelected());
}
});

f.add(cb1,BorderLayout.NORTH);
f.add(cb2,BorderLayout.SOUTH);

f.pack();
f.setVisible(true);
}
});
}
}
 
D

Daniele Futtorovic

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Action a = new AbstractAction("Bite me!"){
public void actionPerformed(ActionEvent e){
System.out.println("actionPerformed");
}
};

a.putValue(Action.SELECTED_KEY, new Object());

final JCheckBox cb1 = new JCheckBox(a);
final JCheckBox cb2 = new JCheckBox(a);

f.add(cb1,BorderLayout.NORTH);
f.add(cb2,BorderLayout.SOUTH);

f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}


@see doc:
<http://java.sun.com/javase/6/docs/api/javax/swing/Action.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top