代码如下
package com.nfs;
import java.awt.*;
import java.util.Vector;import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*; public class TestSimpleJTable {
private static DefaultTableModel tableModel1 = new DefaultTableModel() {
public boolean isCellEditable(int row, int col) {
if (col == 1) {
return true;
}
else {
return false;
}
}//使得第二列的单元格子可编辑,其他列的单元格子不可编辑 // public Class getColumnClass(int col) {
// Vector v=(Vector)dataVector.elementAt(0);
// return v.element(col).getClass();
// } public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}//可以使单元格子实现Image,CheckBox等
}; private JTable jTable1 = new JTable(tableModel1);
public static void main(String[] args) {
String[][] data = {{"A", "B"}, {"C", "D"}};
String[] colName = {"First", "Last"}; JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextField field = new JTextField();
JTable table = new JTable(data,colName){//(tableModel1){
/**
* long
*/
private static final long serialVersionUID = 1L; // JTable的RowSelectionChanged事件在这里。
public void valueChanged(ListSelectionEvent e) {
super.valueChanged(e); if (!this.isVisible()) {
return;
} ListSelectionModel sm =
(ListSelectionModel)e.getSource();
int row = sm.getLeadSelectionIndex(); int colCount = this.getColumnCount();
String[] sa = new String[colCount]; for (int col=0; col<colCount; col++) {
sa[col] = (String)this.getValueAt(row, col);
} String s = "";
for (int i=0; i<sa.length; i++) {
s += sa[i] + "\t";
}
System.out.println("--->>>"+s);
field.setText(s);
}
}; JTable table1 = new JTable(tableModel1);
tableModel1.addRow(new Object[] {new ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "), new Boolean(false), "John ", "male ", "23 "});
tableModel1.addRow(new Object[]{"111","222","233","444","555"});
Vector<String> columnNames = new Vector();
// String[] m_colNames = { "子服务ID", "主服务ID",
// "子服务名称","开始路径","日志路径","备注","是否启用","是否可视","创建时间"};
columnNames.addElement("子服务I");// {"日期","类别","分类","多少钱","卡/现金","说明"};
columnNames.addElement("主服务ID");
columnNames.addElement("子服务名称");
columnNames.addElement("开始路径");
columnNames.addElement("日志路径");
columnNames.addElement("备注");
columnNames.addElement("是否启用");
columnNames.addElement("是否可视");
columnNames.addElement("创建时间");
// dummyMacData.addElement(new Data("105", "1",
// "子服务2","","","","","","2012-09-08"));
Vector<Vector> rowData = new Vector<Vector>();
Vector<String> oneRow = new Vector<String>();
oneRow.addElement("105");
oneRow.addElement("1");
oneRow.addElement("子服务1");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("2012-09-09"); rowData.addElement(oneRow);
Vector<String> oneRow1 = new Vector<String>();
// oneRow.clear();
oneRow1.addElement("100");
oneRow1.addElement("1");
oneRow1.addElement("子服务2");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("2012-04-09");
rowData.addElement(oneRow1);
Vector oneRow2 = new Vector();
oneRow2.addElement(new ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "));
oneRow2.addElement(new ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "));
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
rowData.addElement(oneRow2);
tableModel1.setDataVector(rowData, columnNames);
table1.setPreferredSize(
new Dimension(200, 200)); table.setPreferredSize(
new Dimension(200, 200)); Container container = frame.getContentPane();
container.add(table, BorderLayout.CENTER);
container.add(table1, BorderLayout.EAST);
container.add(field, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
}
package com.nfs;
import java.awt.*;
import java.util.Vector;import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*; public class TestSimpleJTable {
private static DefaultTableModel tableModel1 = new DefaultTableModel() {
public boolean isCellEditable(int row, int col) {
if (col == 1) {
return true;
}
else {
return false;
}
}//使得第二列的单元格子可编辑,其他列的单元格子不可编辑 // public Class getColumnClass(int col) {
// Vector v=(Vector)dataVector.elementAt(0);
// return v.element(col).getClass();
// } public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}//可以使单元格子实现Image,CheckBox等
}; private JTable jTable1 = new JTable(tableModel1);
public static void main(String[] args) {
String[][] data = {{"A", "B"}, {"C", "D"}};
String[] colName = {"First", "Last"}; JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextField field = new JTextField();
JTable table = new JTable(data,colName){//(tableModel1){
/**
* long
*/
private static final long serialVersionUID = 1L; // JTable的RowSelectionChanged事件在这里。
public void valueChanged(ListSelectionEvent e) {
super.valueChanged(e); if (!this.isVisible()) {
return;
} ListSelectionModel sm =
(ListSelectionModel)e.getSource();
int row = sm.getLeadSelectionIndex(); int colCount = this.getColumnCount();
String[] sa = new String[colCount]; for (int col=0; col<colCount; col++) {
sa[col] = (String)this.getValueAt(row, col);
} String s = "";
for (int i=0; i<sa.length; i++) {
s += sa[i] + "\t";
}
System.out.println("--->>>"+s);
field.setText(s);
}
}; JTable table1 = new JTable(tableModel1);
tableModel1.addRow(new Object[] {new ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "), new Boolean(false), "John ", "male ", "23 "});
tableModel1.addRow(new Object[]{"111","222","233","444","555"});
Vector<String> columnNames = new Vector();
// String[] m_colNames = { "子服务ID", "主服务ID",
// "子服务名称","开始路径","日志路径","备注","是否启用","是否可视","创建时间"};
columnNames.addElement("子服务I");// {"日期","类别","分类","多少钱","卡/现金","说明"};
columnNames.addElement("主服务ID");
columnNames.addElement("子服务名称");
columnNames.addElement("开始路径");
columnNames.addElement("日志路径");
columnNames.addElement("备注");
columnNames.addElement("是否启用");
columnNames.addElement("是否可视");
columnNames.addElement("创建时间");
// dummyMacData.addElement(new Data("105", "1",
// "子服务2","","","","","","2012-09-08"));
Vector<Vector> rowData = new Vector<Vector>();
Vector<String> oneRow = new Vector<String>();
oneRow.addElement("105");
oneRow.addElement("1");
oneRow.addElement("子服务1");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("");
oneRow.addElement("2012-09-09"); rowData.addElement(oneRow);
Vector<String> oneRow1 = new Vector<String>();
// oneRow.clear();
oneRow1.addElement("100");
oneRow1.addElement("1");
oneRow1.addElement("子服务2");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("");
oneRow1.addElement("2012-04-09");
rowData.addElement(oneRow1);
Vector oneRow2 = new Vector();
oneRow2.addElement(new ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "));
oneRow2.addElement(new ImageIcon( "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.gif "));
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
oneRow2.addElement("");
rowData.addElement(oneRow2);
tableModel1.setDataVector(rowData, columnNames);
table1.setPreferredSize(
new Dimension(200, 200)); table.setPreferredSize(
new Dimension(200, 200)); Container container = frame.getContentPane();
container.add(table, BorderLayout.CENTER);
container.add(table1, BorderLayout.EAST);
container.add(field, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
}
解决方案 »
- 请教 JScrollPane为什么总是显示不了两个滚动条??
- jdk1.4中数组对象问题
- 请问一下?
- 急,放在byte[]中的unicode怎么转然换成GB2312?
- 谁能给我一个最简单的JTree的例子?
- 哪位用过用过ORACLE的Jdeveloper开发过项目,谈谈心得?
- Applet中改变字体和大小显示?在线等待,急!!!
- 我的java程序要進行平台移植,原來的DLL庫還能用嗎?
- 在问一个javamail的问题,怎么收到草稿箱、发件箱等不是收件箱里的信件?谢谢!
- 用ArrayList或LinkedList实现先进先出功能
- 几道竞赛题,请用java做出来,求教!
- java基础问题,java 方法为什么要覆写,在子类中重新写个方法不就好了吗
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
这个方法决定了显示的效果。
而默认只是把value toString()了。如果你想显示为图片,则需要重写此方法:import java.awt.Component;import javax.swing.Icon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;public class SupportIconTableCellRenderer extends DefaultTableCellRenderer { @Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, column); if (value instanceof Icon) {
this.setIcon((Icon) value);
// remove lable's text
this.setText("");
} return this;
}}
简单实现了下,自己可以根据需要扩展。