我从数据库中获取数据列表并将它们存储到ResultSet中。我将把结果集存储到数组中,然后将它们映射到梳形框中。问题是我无法预先确定数组的长度。我可以实现它吗?
import java.sql.SQLException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class test extends JFrame {
public static void main(String[] args) throws SQLException {
DBConn db = new DBConn();
JFrame frame = new JFrame();
JComboBox cBoxTaskID = null;
frame.setSize(500, 300);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
int x = 0;
String vSQL = "select taskid from tbltask where status = 'new'";
db.rs = db.GetRecordsetRS(vSQL);
String[] dataTaskIDAdmin = new String[100];
try {
while (db.rs.next()) {
dataTaskIDAdmin[x++] = db.rs.getString("taskid");
}
} catch (SQLException e) {
e.printStackTrace();
}
cBoxTaskID = new JComboBox(dataTaskIDAdmin);
cBoxTaskID.setBounds(50, 50, 200, 20);
frame.add(cBoxTaskID);
}
}
通过使用此...我得到以下结果
修改 MainManager
public class MainManager implements ActionListener{
DBConn db = new DBConn();
WindowConstruct wconstruct;
IntWindowConstruct internal;
TableConstruct tblConstruct;
static DefaultComboBoxModel dataTaskID = new DefaultComboBoxModel();
static DefaultComboBoxModel dataEngineer = new DefaultComboBoxModel();
static JComboBox cBoxTaskID = new JComboBox();
static JComboBox cBoxEngineer = new JComboBox();
public MainManager() {
wconstruct = new WindowConstruct("..:: Manager's Helpdesk Main Page ::..", 1500, 800, false, null,"manager");
wconstruct.btnNew.addActionListener(this);
wconstruct.btnAssign.addActionListener(this);
wconstruct.btnLogout.addActionListener(this);
cBoxTaskID.setModel(dataTaskID);
cBoxEngineer.setModel(dataEngineer);
}
}
在这里,我声明了DefaultComboBoxModel和JComboBox的变量。稍后我在构造函数wconstruct上更新模型。
当我穿上"分配"按钮,它将调用另一个名为 - IntWindowConstruct的构造函数。
if (userType.equalsIgnoreCase("manager") && btnType.equalsIgnoreCase("assign")) {
btnSubmitAssign.setBounds(100, 140, 100, 20);
btnCancelAssign.setBounds(300, 140, 100, 20);
lblTask.setBounds(50, 50, 100, 20);
lblEngineer.setBounds(50, 90, 100, 20);
// cBoxTaskID = new JComboBox(MainManager.dataTaskID);
MainManager.cBoxTaskID.setBounds(160, 50, 250, 20);
// cBoxEngineer = new JComboBox(MainManager.dataEngineer);
MainManager.cBoxEngineer.setBounds(160, 90, 250, 20);
this.add(MainManager.cBoxTaskID);
this.add(MainManager.cBoxEngineer);
this.add(btnSubmitAssign);
this.add(btnCancelAssign);
this.add(lblTask);
this.add(lblEngineer);
}
btnAssign的ActionListener
if (src == wconstruct.btnAssign) {
if (Global.intNewAssign == false) {
int x = 0;
String vSQL = "select taskid from tbltask where status = 'new'";
db.rs = db.GetRecordsetRS(vSQL);
try {
while (db.rs.next()) {
dataTaskID.addElement(db.rs.getString("taskid"));
}
} catch (SQLException err) {
err.printStackTrace();
}
int y = 0;
String vSQL1 = "select username from tblusermaster where usertype='engineer' and status='active'";
db.rs1 = db.GetRecordsetRS(vSQL1);
try {
while (db.rs1.next()) {
dataEngineer.addElement(db.rs1.getString("username"));
}
} catch (SQLException err) {
err.printStackTrace();
}
internal = new IntWindowConstruct("Manager's Assign task", 500, 250, null, "manager", "assign");
internal.btnSubmitAssign.addActionListener(this);
internal.btnCancelAssign.addActionListener(this);
wconstruct.panelIntWin.add(internal);
Global.intNewAssign = true;
} else {
showMessageDialog(null,
"Assign task's window has been opened. Kindly please close it before clicking this button.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
答案 0 :(得分:3)
问题是我无法事先确定数组的长度
完全。不要使用数组。
只需将ResultSet中的数据直接加载到ComboBoxModel
即可。您可以使用DefaultComboBoxModel
并使用addElement(...)
方法加载项目。
不要对不同对象中的数据进行多次引用。
答案 1 :(得分:2)
使用ArrayList
代替,然后在完成后将其转换为数组...
List<String> dataTaskIDAdmin = new ArrayList<>(100);
try{
while(db.rs.next()){
dataTaskIDAdmin.add(db.rs.getString("taskid"));
}
}catch(SQLException e){
e.printStackTrace();
}
cBoxTaskID = new JComboBox(dataTaskIDAdmin.toArray(new String[dataTaskIDAdmin.size()]));
或直接使用DefaultComboBoxModel
...
DefaultComboBoxModel model = new DefaultComboBoxModel();
try{
while(db.rs.next()){
model.addElement(db.rs.getString("taskid"));
}
}catch(SQLException e){
e.printStackTrace();
}
cBoxTaskID = new JComboBox(model);
查看How to Use Combo Boxes了解更多详情......
例如......
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestComboBox {
public static void main(String[] args) {
new TestComboBox();
}
public TestComboBox() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JComboBox comboBox;
private JButton update;
public TestPane() {
setLayout(new GridBagLayout());
update = new JButton("Update");
comboBox = new JComboBox();
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateCombo();
}
});
updateCombo();
add(comboBox);
add(update);
}
protected void updateCombo() {
DefaultComboBoxModel model = new DefaultComboBoxModel();
Random rnd = new Random();
for (int index = 0; index < 10 + rnd.nextInt(90); index++) {
model.addElement(rnd.nextInt(1000));
}
comboBox.setModel(model);
}
}
}
<强>更新强>
int y = 0;
String vSQL1 = "select username from tblusermaster where usertype='engineer' and status='active'";
db.rs1 = db.GetRecordsetRS(vSQL1);
dataEngineer = new DefaultComboBoxModel();
try{
while(db.rs1.next()){
dataEngineer.addElement(db.rs1.getString("username"));
}
}catch(SQLException err){
err.printStackTrace();
}
cBoxEngineer.setModel(dataEngineer);