新建有列名的JTable(JAVA)(回答的好,给多加30分)

personInf=newJTable(5,3);personInf.setPreferredScrollableViewportSize(newDimension(25... personInf=new JTable(5,3);
personInf.setPreferredScrollableViewportSize(new Dimension(250, 300));//设置表格的大小
priPanel.add(new JScrollPane (personInf));
运行结果如图,大家可以看到列名是A,B,C,我要添加什么语句才能改成“图书编号”,“图书名称”,“出版社名”呢?
展开
 我来答
百度网友0c3258b02
2009-05-21 · TA获得超过672个赞
知道小有建树答主
回答量:550
采纳率:0%
帮助的人:627万
展开全部
换列名就行了
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);
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
long_557799
2009-05-21 · 超过26用户采纳过TA的回答
知道答主
回答量:51
采纳率:0%
帮助的人:0
展开全部
请参考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;
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式