Reputation: 105
I have some problem with my code!.... I was able to create an array of 12 JButtons in a circular form in a JPanel with the code below!... I am setting an actionListener on each JButton, and I want to disable others once one of the Jbuttons is clicked.... and enable it afterwards..... For more understanding, here is my code
int n = 10;
public Beginner() {
int radius = 200;
Point center = new Point (250, 250);
double angle = Math.toRadians(360 / n);
List <Point> points = new ArrayList<Point> ();
points.add(center);
for (int i = 0; i < n; i++) {
double theta = i * angle;
int dx = (int) (radius * Math.sin(theta));
int dy = (int) (radius * Math.cos(theta));
Point p = new Point (center.x + dx , center.y + dy);
points.add(p);
}
draw (points);
}
public void draw (List<Point> points) {
JPanel panels = new JPanel();
SpringLayout spring = new SpringLayout();
int count = 1;
for (Point point: points) {
quest = new JButton("Question " + count);
quest.setForeground(Color.BLACK);
Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
quest.setFont(fonte);
add (quest);
count++;
spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels );
spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels );
setLayout(spring);
panels.setOpaque(false);
panels.setVisible(true);
panels.setLocation(5,5);
add(panels);
quest.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent q) {
if (point.equals(points.get(0))) {
//Some action....
//It is at this point that every other Jbutton in the panel is to be disabled until the action ends..... It is here that I need help!!!
Upvotes: 0
Views: 290
Reputation: 3880
try this solution:
quest.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
for(Component c : yourPanel.getComponents()){
if(c instanceof JButton && !c.equals(b)){
c.setEnabled(false);
}
}
}
});
Upvotes: 2