请问怎样才能在jTable中单击一个单元格后将其中的数据提取出来? 用JTable的setValueAt(int iRow,int iCol)方法设定单元格数据用getValueAt(int iRow,int iCol)得到单元格数据如还不能帮你,请再说详细点,在给你解答 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 onefox(一品狐) 没有问题的为了学习~~~~我不惜一切代价了 我研究了一下jdk的demo里面的代码,其中有一个例子是讲如何选择一行或一列现在就应该能够确定某一个单元格了吧,对于其中的数据也应该可以选择出来吧import javax.swing.JTable;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionListener;import javax.swing.event.ListSelectionEvent;import javax.swing.JScrollPane;import javax.swing.JPanel;import javax.swing.JFrame;import java.awt.*;import java.awt.event.*;public class SimpleTableSelectionDemo extends JFrame { private boolean DEBUG = true; private boolean ALLOW_COLUMN_SELECTION = true; private boolean ALLOW_ROW_SELECTION = true; public SimpleTableSelectionDemo() { super("SimpleTableSelectionDemo"); Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Angela", "Lih", "", new Integer(4), new Boolean(false)} }; String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); if (ALLOW_ROW_SELECTION) { // true by default ListSelectionModel rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel)e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No rows are selected."); } else { int selectedRow = lsm.getMinSelectionIndex(); System.out.println("Row " + selectedRow + " is now selected."); } } }); } else { table.setRowSelectionAllowed(false); } if (ALLOW_COLUMN_SELECTION) { // false by default if (ALLOW_ROW_SELECTION) { //We allow both row and column selection, which //implies that we *really* want to allow individual //cell selection. table.setCellSelectionEnabled(true); } table.setColumnSelectionAllowed(true); ListSelectionModel colSM = table.getColumnModel().getSelectionModel(); colSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel)e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No columns are selected."); } else { int selectedCol = lsm.getMinSelectionIndex(); System.out.println("Column " + selectedCol + " is now selected."); } } }); } if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this window. getContentPane().add(scrollPane, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } private void printDebugData(JTable table) { int numRows = table.getRowCount(); int numCols = table.getColumnCount(); javax.swing.table.TableModel model = table.getModel(); System.out.println("Value of data: "); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + model.getValueAt(i, j)); } System.out.println(); } System.out.println("--------------------------"); } public static void main(String[] args) { SimpleTableSelectionDemo frame = new SimpleTableSelectionDemo(); frame.pack(); frame.setVisible(true); }} 这个其实比较简单。直接做的话,先得到选定的行(jtable.getSelectedRow())然后就可以调用JTable.getValueAt()和JTable.setValueAt()来对表格中的数据进行读取、更新了。这个应该是最基本的做法,且自己可以在这基础上进行包装另外做法:看你的JTable的Model是自己写的还是用默认的了。反正不管怎么样都会有一个接口让你往Model里面set数据的,也有对应的取数据的方法。具体看你怎么作了 很对不起,我回学校后校园网一直段了,所以这么晚才回复你的问题其实很简单,起先我看错了。 我做了个简单的例子,下载位置在下面http://my.nbip.net/homepage/zhouB403/MyCode/TableEditDemo.exe双击一行记录,打开行编辑窗口 前几天我通过研究demo解决这个问题了不过我还是很谢谢你的帮助我也仔细看了你的代码,不错谢谢但是我还是有一个问题,我感觉JTable对鼠标的响应速度很忙,不是太灵敏,单击一行数据后要等一阵子才能得到所在表格的数据,或者要重新点击后才会出来,有没有比较好的解决方法?(实在是不好意思再麻烦你) 可能和我查询数据库有关系我每次得到表格中的一个数值后就要用这个数到数据库中查询相应的数据这个过程就比较慢了现在我用的是Access的数据库,这跟数据库有关系吗? 小菜鸟请教大侠几个流问题 急急急:如何设置指定索引号的选项卡被选中???????? 请教各位个问题,请进来看看~~ 新人分少,望赐教~ 急,new java.util.date 出来的日期慢了十六个小时,昨天还用得好好的 如何在一个String中插入一个字符串? jbuilder中的tree 请问如何用java写一个日志文件 怎么调用java.lang.math里的各种方法,如max(),cos() 子类在实例化时,会自动调用父类的构造函数,那请问:会实例化一个父类的对象吗? java 安全性编程实例随书例子 怎样设置前景色?
没有问题的
为了学习~~~~
我不惜一切代价了
现在就应该能够确定某一个单元格了吧,对于其中的数据也应该可以选择出来吧import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;public class SimpleTableSelectionDemo extends JFrame {
private boolean DEBUG = true;
private boolean ALLOW_COLUMN_SELECTION = true;
private boolean ALLOW_ROW_SELECTION = true; public SimpleTableSelectionDemo() {
super("SimpleTableSelectionDemo"); Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"", new Integer(4), new Boolean(false)}
}; String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"}; final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (ALLOW_ROW_SELECTION) { // true by default
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No rows are selected.");
} else {
int selectedRow = lsm.getMinSelectionIndex();
System.out.println("Row " + selectedRow
+ " is now selected.");
}
}
});
} else {
table.setRowSelectionAllowed(false);
} if (ALLOW_COLUMN_SELECTION) { // false by default
if (ALLOW_ROW_SELECTION) {
//We allow both row and column selection, which
//implies that we *really* want to allow individual
//cell selection.
table.setCellSelectionEnabled(true);
}
table.setColumnSelectionAllowed(true);
ListSelectionModel colSM =
table.getColumnModel().getSelectionModel();
colSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No columns are selected.");
} else {
int selectedCol = lsm.getMinSelectionIndex();
System.out.println("Column " + selectedCol
+ " is now selected.");
}
}
});
} if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
} //Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel(); System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
} public static void main(String[] args) {
SimpleTableSelectionDemo frame = new SimpleTableSelectionDemo();
frame.pack();
frame.setVisible(true);
}
}
直接做的话,先得到选定的行(jtable.getSelectedRow())
然后就可以调用JTable.getValueAt()和JTable.setValueAt()来对表格中的数据进行读取、更新了。这个应该是最基本的做法,且自己可以在这基础上进行包装
另外做法:看你的JTable的Model是自己写的还是用默认的了。
反正不管怎么样都会有一个接口让你往Model里面set数据的,也有对应的取数据的方法。具体看你怎么作了
不过我还是很谢谢你的帮助
我也仔细看了你的代码,不错
谢谢但是我还是有一个问题,我感觉JTable对鼠标的响应速度很忙,不是太灵敏,单击一行数据后要等一阵子才能得到所在表格的数据,或者要重新点击后才会出来,有没有比较好的解决方法?(实在是不好意思再麻烦你)
我每次得到表格中的一个数值后就要用这个数到数据库中查询相应的数据
这个过程就比较慢了
现在我用的是Access的数据库,这跟数据库有关系吗?