Nicolas
Nicolas

Reputation: 11

Java JCheckBox ActionListener

I have a program that is a list of notes that can be added by the user. Every note is loaded through a for loop and given a JCheckBox on runtime from the notes.txt. How do I add an actionlistener for my JCheckBoxes when they are only instance based and not permanent code?

(e.g. they don't have their own variable names?)

I need to update notes.txt with a 0 or a 1 based on if the JCheckBox is checked or not. Here is my code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class SideNotes {

public static JPanel panel = new JPanel();
private static JButton add = new JButton("Add note");
JCheckBox[] notes;

public SideNotes() {
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(add);
    loadNotes();
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                addNote();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
}

public void addNote() throws IOException {
    String note = JOptionPane.showInputDialog("Enter note: ", null);
    JCheckBox jcb = new JCheckBox(note, false);
    panel.add(jcb);
    panel.revalidate();
    panel.repaint();

    File file = new File("notes.txt");
    FileWriter writer = new FileWriter(file, true);
    BufferedWriter brw = new BufferedWriter(writer);

    brw.write(note);
    brw.newLine();
    brw.close();
}

private void loadNotes() {
    File file = new File("notes.txt");
    if (file.exists()) {
        try {
            FileInputStream fs = new FileInputStream(file);
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(fs));

            BufferedReader reader = new BufferedReader(new FileReader(
                    "notes.txt"));
            int lines = 0;
            while (reader.readLine() != null)
                lines++;
            reader.close();
            notes = new JCheckBox[lines];

            for (int i = 0; i < notes.length; i++) {
                String note = br.readLine();
                int checked = note.charAt(note.length() - 1);
                System.out.println(checked);
                if (checked == 49) {
                    note = note.substring(0, note.length() - 1);
                    notes[i] = new JCheckBox(note, true);   
                } else {
                    note = note.substring(0, note.length() - 1);
                    notes[i] = new JCheckBox(note, false);
                }
                panel.add(notes[i]);
                panel.revalidate();
                panel.repaint();
            }
            br.close();

        } catch (Exception e1) {
        }
    } else {
        System.out.println("File does not exist");
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(200, 400);
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(add);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    new SideNotes();
}

}

Upvotes: 0

Views: 1875

Answers (1)

camickr
camickr

Reputation: 324108

Create a generic ActionListener outside the loop where you create the checkboxes:

ActionListener al = new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        JCheckBox checkbox = (JCheckBox)e.getSource();
        // do something with the checkbox
    }
}

Then after you create the checkbox you add the ActionListener to the checkbox

notes[i].addActionListener(al);
panel.add(notes[i]);

Upvotes: 1

Related Questions