String[] tableHeads = {"ID","节点ID","服务UUID","服务描述","延迟时间","服务名称","服务类型"};
Vector tableHeadName = new Vector();
Vector rows = new Vector();
for (int i = 0; i < tableHeads.length; i++) {
tableHeadName.add(tableHeads);
}
do
{
rows.addElement( getNextRow( commodityrs, rsmd ) );
}
while ( commodityrs.next() );
table = new JTable( rows, tableHeadName ); //建立一个以Vector为输入来源的数据表格,可显示
commoditytable.setColumnSelectionAllowed(true);
commoditytable.setRowSelectionAllowed(true);
JScrollPane scroller = new JScrollPane( commoditytable );
scroller.setSize(800, 20);
Container c = getContentPane();
c.remove(1);
tablePanel = new JPanel();
tablePanel.setLayout( new BorderLayout() );
tablePanel.add( scroller, BorderLayout.CENTER );
c.add(tablePanel,BorderLayout.CENTER);数据可以正常显示,列名显示不了,列名显示的是Ljava.lang.string;@...
求解决方法
返回该模型中的列数。
String getColumnName(int columnIndex)
返回 columnIndex 位置的列的名称。
int getRowCount()
返回该模型中的行数。
Object getValueAt(int rowIndex, int columnIndex)
返回 columnIndex 和 rowIndex 位置的单元格值 实现这几个函数就可以了他内部估计是自己的一个toString()函数来反射你传进去的列名的..的出来就是这个东东了
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
{
commoditycolumnHeads.addElement( rsmd.getColumnName( i ) );
}
do {
commodityrows.addElement( getNextRow( commodityrs, rsmd ) );
} while ( commodityrs.next() );
ResultSetMetaData commodityrsmd = commodityrs.getMetaData();
commoditytable = new JTable( commodityrows, tableHeadName );
JScrollPane scroller = new JScrollPane( commoditytable );
scroller.setSize(800, 20);
Container c = getContentPane();
c.remove(1);
tablePanel = new JPanel();
tablePanel.setLayout( new BorderLayout() );
tablePanel.add( scroller, BorderLayout.CENTER );
原来的代码如上,是从数据库中读字段名和数据,然后以字段名为列名,数据为表内容制成表格,可以得到正确结果
但我现在要把列名显示为我想要的{"ID","节点ID","服务UUID","服务描述","延迟时间","服务名称","服务类型"};
却无法显示
tableModel.setDataVector(commodityrows, tableHeadName);
commoditytable = new JTable(tableModel);如1楼所言,建tableModel,问题依旧
public MyTableModel(Vector data,Vector columns){
super(data,columns);
}
public boolean isCellEditable(int row,int column){
return false;
}
public Class<?> getColumnClass(int columnIndex) {
if(columnIndex==2){
return String.class;
}
return Object.class;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
((Vector)data.get(rowIndex)).set(columnIndex,aValue);
}
}
******************************
jsp = new JScrollPane();
DefaultTableModel tableModel;
tableModel = new MyTableModel(data,columns);
JTable_FollowEngineReport = new JTable(tableModel){};
jsp.getViewport().add(JTable_FollowEngineReport,null);
怎么就能顯示不了呢 你試試
我现在就是要从数据库表中获取数据,以Jtable显示,但是Jtable的列名要用我自己定的
另:当jtable中某个单元格的数据过长,我想实现这样的功能:在这个单元格上单击右键后,弹出文本框(或菜单)将其内容完整显示,如何实现?