你可以自己写一个自己的DefaultTableModel extends API中的DefaultTableModel,在里面可以设置enable的,然后你想选中一行的话,就必须setCellEnable(false)
你首先得获取你选择的指定行
(getselectrow()方法),再通过getValueAt(row,column);获取指定行的每一个单元格中的数据,再将你获取的值用insert语句插入到数据库中。PS:数据库交互应该不用我多说的吧~~~
getCellRect(int row, int column, boolean includeSpacing)
返回位于 row 和 column 相交位置的单元格矩形。
最主要的事件处理给你:
jTableaddMouseListener(new javaawteventMouseAdapter() {
public void mouseClicked(javaawteventMouseEvent e) {
if(jTablegetValueAt(jTablegetSelectedRow(),0)!=null)
{
String s = (String) jTablegetValueAtjTablegetSelectedRow(),0); //获取所选中的行的第一个位置的内容,当然你也可以指定具体的该行第几格
}
jtextfieldsetText(s);
repaint();
}
});
import javaawtBorderLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJScrollPane;
import javaxswingJTable;
import javaxswingJTextField;
import javaxswingeventTableModelEvent;
import javaxswingeventTableModelListener;
public class JTableTest extends JFrame {
private JPanel searchPanel;
private JTextField searchField;
private JButton searchButton;
private JTable table;
private TestTableModel tableModel;
public JTableTest() {
super("Simple JTable Test");
setSize(400, 300);
searchPanel = new JPanel();
searchField = new JTextField(20);
searchPaneladd(searchField);
searchButton = new JButton("搜索");
searchButtonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tablegetSelectionModel()clearSelection();
doSearch();
}
});
searchPaneladd(searchButton);
getContentPane()add(searchPanel, BorderLayoutNORTH);
tableModel = new TestTableModel();
tableModeladdTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (tableModelgetHitRowCount() > 0) {
tablesetRowSelectionInterval(0, tableModelgetHitRowCount() - 1);
}
}
});
table = new JTable(tableModel);
JScrollPane jsp = new JScrollPane(table);
getContentPane()add(jsp, BorderLayoutCENTER);
}
private void doSearch() {
tableModeldoSearch(searchFieldgetText());
}
public static void main(String[] args) {
JTableTest self = new JTableTest();
selfsetVisible(true);
}
}import javautilArrayList;
import javautilArrays;
import javautilList;
import javaxswingtableAbstractTableModel;
public class TestTableModel extends AbstractTableModel {
private List<String> data = ArraysasList("王二", "王二小", "张三", "张三丰", "李四", "李四光");
private List<Integer> searchHitRows = new ArrayList<Integer>();
@Override
public int getRowCount() {
return datasize();
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public String getColumnName(int column) {
return "姓名";
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return rowIndex < datasize() dataget(rowIndex) : null;
}
public void doSearch(String key) {
searchHitRowsclear();
if (key == null || ""equals(key))
return;
for (int i = 0; i < datasize(); i++) {
String value = dataget(i);
if (valueindexOf(key) < 0)
continue;
int j = 0;
for (j = i - 1; j >= 0; j--) {
String valueJ = dataget(j);
if (valueJindexOf(key) >= 0)
break;
}
String temp = dataget(j + 1);
dataset(j + 1, value);
dataset(i, temp);
searchHitRowsadd(IntegervalueOf(j + 1));
}
fireTableDataChanged();
}
public int getHitRowCount() {
return searchHitRowssize();
}
}
以上就是关于jtable 如何选择行全部的内容,包括:jtable 如何选择行、java中怎样将一个JTable表中的指定行添加到数据库用的是Mysql、JTable什么方法可以选择某行某列的表格等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)