换列名就行了
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;
public class gg extends JFrame{
private JTable jTable1;
private JScrollPane jScrollPane;
private DefaultTableModel tableModel;
private JPanel jPanel1;
public gg() {
jPanel1 = new JPanel();
jScrollPane = new JScrollPane();
try{
String [] head = {"表格列名1","表格列名2","表格列名3"};
Object [][] silencer = {};
//String[] dataType =new String[head.length];
tableModel = new DefaultTableModel(silencer,head);
jTable1 = new JTable(tableModel);
jScrollPane = new JScrollPane(jTable1);
jPanel1.add(jScrollPane);
this.add(jPanel1);
//System.out.println(line);
tableModel.addRow(new String[]{"表格列名1","表格列名2","表格列名3"});
tableModel.addRow(new String[]{"yaowei","yaowei","yaowei"});
tableModel.addRow(new String[]{"gg","gg","gl"});
}catch(Exception e){
e.printStackTrace();
}
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(100,100,500,300);
this.setVisible(true);
}
}
请参考java高级编程书上第六章的这两个程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class SimpleTableTest extends JFrame {
protected JTable table;
protected SortedColumnHeaderRenderer renderer;
public static void main(String[] args) {
SimpleTableTest stt = new SimpleTableTest();
stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stt.setSize(400, 200);
stt.setVisible(true);
}
public SimpleTableTest() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
TableValues tv = new TableValues();
SortedTableModel stm = new SortedTableModel(tv);
// stm.sortRows(TableValues.ACCOUNT_BALANCE, true);
table = new JTable(stm);
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(true);
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(TableValues.GENDER);
tc.setCellRenderer(new GenderRenderer());
tc.setCellEditor(new GenderEditor());
MultiLineHeaderRenderer mlhr = new MultiLineHeaderRenderer();
// tc = tcm.getColumn(TableValues.ACCOUNT_BALANCE);
// tc.setHeaderRenderer(mlhr);
renderer = new SortedColumnHeaderRenderer(stm, mlhr);
int count = tcm.getColumnCount();
for (int i = 0; i < count; i++) {
tc = tcm.getColumn(i);
tc.setHeaderRenderer(renderer);
}
JTableHeaderToolTips jthtt =
new JTableHeaderToolTips(table.getColumnModel());
jthtt.setToolTips(new String[] {"Customer's First Name",
"Customer's Last Name", "Customer's Date of Birth",
"Customer's Account Balance", "Customer's Gender"});
table.setTableHeader(jthtt);
table.setDefaultRenderer(Float.class, new CurrencyRenderer());
JScrollPane jsp = new JScrollPane(table);
pane.add(jsp, BorderLayout.CENTER);
addHeaderListener();
}
public void addHeaderListener() {
table.getTableHeader().addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
JTableHeader header = (JTableHeader)(event.getSource());
int index = header.columnAtPoint(event.getPoint());
Class dataType = table.getModel().getColumnClass(index);
Class[] interfaces = dataType.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (interfaces[i].equals(java.lang.Comparable.class)) {
renderer.columnSelected(index);
break;
}
}
table.setColumnSelectionInterval(index, index);
}
});
}
}
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.table.AbstractTableModel;
public class TableValues extends AbstractTableModel {
public final static int FIRST_NAME = 0;
public final static int LAST_NAME = 1;
public final static int DATE_OF_BIRTH = 2;
public final static int ACCOUNT_BALANCE = 3;
public final static int GENDER = 4;
public final static boolean GENDER_MALE = true;
public final static boolean GENDER_FEMALE = false;
public final static String[] columnNames = {"First\nName", "Last\nName",
"Date of\nBirth", "Account\nBalance", "Gender"};
public Object[][] values = {
{
"Clay", "Ashworth",
new GregorianCalendar(1962, Calendar.FEBRUARY, 20).getTime(),
new Float(12345.67), new Boolean(GENDER_MALE)
}, {
"Jacob", "Ashworth",
new GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(),
new Float(23456.78), new Boolean(GENDER_MALE)
}, {
"Jordan", "Ashworth",
new GregorianCalendar(1989, Calendar.AUGUST, 31).getTime(),
new Float(34567.89), new Boolean(GENDER_FEMALE)
}, {
"Evelyn", "Kirk",
new GregorianCalendar(1945, Calendar.JANUARY, 16).getTime(),
new Float(-456.70), new Boolean(GENDER_FEMALE)
}, {
"Belle", "Spyres",
new GregorianCalendar(1907, Calendar.AUGUST, 2).getTime(),
new Float(567.00), new Boolean(GENDER_FEMALE)
}
};
public int getRowCount() {
return values.length;
}
public int getColumnCount() {
return values[0].length;
}
public Object getValueAt(int row, int column) {
return values[row][column];
}
public String getColumnName(int column) {
return columnNames[column];
}
public Class getColumnClass(int column) {
Class dataType = super.getColumnClass(column);
if (column == ACCOUNT_BALANCE) {
dataType = Float.class;
}
else if (column == DATE_OF_BIRTH) {
dataType = java.util.Date.class;
}
else if ((column == FIRST_NAME) || (column == LAST_NAME)) {
dataType = String.class;
}
else if (column == GENDER) {
dataType = Boolean.class;
}
return dataType;
}
public boolean isCellEditable(int row, int column) {
if (column == GENDER) {
return true;
}
return false;
}
public void setValueAt(Object value, int row, int column) {
values[row][column] = value;
}
}