jeudi 18 mai 2017

Java CheckBox selectable, but isSelected() returning false

So I've been looking for a solution for hours, but I just can't find my mistake. I created JCheckBoxes using a loop and adding it to a scroll panel. But if I check them, isSelected() always returns false.

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Conn_Fak_GUI extends JFrame{
public static ArrayList<String> fields;
public Conn_Fak cf;
public ArrayList <JCheckBox> cb;

public Conn_Fak_GUI()
{
    this.setTitle("AMAG - Schnittstelle");
    this.setSize(500, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(null);


    JLabel db_fak = new JLabel("Pfad zur Amicron - Datenbank: ");
    db_fak.setBounds(10,10,200,20);
    this.add(db_fak);

    JTextArea input_db_fak = new JTextArea();
    input_db_fak.setBounds(10,30,300,20);
    this.add(input_db_fak);

    JLabel usernameinfo = new JLabel("Benutzername:");
    usernameinfo.setBounds(10,60,100,20);
    this.add(usernameinfo);

    JTextField username = new JTextField();
    username.setBounds(110,60,100,20);
    this.add(username);

    JLabel passwordinfo = new JLabel("Passwort:");
    passwordinfo.setBounds(10,80,100,20);
    this.add(passwordinfo);

    JTextField password = new JTextField();
    password.setBounds(110,80,100,20);
    this.add(password);


    JButton connect = new JButton("Verbinden");
    connect.setBounds(350,80,100,20);
    connect.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e)
        {

            try {
                cf = new 
  Conn_Fak(input_db_fak.getText(),username.getText(),password.getText());
            } catch (ClassNotFoundException | SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                fields= cf.getFields();
                displayFields(); 
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            displayFields();
        }
    });


    JButton export = new JButton("Exportieren");
    export.setBounds(350,360,100,20);
    export.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.out.println(cb.get(1).isSelected());
            ExportData ed = new ExportData(fields,cf,cb);
            try {
                ed.exportToFile();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    this.add(export);
    this.add(connect);

    //FileChooser
    JLabel ofile = new JLabel("Zieldatei: ");
    ofile.setBounds(10,340,200,20);
    this.add(ofile);

    JTextArea input_ofile = new JTextArea();
    input_ofile.setBounds(10,360,300,20);
    this.add(input_ofile);

    this.setVisible(true);
}
public void displayFields()
{
    JCheckBox jb;
    cb = new ArrayList<JCheckBox>();


    for(int i = 0; i < fields.size(); i++)
    {
        jb = new JCheckBox();
        jb.setText(fields.get(i));
        cb.add(jb);
    }




    JPanel contentPane = new JPanel();
    JPanel listOfFiles = new JPanel();
    listOfFiles.setLayout(new BoxLayout(listOfFiles, BoxLayout.Y_AXIS));


    for(int i = 0; i < cb.size(); i++){
        listOfFiles.add(cb.get(i));
    }

    JScrollPane jScrollPane = new JScrollPane(listOfFiles);





    jScrollPane.setVerticalScrollBarPolicy
    (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane.setPreferredSize(new Dimension (300, 200));
    contentPane.setBounds(10,120,300,200);
    contentPane.add(jScrollPane);

    this.add(contentPane);
    this.revalidate();
    this.repaint();

}


}

import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JCheckBox;

public class ExportData {
ArrayList <String> activatedFields;
ArrayList <String> fields;
ArrayList <JCheckBox> cb;
Conn_Fak cf;

public ExportData(ArrayList<String> fields, Conn_Fak cf,ArrayList<JCheckBox> 
cb){
    super();
    this.fields = fields;
    this.cf = cf;
    this.cb = cb;
}

public void exportToFile() throws SQLException{
    System.out.println(cb.size());
    if(!cb.get(1).isSelected()){
        System.out.println(cb.get(1));
    }


    for(int i = 0; i < cb.size(); i++){
        if(cb.get(i).isSelected()){
            System.out.println(cb.get(i).getText());
            activatedFields.add(cb.get(i).getText());
        }

    }
    for(int j = 0; j < activatedFields.size();j++){
        System.out.println("+"+activatedFields.get(j));
    }
    cf.getData(activatedFields);


    }


}

I hope you can help me. If you need more code i will post it.




Aucun commentaire:

Enregistrer un commentaire