JList 从组件面板放置在表单上没有响应



好的,我正在使用IntelliJ作为一个简单的sqlite数据库程序。 我对这个平台很陌生。 我在窗体上放置了一个 JList 组件,并将其命名为 list1。 现在,当我尝试用字符串填充它时,我使用了向量和列表模型,但我没有看到任何数据。 我认为它没有与IntelliJ创建的JList连接。 我已经看到了通过代码完成所有操作的示例,但是我想知道如何使用设计时放置的JList。 我想按下一个按钮并在循环浏览 sqlite 数据后填充列表。 我知道我不是那么好,但这是我到目前为止所拥有的:

public class mainForm {
private JPanel panelMain;
private JButton button1;
// private Vector vStatic = new Vector();
/////LOOK HERE ->
DefaultListModel listModel;
JList list1;


public mainForm() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// JOptionPane.showMessageDialog(null, "This really sucks!!");
Connection c;
Statement stmt;
listModel = new DefaultListModel();
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:javacarl.db");
//  c.setAutoCommit(true);
JOptionPane.showMessageDialog(null, "Open Success!");
stmt = c.createStatement();
ResultSet res = stmt.executeQuery("SELECT * FROM CARL");
//  int i = 0;
res.next();
/////LOOK HERE ->
while (res.next()) {
listModel.addElement(res.getString("JOB"));
}
JList list1 = new JList(listModel);
list1.updateUI();
stmt.close();
c.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Operation Not A Successful!"+e1);
System.exit(0);
}
JOptionPane.showMessageDialog(null, "Operation Success!");
}
});
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new mainForm().createAndShowUI();
}
});
Connection c;
Statement stmt;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:javacarl.db");
JOptionPane.showMessageDialog(null, "Opened database successfully!");
stmt = c.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS CARL " +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
" JOB            TEXT    NOT NULL, " +
" DATE           TEXT    NOT NULL, " +
" MATERIAL       REAL    NOT NULL, " +
" LABOR          REAL    NOT NULL, " +
" SUBTOTAL       REAL    NOT NULL, " +
" TAX            REAL, " +
" TOTAL          REAL, " +
" DEPAMT         REAL, " +
" AMTPAID        REAL)";
stmt.executeUpdate(sql);
stmt.close();
//  c.commit();
c.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
// JOptionPane.showMessageDialog(null, "Table Created successfully!" );
}
private void createAndShowUI() {
Toolkit t;
Dimension s;
JFrame frame = new JFrame("MainForm");
frame.setContentPane(new mainForm().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(750, 500));
t = Toolkit.getDefaultToolkit();
s = t.getScreenSize();
frame.setLocation(s.width / 4 - frame.getWidth() / 2, s.height / 4 - frame.getHeight() / 2);
initComponents(frame);
frame.pack();
frame.setVisible(true);
}

private void initComponents(JFrame frame) {
JMenu menu1 = new JMenu("File");
JMenuItem deleteall = new JMenuItem("Delete all records");
JMenuItem addjob = new JMenuItem("Add Job");
JMenuItem exit1 = new JMenuItem("Exit");
menu1.add(deleteall);
menu1.add(addjob);
menu1.add(exit1);
addjob.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection c;
Statement stmt;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:javacarl.db");
c.setAutoCommit(false);
stmt = c.createStatement();
//String sql = "DELETE FROM CARL";
String sql = "INSERT INTO CARL (JOB,DATE,MATERIAL,LABOR,SUBTOTAL,TAX,TOTAL,DEPAMT,AMTPAID) " +
"VALUES ('Disaster on goodrich rd!','03/03/2020', 1000.00, 100.00, 1100.00,11.00,0,0,0 );";
//stmt.executeUpdate(sql);
// sql = "DELETE FROM sqlite_sequence WHERE name = 'CARL'";
stmt.executeUpdate(sql);
c.commit();
stmt.close();
c.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "New record failed!");
System.exit(0);
}
JOptionPane.showMessageDialog(null, "New record inserted!");
//  JFileChooser chooser = new JFileChooser();
//  chooser.showOpenDialog(null);
}
});
deleteall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection c;
Statement stmt;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:javacarl.db");
c.setAutoCommit(false);
stmt = c.createStatement();
String sql = "DELETE FROM CARL";
stmt.executeUpdate(sql);
sql = "DELETE FROM sqlite_sequence WHERE name = 'CARL'";
stmt.executeUpdate(sql);
c.commit();
stmt.close();
c.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Deletion failed!");
System.exit(0);
}
JOptionPane.showMessageDialog(null, "All records deleted!");
}
});
exit1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenuBar jmb = new JMenuBar();
jmb.add(menu1);
frame.setJMenuBar(jmb);
//  report.addItemListener((ItemListener) this);
}

}

我可以在这里发布这段对我有用的代码:

public void updateJList() {
Connection c;
Statement stmt;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:javajobs.db");
stmt = c.createStatement();
ResultSet res = stmt.executeQuery("SELECT * FROM JOBS ORDER BY ID DESC");
model.clear();
while (res.next()) {
model.addElement(res.getInt("ID") + "*-- " + res.getString("DATE") +"  -- " + res.getString("NAME")
+ " -- Job Desc: " + res.getString("JOB") + " -- Labor = "
+ res.getBigDecimal("LABOR")+ " -- Mat = " + res.getString("MATERIAL") + " -- Total = "
+ res.getBigDecimal("TOTAL"));
}
stmt.close();
c.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Operation JList load Not Successful!" + e1);
}
}

相关内容

  • 没有找到相关文章

最新更新