我有这个JTable,我有一个Jextfield,两个都在两个不同的jPanel中。我希望用户能够单击jtable中的某个单元格,并从所选行但在第一列中获取单元格的值到jtextfield,而不管用户按下的其他列。当我点击jtable ...建议时出现错误时,我不明白为什么?
先谢谢^ _ ^
package computerscienceia;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class NewLease2 extends jFrame {
public NewLease2(String key) throws IOException {
try {
initComponents();
this.setTitle(key+"'s Available Flats");
RandomAccessFile file = new RandomAccessFile("Ds.txt","rw");
jPanel1.setLayout(new BorderLayout());
JTable tbl = new JTable();
DefaultTableModel dtm = new DefaultTableModel();
String colomnName[] = {"Floor number","Flat number","Area (sqm)","View","Vacant"};
dtm.setColumnIdentifiers(colomnName);
tbl.setEnabled(false);
tbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e){
if(e.getClickCount() == 1){
final JTable target = (JTable)e.getSource();
final int row = target.getSelectedRow();
final String text = (String)target.getValueAt(row, 0).toString();
jTextField1.setText(text);
}
}
});
int position;
int flatnum ;
String line;
ArrayList allFilecontents = new ArrayList();
ArrayList redefinedList = new ArrayList();
while((line = file.readLine())!= null){
if(!line.contains("/////////")){
allFilecontents.add(line);
}
}
for(int x=0; x<allFilecontents.size(); x++){
String line2 = (String) allFilecontents.get(x);
//System.out.println("["+line2+"]");
if(line2.contains("-")){
String[] Splittedline2 = line2.split("-");
if(Splittedline2[0].equals(key)){
position = x; ///WHOLE Point is to Find Key in File and its position
flatnum = Integer.parseInt(Splittedline2[3]);
System.out.println("Position: "+position);
System.out.println("Flatnum: "+flatnum);
for(int i=0; i<= flatnum; i++){
redefinedList.add(allFilecontents.get(position+i));
}
break;
}
}
}
for(int e=0; e<redefinedList.size()-1; e++){
Object[] SplittedE = redefinedList.get(e+1).toString().split("_");
if(SplittedE.length == 5){
for(int t=0; t<SplittedE.length; t++){
System.out.print(SplittedE[t]+",");
}
}
System.out.println(SplittedE.length);//At this point all the data is at FinalList. So
dtm.addRow(SplittedE);
}
tbl.setPreferredScrollableViewportSize(new Dimension(100,400));
tbl.setFillsViewportHeight(true);
tbl.getTableHeader().setReorderingAllowed(false);
tbl.setModel(dtm);
jPanel1.add(new JScrollPane(tbl));
file.close();
}
catch (Exception e) {
e.getCause();
}
}
完整错误Stacktrace: 线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.ArrayIndexOutOfBoundsException:-1 at java.util.Vector.elementData(Vector.java:734) 在java.util.Vector.elementAt(Vector.java:477) 在javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648) 在javax.swing.JTable.getValueAt(JTable.java:2719) at computerscienceia.NewLease2 $ 1.mouseClicked(NewLease2.java:37) 在java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6530) 在javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6292) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4883) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access $ 400(EventQueue.java:97) at java.awt.EventQueue $ 3.run(EventQueue.java:697) at java.awt.EventQueue $ 3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue $ 4.run(EventQueue.java:719) at java.awt.EventQueue $ 4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
您可以覆盖isCellEditable()方法,如下所示,使所有单元格都不可编辑:
JTable tbl = new JTable(){
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
};
答案 1 :(得分:0)
我认为我部分解决了这个问题。 有一次我输入了:tbl.setEnabled(false); 因为我不希望用户对任何单元格进行更改。然而,我删除它,现在它的工作。
但是我现在遇到了一个新问题。有没有人知道如何使细胞不可编辑。