public void loginReader (Vector<String> loginy, String tableName)
{
String query = "select login from " + tableName;
try {
Statement statement = mConnection.createStatement();
ResultSet rs = statement.executeQuery(query);
while (rs.next())
{
Vector<String> vstring = new Vector<String>();
vstring.add(rs.getString("login"));
loginy.addAll(vstring);
}
} catch (SQLException e)
{
e.printStackTrace();
}
}
这是在类DatabaseManagement中。我创建了另一个类(GUI),那就是JComboBox。为什么它不起作用?
package DataBase_Hospital;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Properties;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Message extends JFrame implements ActionListener {
JButton SEND_MESSAGE;
JButton READ_MESSAGE;
public JLabel background;
JLabel NAME_LABEL;
JTextField NAME_FIELD;
JTextArea DATABASE_FIELD;
static Vector<String> loginy = new Vector<String>();
private static DatabaseManagement DATABASE;
public Message() {
setSize(290, 500);
setTitle("Message Panel");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JLabel background=new JLabel(new ImageIcon("/Users/Dominik/Desktop/messageFrame.png"));
add(background);
DATABASE_FIELD = new JTextArea(3,3);
JScrollPane scrollPane = new JScrollPane(DATABASE_FIELD);
scrollPane.setBounds(45, 50, 200, 200);
background.add(scrollPane);
DATABASE_FIELD.setEditable(true);
NAME_LABEL = new JLabel("Odbiorca :");
NAME_LABEL.setBounds(40, 380, 140, 20);
background.add(NAME_LABEL);
SEND_MESSAGE = new JButton();
SEND_MESSAGE.setIcon(new ImageIcon("/Users/Dominik/Desktop/sendMail.jpg"));
SEND_MESSAGE.setBounds(75, 270, 60, 60);
background.add(SEND_MESSAGE);
SEND_MESSAGE.addActionListener(this);
SEND_MESSAGE.setToolTipText("Send message");
READ_MESSAGE = new JButton();
READ_MESSAGE.setIcon(new ImageIcon("/Users/Dominik/Desktop/jwj.png"));
READ_MESSAGE.setBounds(150, 270, 60, 60);
background.add(READ_MESSAGE);
READ_MESSAGE.addActionListener(this);
READ_MESSAGE.setToolTipText("Read message");
JComboBox loginList = new JComboBox(loginy);
loginList.setSelectedIndex(loginy.capacity());
loginList.addActionListener(this);
loginList.setBounds(145, 380, 100, 20);
background.add(loginList);
}
public static void main(String[] args) {
Message window = new Message();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
DATABASE.loginReader(loginy,"uzytkownicy");
}
public void actionPerformed(ActionEvent e)
{
Object EVENT_SOURCE = e.getSource();
DATABASE = new DatabaseManagement("pacjent");
if (EVENT_SOURCE == SEND_MESSAGE)
{
DATABASE.sendMessage(DATABASE_FIELD.getText(), "uzytkownicy", NAME_FIELD.getText()) ;
}
}
}
答案 0 :(得分:2)
使用空JComboBox
创建Vector
后,您将selectedIndex
设置为loginy.capacity ()
。问题是,虽然Vector
的容量为10(如JavaDoc for the default constructor中所述),但它的实际大小为0.因此ArrayOutOfBoundsException
。在设置Vector
的所选索引之前,您应该检查JComboBox
的尺寸。
答案 1 :(得分:1)
我怀疑问题在于您是否尝试将组合框的选定索引设置为向量的容量。
loginList.setSelectedIndex(loginy.capacity());
来自文档.capacity()
:
返回此向量的当前容量。 返回: 当前容量(其内部数据数组的长度,保存在此向量的字段elementData中)
这不是数据库中的大小,即登录次数。这是内部数据结构的容量,它总是> =矢量中的元素数。
尝试使用Vector#size()
,但您仍然需要从中减去一个(前提是数据中存在数据),因此您的代码应为:
loginList.setSelectedIndex(loginy.size() - 1);
这将设置comboBox中的最后一次登录。在您的情况下不需要这样,因为您在创建组合框后填充向量,因此您可以从代码中删除此行,直到填充向量。
修改
您需要做的就是让登录重新排序执行顺序。即填充您的向量然后创建您的组合框,将您的主要方法更改为以下内容:
public static void main(String[] args) {
//First initialise your database (dont do this in the action performed method)
// you should only need one and not need to create a new one on each action
DATABASE = new DatabaseManagement("pacjent");
// Read logins (I assume this is the method that does it)
DATABASE.loginReader(loginy,"uzytkownicy");
// Then create your message window...
Message window = new Message();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}