所以我一直在尋找一個解決方案几個小時,但我找不到我的錯誤。 我使用循環創建了JCheckBoxes並將其添加到滾動面板。但是,如果我檢查它們,isSelected()總是返回false。Java CheckBox可選,但isSelected()返回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);
}
}
我希望你能幫助我。如果你需要更多的代碼,我會發布它。
'欄= cf.getFields();' - 哪裏是類'Conn_Fak'? – BackSlash
在哪一點是錯誤的返回?有時你會得到索引我的CheckBox,有時在索引1 –