Frage

I have a JPanel, which contains a JPanel and a JButtons. in the inner JPanel I have some JCheckBoxes which are initially selected. I have added an actionListener to my JButtons that check, if each of the JCheckBoxes changed to unselected an action performs. but It doesn't work. this is my code, where would the problem be?

public LimitPanel(String[] dates, int column) {

    GridLayout layout = new GridLayout(1 + dates.length, 0);
    setLayout(layout);
    setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    setVisible(true);

    this.dates = dates;

    checks = new JCheckBox[dates.length][column-1];

    for (int i = 0; i < dates.length; i++) {

        for (int j = 0; j < column - 1; j++) {
            checks[i][j] = new JCheckBox();
            checks[i][j].setSelected(true);
            checks[i][j]
                    .setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            add(checks[i][j]);

        }
    }
}
public JCheckBox[][] getChecks() {
    return checks;
}

in my Main Clas, I have another method that contains:

ResultSet e = connect.select("Invi", "*");
        try {
            while (e.next()) {
                final int inviID = e.getInt("inviID");
                    JPanel pn = new JPanel();
                pn.setSize(d.width, d.height);
            pn.setLocation(0, 0);
                    pn.setLayout(new BoxLayout(pn, BoxLayout.PAGE_AXIS));

                    lp = new LimitPanel(st, 6);
                    pn.add(lp);



                    JButton sabt = new JButton("  ثبت  ");
                    sabt.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            System.out.println("saaaaaaaaaaaaaaaabt");
                            JCheckBox[][] jcb = new JCheckBox[lp.getDates().length][5];
                            jcb = lp.getChecks();
                            for (int i = 0; i < lp.getDates().length; i++)
                                for (int j = 0; j < 5; j++) {
                                    if (!jcb[i][j].isSelected()) {
                                        System.out.println("naaaaaaaaa");
                                        connect.insertLimit(inviID, (lp
                                                .getDates())[i], j+"");
                                    }

                                }

                        }
                    });
                    pn.add(sabt);

                    panels.add(pn);

                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            setContentPane(panels.get(p));
            revalidate();

I edited it to contain what ever is necessary, my problem is that System.out.println("saaaaaaaaaaaaaaaabt"); is always works when I press the button but what ever I do with the checkBoxes System.out.println("naaaaaaaaa"); never works.

War es hilfreich?

Lösung

Your using a while (while (e.next()) {) to build portions of your program. Within it your a creating a new reference to lp and JButton, sabt, on each iteration.

Your ActionListener will only be able to reference the LAST instance of lp created. This is most likely why you actionPerformed is doing what you think it should be...

Think about it like this...if I do...

lp = new new LimitPanel(st, 6);
lp = new new LimitPanel(st, 6);
lp = new new LimitPanel(st, 6);
lp = new new LimitPanel(st, 6);

JCheckBox[] = lp.getChecks();

Which instance of lp have I obtained the check boxes from??

Updated with more details

// Create a new instance of "LimitPanel"
lp = new LimitPanel(st, 6);
// You can check this by using the hashCode of the object...
System.out.println(lp.hashCode());
pn.add(lp);

JButton sabt = new JButton("  ثبت  ");
sabt.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        /*...*/
        // Use what ever was last assigned to "lp"
        jcb = lp.getChecks();
        // You can check this by using the hashCode of the object...
        System.out.println(lp.hashCode());
        /*...*/
    }
});

IF you really wanted to ensure that the ActionListener was using a particular instance of LimitPanel, you should pass that reference to a special instance of ActionListener...

For example...

lp = new LimitPanel(st, 6);
// You can check this by using the hashCode of the object...
System.out.println(lp.hashCode());
pn.add(lp);

JButton sabt = new JButton("  ثبت  ");
sabt.addActionListener(new LimitActionHandler(lp));

And the LimitActionHandler...

public class LimitActionHandler implements ActionListener {

    private LimitPanel limitPane;

    public LimitActionHandler(LimitPanel limitPane) {
        this.limitPane = limitPane;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        /*...*/
        // Use what ever was last assigned to "lp"
        jcb = limitPane.getChecks();
        // You can check this by using the hashCode of the object...
        System.out.println(limitPane.hashCode());
        /*...*/
    }
}

As I stated in my comments, I think it's a bad idea to expose the JCheckBoxes from the LimitPanel, as it allows other parts of your application unrestricted access to those objects, which they don't need for there work...

JCheckBox[] jcb = limitPane.getChecks();
for (JCheckBox cb : jcb) {
    cb.setSelected(false); //...
}
for (JCheckBox cb : jcb) {
    cb.getParent().remove(cb); //...
}

This is very dangerous. You can argue that your application won't do these things, but you can't stop it from happening...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top