java怎么设置文本框中的显示内容随JTable内的数据改变而改变?
展开全部
自己实现abstracttablemodel,设置其中的setValat,然后对具体的值进行判断对应的那一个jtextfield,
如果判断是需要更改内容的行列的值对应的jtextfield,那么就在设置值的同时更改jtextfield的值
final class userTableModel extends AbstractTableModel
{
/**
*
*/
private static final long serialVersionUID = 1L;
protected List <Object> contents;
protected ArrayList<Integer> rowIndex = new ArrayList<Integer>();
protected List <String> columnNames;
private int curF = -1;
private String curS = "";
private int checkNum = -1;
public userTableModel() {
this.columnNames = new Vector <String>();
this.contents = new Vector <Object>();
}
public userTableModel(String[] columnNames) {
this();
if (null == columnNames)
{
return;
}
for (String columnName : columnNames) {
this.columnNames.add(columnName);
}
}
public userTableModel(Object[][] data,String[] columnNames) throws Exception{
this(columnNames);
addContents(data);
}
public void setCheckNum(int n){
this.checkNum = n;
}
public void addContents(Object[][] datas) throws Exception {
this.contents.clear();
if (null == datas) {
return;
}
for (Object[] data : datas) {
addRow(data);
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
// TODO Auto-generated method stub
if (contents.size() == 0) return super.getClass();
Object value = getValueAt(0, columnIndex);
if (value != null) {
return value.getClass();
}
return super.getClass();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.size();
}
@Override
public String getColumnName(int columnIndex) {
// TODO Auto-generated method stub
return columnNames.get(columnIndex);
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.contents.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector) contents.get(rowIndex)).get(columnIndex);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return super.isCellEditable(rowIndex, columnIndex);
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
((Vector) contents.get(rowIndex)).set(columnIndex, value);
fireTableCellUpdated(rowIndex, columnIndex);
}
public void addRow(Object[] rowData){
if (null == rowData) {
return;
}
if (this.columnNames.size() != rowData.length) {
try {
throw new Exception("Error Lenght of DATA");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Vector <Object> content = new Vector <Object>(this.columnNames.size());
for (int i = 0; i < this.columnNames.size(); i++) {
content.add(rowData[i]);
}
contents.add(content);
fireTableRowsInserted(0, contents.size() - 1);
}
public void removeRows(int row, int count) {
for (int i = 0; i < count; i++) {
if (contents.size() >=row) {
contents.remove(row);
fireTableRowsDeleted(row, row);
}
}
}
public void clear(){
removeRows(0,contents.size());
}
public void removeRow(int row){
contents.remove(row);
fireTableRowsDeleted(row, row);
}
public void insertRow(int row,String[] rowData){
fireTableRowsInserted(0, contents.size() - 1);
}
public void setColumnIdentifiers(String[] columnIdentifiers){
this.columnNames.clear();
for (int i = 0 ; i < columnIdentifiers.length;i++){
if (columnIdentifiers[i] == null)
this.columnNames.add("");
else
this.columnNames.add(columnIdentifiers[i]);
}
}
/*
* clearFlag - from which row to find it,if true,then from 0 else from the last time find result row
* sameFind - the same or is the sub,true - to find the same
* caseS - case sensertive
*/
public int findValueAt(String val,String colN,boolean clearFlag,boolean sameFind,boolean caseS){
if (!val.equals(this.curS) || clearFlag) this.curF = -1;
this.curS = val;
int n,m;
if (this.curF == -1) m = 0;
else
m = this.curF + 1;
if (colN != null)
{
if ((n = this.findColumn(colN)) == -1) return -1;
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(n).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(n).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(n).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
this.curF = -1;
}else
{//looking from all the contents
for (n = 0;n < getColumnCount();n++){
if (n != this.checkNum)
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(n).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(n).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(n).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
}
this.curF = -1;
}
return -1;
}
public int findValueAt(String val,int colN,boolean clearFlag,boolean sameFind,boolean caseS){
if (!val.equals(this.curS) || clearFlag) this.curF = -1;
this.curS = val;
int n,m;
if (this.curF == -1) m = 0;
else
m = this.curF + 1;
if (colN > -1)
{
if ((colN == this.checkNum) || colN > this.getColumnCount()) return -1;
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(colN).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(colN).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(colN).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
this.curF = -1;
}else
{
for (n = 0;n < getColumnCount();n++){
if (n != this.checkNum)
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(n).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(n).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(n).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
}
this.curF = -1;
}
return -1;
}
public int findColumn(String columnName){
for (int i = 0; i < columnNames.size();i++){
if (columnNames.get(i).equals(columnName)){
return i;
}
}
return -1;
}
如果判断是需要更改内容的行列的值对应的jtextfield,那么就在设置值的同时更改jtextfield的值
final class userTableModel extends AbstractTableModel
{
/**
*
*/
private static final long serialVersionUID = 1L;
protected List <Object> contents;
protected ArrayList<Integer> rowIndex = new ArrayList<Integer>();
protected List <String> columnNames;
private int curF = -1;
private String curS = "";
private int checkNum = -1;
public userTableModel() {
this.columnNames = new Vector <String>();
this.contents = new Vector <Object>();
}
public userTableModel(String[] columnNames) {
this();
if (null == columnNames)
{
return;
}
for (String columnName : columnNames) {
this.columnNames.add(columnName);
}
}
public userTableModel(Object[][] data,String[] columnNames) throws Exception{
this(columnNames);
addContents(data);
}
public void setCheckNum(int n){
this.checkNum = n;
}
public void addContents(Object[][] datas) throws Exception {
this.contents.clear();
if (null == datas) {
return;
}
for (Object[] data : datas) {
addRow(data);
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
// TODO Auto-generated method stub
if (contents.size() == 0) return super.getClass();
Object value = getValueAt(0, columnIndex);
if (value != null) {
return value.getClass();
}
return super.getClass();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.size();
}
@Override
public String getColumnName(int columnIndex) {
// TODO Auto-generated method stub
return columnNames.get(columnIndex);
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.contents.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector) contents.get(rowIndex)).get(columnIndex);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return super.isCellEditable(rowIndex, columnIndex);
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
((Vector) contents.get(rowIndex)).set(columnIndex, value);
fireTableCellUpdated(rowIndex, columnIndex);
}
public void addRow(Object[] rowData){
if (null == rowData) {
return;
}
if (this.columnNames.size() != rowData.length) {
try {
throw new Exception("Error Lenght of DATA");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Vector <Object> content = new Vector <Object>(this.columnNames.size());
for (int i = 0; i < this.columnNames.size(); i++) {
content.add(rowData[i]);
}
contents.add(content);
fireTableRowsInserted(0, contents.size() - 1);
}
public void removeRows(int row, int count) {
for (int i = 0; i < count; i++) {
if (contents.size() >=row) {
contents.remove(row);
fireTableRowsDeleted(row, row);
}
}
}
public void clear(){
removeRows(0,contents.size());
}
public void removeRow(int row){
contents.remove(row);
fireTableRowsDeleted(row, row);
}
public void insertRow(int row,String[] rowData){
fireTableRowsInserted(0, contents.size() - 1);
}
public void setColumnIdentifiers(String[] columnIdentifiers){
this.columnNames.clear();
for (int i = 0 ; i < columnIdentifiers.length;i++){
if (columnIdentifiers[i] == null)
this.columnNames.add("");
else
this.columnNames.add(columnIdentifiers[i]);
}
}
/*
* clearFlag - from which row to find it,if true,then from 0 else from the last time find result row
* sameFind - the same or is the sub,true - to find the same
* caseS - case sensertive
*/
public int findValueAt(String val,String colN,boolean clearFlag,boolean sameFind,boolean caseS){
if (!val.equals(this.curS) || clearFlag) this.curF = -1;
this.curS = val;
int n,m;
if (this.curF == -1) m = 0;
else
m = this.curF + 1;
if (colN != null)
{
if ((n = this.findColumn(colN)) == -1) return -1;
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(n).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(n).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(n).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
this.curF = -1;
}else
{//looking from all the contents
for (n = 0;n < getColumnCount();n++){
if (n != this.checkNum)
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(n).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(n).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(n).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
}
this.curF = -1;
}
return -1;
}
public int findValueAt(String val,int colN,boolean clearFlag,boolean sameFind,boolean caseS){
if (!val.equals(this.curS) || clearFlag) this.curF = -1;
this.curS = val;
int n,m;
if (this.curF == -1) m = 0;
else
m = this.curF + 1;
if (colN > -1)
{
if ((colN == this.checkNum) || colN > this.getColumnCount()) return -1;
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(colN).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(colN).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(colN).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
this.curF = -1;
}else
{
for (n = 0;n < getColumnCount();n++){
if (n != this.checkNum)
for (int i = m;i < contents.size();i++){
if (sameFind && caseS){
if (((Vector)contents.get(i)).get(n).toString().equals(val))
{
this.curF = i;
return i;
}
}else if (sameFind)
{
if (((Vector)contents.get(i)).get(n).toString().toUpperCase().equals(val.toUpperCase()))
{
this.curF = i;
return i;
}
}else if (((Vector)contents.get(i)).get(n).toString().toUpperCase().indexOf(val.toUpperCase()) >-1)
{
this.curF = i;
return i;
}
}
}
this.curF = -1;
}
return -1;
}
public int findColumn(String columnName){
for (int i = 0; i < columnNames.size();i++){
if (columnNames.get(i).equals(columnName)){
return i;
}
}
return -1;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |