3

I am trying to do an action on button being clicked, but i need to make a check whether JCheckBox is checked or not.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;

import javax.swing.*;

public class RandomPassword extends JFrame{
RandomPassword(String s){
    super(s);
    setSize(300,300);
    setVisible(true);
    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent ev){
            System.exit(0);

        }
    });
    setLayout(null);
    setFont(new Font("Serif", Font.PLAIN, 20));
    Label l1 = new Label("Введите количество символов:");
    l1.setBounds(50, 100, 200, 30);
    add(l1);
    JTextField tf1 = new JTextField(1002);
    tf1.setBounds(50,130,200,30);
    add(tf1);
    JTextArea ta1 = new JTextArea();
    ta1.setPreferredSize(new Dimension(150,30));
    ta1.setBounds(50,210,230,30);
    add(ta1);
    JCheckBox ch1 = new JCheckBox("Использовать заглавные буквы");
    ch1.setBounds(50, 0, 200, 30);
    add(ch1);
    JCheckBox ch2 = new JCheckBox("Использовать цифры");
    ch2.setBounds(50, 30, 200, 30);
    add(ch2);
    JCheckBox ch3 = new JCheckBox("Использовать спецсимволы");
    ch3.setBounds(50, 60, 200, 30);
    add(ch3);
    JButton b1 = new JButton("Сгенерировать");
    b1.setBounds(75, 170, 150, 30);
    add(b1);
    b1.addActionListener(new Action());


}


public static void main(String[] args){
    new RandomPassword("Генератор случайных паролей");


}
static  class Action implements ActionListener{
        public void actionPerformed(ActionEvent e){



        }
}

}

I want to make a Checkbox in static class Action, but he is throwing me an exception. What do i have to do?
Trying this one didn`t help me.

JButton b1 = new JButton(new AbstractAction("Сгенерировать") {

        public void actionPerformed(ActionEvent e) {
            ch1.isSelected();
        }
    }); 
Akado2009
  • 381
  • 1
  • 4
  • 11
  • Could you be a bit more specific? What kind of exception do you get? The problem seems to be that those check boxes you create are not in the scopre of the action listener. Try using an inner class of the constructor. – tobias_k Apr 24 '14 at 17:51
  • Yes, i guess its the problem of scope of the action listener, could you help me to write an innter class? – Akado2009 Apr 24 '14 at 17:53
  • Finally i get it, have to use final with ch1, ch2 and ch3. Ty for help about inner class – Akado2009 Apr 24 '14 at 17:58
  • If this helped you, feel free to accept an answer to mark this question as being resolved. – tobias_k Apr 24 '14 at 18:00

2 Answers2

0

You can access to the cliked JCheckBox with:

((JCheckBox)e.getSource())
PhilippeVienne
  • 550
  • 8
  • 19
0

The way your program is structured your check boxes are not in the scope of the actionPerformed method. One way to counter this would be to use an anonymous inner class directly in the contructor.

final JCheckBox ch3 = new JCheckBox("Использовать спецсимволы");
...
b1.addActionListener(new  ActionListener(){
    public void actionPerformed(ActionEvent e){
        System.out.println(ch3.isSelected());
    }});

Note that this way you need to make the check box variable final, so it can be accessed in the inner class. You can then use the isSelected method to check whether the check box is currently selected.

As an unrelated note, better put the call to setVisible(true) at the end of the constructor, otherwise it seems like some GUI elements are not drawn correctly.

tobias_k
  • 81,265
  • 12
  • 120
  • 179