当我们向JTable中添加了数据(比如对数据库进行了INSERT等操作),怎样才能使立即进行更新呢?我试了validate()好像都不行。但是,拖动Column,改变窗口size等可以让JTable有所反应~~当我们删除了数据,同样的问题将被临.
二.进行查找的时候,若查找结果是一集合,欲将它们在JTable中以selected的形式表现出来,应当如何进行~~对单一结果,可以使用
DefaultListSelectionModel dsm = new DefaultListSelectionModel();
dsm.addSelectionInterval(RowiIndex,RowiIndex);
table.setSelectionModel(dsm);
抑或其它方法办到。好像addSelectionInterval()只能单一选择,不支持多项选择(MULTIPLE_INTERVAL_SELECTION)
沉思中....
10 个解决方案
#1
updateUI()行不行?
#2
用这个监听就可以做到立即更新,一但往表里加上东西就可以看到^_^
import javax.swing.*;
import javax.swing.event.*;
public class MyTableModelListener implements TableModelListener {
JTable table;
// It is necessary to keep the table since it is not possible
// to determine the table from the event's source
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int mColIndex = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
// The inserted rows are in the range [firstRow, lastRow]
for (int r=firstRow; r<=lastRow; r++) {
// Row r was inserted
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// A column was added
} else {
// Column mColIndex in header changed
}
} else {
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was changed
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// All columns in the range of rows have changed
} else {
// Column mColIndex changed
}
}
}
break;
case TableModelEvent.DELETE:
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was deleted
}
break;
}
}
}
用的时候private MyTableModelListener chang=new MyTableModelListener(jTable1);
import javax.swing.*;
import javax.swing.event.*;
public class MyTableModelListener implements TableModelListener {
JTable table;
// It is necessary to keep the table since it is not possible
// to determine the table from the event's source
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int mColIndex = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
// The inserted rows are in the range [firstRow, lastRow]
for (int r=firstRow; r<=lastRow; r++) {
// Row r was inserted
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// A column was added
} else {
// Column mColIndex in header changed
}
} else {
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was changed
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// All columns in the range of rows have changed
} else {
// Column mColIndex changed
}
}
}
break;
case TableModelEvent.DELETE:
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was deleted
}
break;
}
}
}
用的时候private MyTableModelListener chang=new MyTableModelListener(jTable1);
#3
updateUI()行啊~~ft~~怎么自己先就没有试呢~~~
#4
to fatboyslim~~~:
我的做法试 private class DataModel extends AbstractTableModel 。这样似乎要简单许多~~因为AbstractTableModel已经为我们实现了很多功能了,可以大幅度提高偶们的生产力(这句是广告,呵呵~~)~~
我的做法试 private class DataModel extends AbstractTableModel 。这样似乎要简单许多~~因为AbstractTableModel已经为我们实现了很多功能了,可以大幅度提高偶们的生产力(这句是广告,呵呵~~)~~
#5
我是因为有现成的这个类,所以每次都用,呵呵。只是聊以自慰,阿Q一下
其实也是在csdn问过类似的问题牛人告诉我的
其实也是在csdn问过类似的问题牛人告诉我的
#6
大家再帮忙解决第二个wt吧~~~3ku~~
#7
TableModel 生成对象,添加在JTable里
#8
to winter_sui(头上有个包)
能不能说得再详细一点~最好给出源码~
能不能说得再详细一点~最好给出源码~
#9
提供自己的TableModel可以
{
....
myTableModel mtm = new myTableModel(..);
jTable.setModel(mtm);
....
myTableModel tm = (myTableModel)jTable.getModel();
tm.changeData(..);
}
class myTableModel extends AbstractTableModel {
public void changeData(...) {
//修改数据
fireTableDataChanged();
}
}
{
....
myTableModel mtm = new myTableModel(..);
jTable.setModel(mtm);
....
myTableModel tm = (myTableModel)jTable.getModel();
tm.changeData(..);
}
class myTableModel extends AbstractTableModel {
public void changeData(...) {
//修改数据
fireTableDataChanged();
}
}
#10
to 楼主
我现在忙着找工作,等几天我我再说。。。
望见谅。。。。。。。。。。。。。。。。。。
我现在忙着找工作,等几天我我再说。。。
望见谅。。。。。。。。。。。。。。。。。。
#1
updateUI()行不行?
#2
用这个监听就可以做到立即更新,一但往表里加上东西就可以看到^_^
import javax.swing.*;
import javax.swing.event.*;
public class MyTableModelListener implements TableModelListener {
JTable table;
// It is necessary to keep the table since it is not possible
// to determine the table from the event's source
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int mColIndex = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
// The inserted rows are in the range [firstRow, lastRow]
for (int r=firstRow; r<=lastRow; r++) {
// Row r was inserted
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// A column was added
} else {
// Column mColIndex in header changed
}
} else {
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was changed
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// All columns in the range of rows have changed
} else {
// Column mColIndex changed
}
}
}
break;
case TableModelEvent.DELETE:
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was deleted
}
break;
}
}
}
用的时候private MyTableModelListener chang=new MyTableModelListener(jTable1);
import javax.swing.*;
import javax.swing.event.*;
public class MyTableModelListener implements TableModelListener {
JTable table;
// It is necessary to keep the table since it is not possible
// to determine the table from the event's source
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int mColIndex = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
// The inserted rows are in the range [firstRow, lastRow]
for (int r=firstRow; r<=lastRow; r++) {
// Row r was inserted
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// A column was added
} else {
// Column mColIndex in header changed
}
} else {
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was changed
if (mColIndex == TableModelEvent.ALL_COLUMNS) {
// All columns in the range of rows have changed
} else {
// Column mColIndex changed
}
}
}
break;
case TableModelEvent.DELETE:
// The rows in the range [firstRow, lastRow] changed
for (int r=firstRow; r<=lastRow; r++) {
// Row r was deleted
}
break;
}
}
}
用的时候private MyTableModelListener chang=new MyTableModelListener(jTable1);
#3
updateUI()行啊~~ft~~怎么自己先就没有试呢~~~
#4
to fatboyslim~~~:
我的做法试 private class DataModel extends AbstractTableModel 。这样似乎要简单许多~~因为AbstractTableModel已经为我们实现了很多功能了,可以大幅度提高偶们的生产力(这句是广告,呵呵~~)~~
我的做法试 private class DataModel extends AbstractTableModel 。这样似乎要简单许多~~因为AbstractTableModel已经为我们实现了很多功能了,可以大幅度提高偶们的生产力(这句是广告,呵呵~~)~~
#5
我是因为有现成的这个类,所以每次都用,呵呵。只是聊以自慰,阿Q一下
其实也是在csdn问过类似的问题牛人告诉我的
其实也是在csdn问过类似的问题牛人告诉我的
#6
大家再帮忙解决第二个wt吧~~~3ku~~
#7
TableModel 生成对象,添加在JTable里
#8
to winter_sui(头上有个包)
能不能说得再详细一点~最好给出源码~
能不能说得再详细一点~最好给出源码~
#9
提供自己的TableModel可以
{
....
myTableModel mtm = new myTableModel(..);
jTable.setModel(mtm);
....
myTableModel tm = (myTableModel)jTable.getModel();
tm.changeData(..);
}
class myTableModel extends AbstractTableModel {
public void changeData(...) {
//修改数据
fireTableDataChanged();
}
}
{
....
myTableModel mtm = new myTableModel(..);
jTable.setModel(mtm);
....
myTableModel tm = (myTableModel)jTable.getModel();
tm.changeData(..);
}
class myTableModel extends AbstractTableModel {
public void changeData(...) {
//修改数据
fireTableDataChanged();
}
}
#10
to 楼主
我现在忙着找工作,等几天我我再说。。。
望见谅。。。。。。。。。。。。。。。。。。
我现在忙着找工作,等几天我我再说。。。
望见谅。。。。。。。。。。。。。。。。。。