【发布时间】:2014-02-27 10:56:24
【问题描述】:
我正在尝试通过从结果集中获取值并使用它来生成清单来动态创建 Java GUI。我创建了一个小型演示程序来演示我所做的:
SQL 命令
CREATE USER 'test'@'localhost' IDENTIFIED BY 'testpw';
CREATE DATABASE combotest;
USE combotest;
CREATE TABLE combotable (
id INT(5) NOT NULL PRIMARY KEY auto_increment,
type VARCHAR(50) NOT NULL);
INSERT INTO combotable (id, type) VALUES
(default, 'Label'),
(default, 'Textfield'),
(default, 'Combo'),
(default, 'Label'),
(default, 'Textfield'),
(default, 'Combo'),
(default, 'Combo');
GRANT SELECT ON combotest.* TO 'test'@'localhost';
为方便起见,如果您想自己测试,我已将所有 SQL 命令放在上面。
现在,对于我的 Java 代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class resToComboDemo implements ActionListener {
//JDBC Variables
static Connection connect = null;
static Statement statement = null;
static ResultSet res = null;
@SuppressWarnings("rawtypes")
//Other Variables
JComboBox comboBox;
JButton submit;
JFrame frame;
JLabel label;
JTextField textField;
Container pane;
public static void main(String[] args) throws SQLException {
new resToComboDemo();
}
public resToComboDemo() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/combotest?"
+ "user=test&password=testpw");
statement = connect.createStatement();
//Note: in this specific case I do realize that "order by id" is not necessary. I want it there, though.
res = statement.executeQuery("SELECT * FROM combotable ORDER BY id");
createStuff(res);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error 1: "+e, "Error!", JOptionPane.ERROR_MESSAGE);
} finally {
connect.close();
}
}
@SuppressWarnings({"rawtypes", "unchecked" })
public void createStuff (ResultSet res) throws SQLException {
frame = new JFrame("Testing dynamic gui");
Dimension sD = Toolkit.getDefaultToolkit().getScreenSize();
int width = sD.width;
int height = sD.height - 45;
frame.setSize(width,height);
pane = frame.getContentPane();
pane.setLayout(new GridLayout(0, 2));
while (res.next()) {
Object[] options = { "Pass", "Fail"};
String type = res.getString("type");
JLabel label = new JLabel("<html><small>"+type+"</small></html>");
JLabel blank = new JLabel(" ");
blank.setBackground(Color.black);
blank.setOpaque(true);
if (type.equals("Label")) {
label.setBackground(Color.black);
label.setForeground(Color.white);
label.setOpaque(true);
pane.add(label);
pane.add(blank);
} else if (type.equals("Combo")) {
pane.add(label);
comboBox = new JComboBox(options);
pane.add(comboBox);
} else if (type.equals("Textfield")) {
pane.add(label);
textField = new JTextField(20);
pane.add(textField);
}
}
JLabel blank2 = new JLabel(" ");
pane.add(blank2);
submit = new JButton("Submit");
submit.addActionListener(this);
pane.add(submit);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
现在,在这里创建 GUI 一切都很好。但是,我需要能够将 Combobox 和 Textfield 组件视为它们自己独立的实体。意思是,我希望能够从每个不同的组件中获取用户输入。现在,如果我要从文本字段请求信息,它只会给我来自最后一个文本字段的信息。这很完美,因为这就是 java 读取它的方式。我没有问题。
我一生都无法弄清楚如何分别获取每个组件的输入。也许通过获取结果集并将结果添加到某种类型的数组中?我已经以不同的口味尝试过多次,但我无法让它以我需要的方式出现。你们中的一些人会要求我向你们展示我的尝试……但老实说,这不值得。
而且,在有人问之前:不,我不会使用 FlowLayout。 :)
非常感谢任何帮助!
【问题讨论】:
-
为什么不将它们保存在数组或列表中?我还建议修复警告而不是抑制它们。
-
答案取决于。你在乎记录
id吗? -
@MadProgrammer,是的,ID 是相关的,因为我可能要求项目按特定顺序排列。
-
@MikeB 关于将它们保存在一个数组中......我可以让它很好地转储(创建 GUI),但我不知道如何获取每个单独 GUI 的输入元素。
标签: java mysql swing resultset