我有JTable
,通过我的数据库
我正在通过调用此方法初始化我的表,
private void initializeRecordsPanel(){
disableAllPanels();
lblCityJail = new JLabel("Meycauayan City Jail\r\n");
lblCityJail.setBounds(244, 11, 283, 50);
lblCityJail.setForeground(Color.BLACK);
lblCityJail.setFont(new Font("Tahoma", Font.BOLD, 25));
recordsPanel.add(lblCityJail);
btnRefresh.setVisible(true);
try{
connection ();
String columns [] = {
"Last Name", "First Name", "Alias", "Sex", "Case1", "Case2"
};
int count = 0 ;
while(rs.next()){
count ++;
}
String datas[][] = new String [count] [7] ;
int i=0;
connection();
while(rs.next()){
datas[i][0]=rs.getString("lname").toString();
datas[i][1]=rs.getString("fname").toString();
datas[i][2]=rs.getString("alias").toString();
datas[i][3]=rs.getString("sex").toString();
datas[i][4]=rs.getString("case1").toString();
datas[i][5]=rs.getString("case2").toString();
datas[i][6]=rs.getString("ID").toString();
i++;
}
scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(48, 104, 650, 314);
recordsPanel.add(scrollPane_1);
tblListOfInmates = new JTable(datas, columns){
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
//TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tblListOfInmates.getModel());
//tblListOfInmates.setRowSorter(sorter);
tblListOfInmates.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try{
int row = tblListOfInmates.getSelectedRow();
Table_click = tblListOfInmates.getModel().getValueAt(row, 6).toString();
if(rs.next()){
addRecord ar = new addRecord();
ar.connection();
ar.setupFrame();
ar.setSelectedRowToTextFields();
ar.edit.setVisible(false);
ar.save.setVisible(false);
ar.btnEdit.setVisible(true);
ar.setTextFieldsEditable(false);
mainframe.setEnabled(false);
ar.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
MainFrame.mainframe.setEnabled(true);
}
});
st.close();
connection();
}
}catch(Exception e1){e1.printStackTrace();}
}
@Override
public void mouseEntered(MouseEvent e) {
tblListOfInmates.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseExited(MouseEvent e) {
tblListOfInmates.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
scrollPane_1.setViewportView(tblListOfInmates);
JLabel lblSelectToView = new JLabel("* Select to view full record");
lblSelectToView.setBounds(47, 74, 171, 14);
recordsPanel.add(lblSelectToView);
st.close();
connection();
}catch(Exception e){e.printStackTrace();}
recordsPanel.setVisible(true);
}
当我点击一行时,会弹出一个框架并包含我选择的数据。 然后我用这段代码更新表:
try{
connection();
String columns [] = {
"Last Name", "First Name", "Alias", "Sex", "Case1", "Case2"
};
int count = 0 ;
while(rs.next()){
count ++;
}
String datas[][] = new String [count] [7] ;
int i=0;
connection();
while(rs.next()){
datas[i][0]=rs.getString("lname").toString();
datas[i][1]=rs.getString("fname").toString();
datas[i][2]=rs.getString("alias").toString();
datas[i][3]=rs.getString("sex").toString();
datas[i][4]=rs.getString("case1").toString();
datas[i][5]=rs.getString("case2").toString();
datas[i][6]=rs.getString("ID").toString();
i++;
}
scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(48, 104, 650, 314);
recordsPanel.add(scrollPane_1);
tblListOfInmates = new JTable(datas, columns){
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
scrollPane_1.setViewportView(tblListOfInmates);
}catch(Exception e){e.printStackTrace();}
表格已成功更新,但当我再次点击某一行时,它会给我这个错误:
java.lang.ArrayIndexOutOfBoundsException: -1
at javax.swing.JTable$1.getValueAt(Unknown Source)
at RecordManagementSystem.MainFrame$16.mouseClicked(MainFrame.java:805)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
在这一行:
Table_click = tblListOfInmates.getModel().getValueAt(row, 6).toString();
答案 0 :(得分:5)
你正在做的事情有几个问题......
加载数据时,你会......
scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(48, 104, 650, 314);
recordsPanel.add(scrollPane_1);
tblListOfInmates = new JTable(datas, columns){
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
//...
scrollPane_1.setViewportView(tblListOfInmates);
当你更新表格时,你就这样做了......
scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(48, 104, 650, 314);
recordsPanel.add(scrollPane_1);
tblListOfInmates = new JTable(datas, columns){
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
scrollPane_1.setViewportView(tblListOfInmates);
第一个问题是,你没有删除上一个表,因此你现在将组件叠加在一起,这意味着你不知道你实际上在与哪个表进行交互。
下一个问题是,你不需要做所有这些。只需创建JTable
,将其应用于JScrollPane
并将其添加到您需要的容器中。
加载数据时,为数据库中的数据创建一个新的TableModel
,并将其设置为先前创建的JTable
模型...
TableModel model = ...;
//...
tblListOfInmates.setModel(model);
的原则
此:
tblListOfInmates = new JTable(datas, columns){
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
一个坏主意,TableModel
应该控制可以编辑和不编辑的内容。
请查看How to Use Tables和Creating a Table Model了解详情
您还应该使用appropriate layout manager,因为您无法控制可能在不同系统上使用的屏幕分辨率,DPI,渲染管道或字体,所有这些都将使您的像素完美布局并转向变成垃圾......
如果您创建了从数据库中提取的数据的POJO表示,而不是依赖于2D数组,您可能会发现它更容易,但那只是我......
你MouseListener
也让我担心......
tblListOfInmates.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try{
int row = tblListOfInmates.getSelectedRow();
Table_click = tblListOfInmates.getModel().getValueAt(row, 6).toString();
if(rs.next()){
执行if
语句时,您已经移动到结果集的末尾...
答案 1 :(得分:0)
假设您使用的是DefaultTableModel,那么您只需:
model.setRowCount(0)
;