新建JTable,列名问题(JAVA)(解决问题后给高分)
personInf.setPreferredScrollableViewportSize(new Dimension(250, 300));//设置表格的大小
priPanel.add(new JScrollPane (personInf));
//priPanel是一个已经new了的panel
运行出来的结果如图,大家看按到了,列名是A,B,C,但是我希望是“图书编号”,“图书名称”,“出版社名”.
应该添加什么语句呢? 展开
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class JTableTest extends JFrame{
public JTableTest(){
setUI();
}
private void setUI(){
this.setTitle("图书列表");
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
//创建标题
String[] t={"图书编号","图书名称","出版社名"};
//创建行
String [][] r={
{"1","JAVA的那些事儿","无语出版社"},
{"2","还是JAVA的那些事儿","无语出版社"},
{"3","又是JAVA的那些事儿","无语出版社"},
};
//在JTable中加载
JTable table =new JTable(r,t);
//将JTable记载到JScrollPane容器中
JScrollPane sp=new JScrollPane(table);
this.add(sp);
}
public static void main(String aegs[]) {
JTableTest t=new JTableTest();
}
}
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;
}
}
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);
}
});
}
}
public JTable(Vector rowData,
Vector columnNames)构造一个 JTable 来显示 Vector 所组成的 Vector rowData 中的值,其列名称为 columnNames。rowData 中包含的 Vector 应该包含该行的值。换句话说,可以通过以下代码获取第 1 行,第 5 列单元格的值:
((Vector)rowData.elementAt(1)).elementAt(5);
参数:
rowData - 新表的数据
columnNames - 每列的名称
public JTable(Object[][] rowData,
Object[] columnNames)构造一个 JTable 来显示二维数组 rowData 中的值,其列名称为 columnNames。rowData 是一个行数组,因此可以通过以下代码获取第 1 行,第 5 列单元格的值:
rowData[1][5]; 所有行的长度必须与 columnNames 的长度相同。
参数:
rowData - 新表的数据
columnNames - 每列的名称
默认表头是abc,用着两个就不是了。