新建JTable,列名问题。
JTablepersonInf=newJTable(5,3);personInf.setPreferredScrollableViewportSize(newDimens...
JTable personInf=new JTable(5,3);
personInf.setPreferredScrollableViewportSize(new Dimension(250, 300));//设置表格的大小
priPanel.add(new JScrollPane (personInf));
大家可以看到列名是A,B,C,但是我希望变成“图书编号”,“图书名称”,“出版社名”? 展开
personInf.setPreferredScrollableViewportSize(new Dimension(250, 300));//设置表格的大小
priPanel.add(new JScrollPane (personInf));
大家可以看到列名是A,B,C,但是我希望变成“图书编号”,“图书名称”,“出版社名”? 展开
2个回答
展开全部
JTable有好几个重载的构造函数呢,你换一个不就行了,比如:
JTable tableDemo = new JTable(new String[][]{{"01","《JAVA编程思想》","**出版社"},{"02","《精通NetBeans》","**出版社"}},new String[]{“图书编号”,“图书名称”,“出版社名”});我这有一个例子应该对你有帮助:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* DemoActionEvent.java
*
* Created on 2009-5-21, 22:31:30
*/
package com.javaeye.bluenight1;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.DefaultTableModel;
/**
*
* @author BlueNight
*/
public class DemoActionEvent extends javax.swing.JFrame {
/** Creates new form DemoActionEvent */
public DemoActionEvent() {
initComponents();
this.initialJTable();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTableMyTable = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jLabelMessage = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTableMyTable.setModel(initialTableModel());
jScrollPane1.setViewportView(jTableMyTable);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jLabelMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabelMessage.setText("这里将显示动作信息");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(45, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DemoActionEvent().setVisible(true);
}
});
}
private DefaultTableModel initialTableModel()
{
DefaultTableModel myDefaultTableModel=new DefaultTableModel(new String[][]{
{"liuyuehui","318320"},
{"niyuding","830414"},
{"boyingking","123456"}},
new String[]{"用户名","密码"});
return myDefaultTableModel;
}
private void initialJTable()
{
DealAction da=new DealAction();
jTableMyTable.getSelectionModel().addListSelectionListener(da);
jTableMyTable.getColumnModel().addColumnModelListener(da);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabelMessage;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTableMyTable;
// End of variables declaration
class DealAction implements ListSelectionListener,TableColumnModelListener
{
int nowColNum=0;
int nowRowNum=0;
public void valueChanged(ListSelectionEvent e) {
nowRowNum=jTableMyTable.getSelectedRow();
this.createMessage();
}
public void columnSelectionChanged(ListSelectionEvent e) {
nowColNum=jTableMyTable.getSelectedColumn();
this.createMessage();
}
public void createMessage()
{
String ss=(String)jTableMyTable.getValueAt(nowRowNum,nowColNum);
DemoActionEvent.this.jLabelMessage.setText("当前选择为第"+(nowRowNum+1)+"行第"+(nowColNum+1)+"列,值为"+ss);
}
public void columnAdded(TableColumnModelEvent e) {}
public void columnRemoved(TableColumnModelEvent e) {}
public void columnMoved(TableColumnModelEvent e) {}
public void columnMarginChanged(ChangeEvent e) {}
}
}
JTable tableDemo = new JTable(new String[][]{{"01","《JAVA编程思想》","**出版社"},{"02","《精通NetBeans》","**出版社"}},new String[]{“图书编号”,“图书名称”,“出版社名”});我这有一个例子应该对你有帮助:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* DemoActionEvent.java
*
* Created on 2009-5-21, 22:31:30
*/
package com.javaeye.bluenight1;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.DefaultTableModel;
/**
*
* @author BlueNight
*/
public class DemoActionEvent extends javax.swing.JFrame {
/** Creates new form DemoActionEvent */
public DemoActionEvent() {
initComponents();
this.initialJTable();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTableMyTable = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jLabelMessage = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTableMyTable.setModel(initialTableModel());
jScrollPane1.setViewportView(jTableMyTable);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jLabelMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabelMessage.setText("这里将显示动作信息");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(45, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DemoActionEvent().setVisible(true);
}
});
}
private DefaultTableModel initialTableModel()
{
DefaultTableModel myDefaultTableModel=new DefaultTableModel(new String[][]{
{"liuyuehui","318320"},
{"niyuding","830414"},
{"boyingking","123456"}},
new String[]{"用户名","密码"});
return myDefaultTableModel;
}
private void initialJTable()
{
DealAction da=new DealAction();
jTableMyTable.getSelectionModel().addListSelectionListener(da);
jTableMyTable.getColumnModel().addColumnModelListener(da);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabelMessage;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTableMyTable;
// End of variables declaration
class DealAction implements ListSelectionListener,TableColumnModelListener
{
int nowColNum=0;
int nowRowNum=0;
public void valueChanged(ListSelectionEvent e) {
nowRowNum=jTableMyTable.getSelectedRow();
this.createMessage();
}
public void columnSelectionChanged(ListSelectionEvent e) {
nowColNum=jTableMyTable.getSelectedColumn();
this.createMessage();
}
public void createMessage()
{
String ss=(String)jTableMyTable.getValueAt(nowRowNum,nowColNum);
DemoActionEvent.this.jLabelMessage.setText("当前选择为第"+(nowRowNum+1)+"行第"+(nowColNum+1)+"列,值为"+ss);
}
public void columnAdded(TableColumnModelEvent e) {}
public void columnRemoved(TableColumnModelEvent e) {}
public void columnMoved(TableColumnModelEvent e) {}
public void columnMarginChanged(ChangeEvent e) {}
}
}
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
2009-05-25
展开全部
用DefaultTableModel构造方法~
Object row[][]= new Object[5][3];
String[] tableHeadName = {"图书编号","图书名称", "出版社名"}; //表头
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setDataVector(row,tableHeadName); //行列加入
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
Object row[][]= new Object[5][3];
String[] tableHeadName = {"图书编号","图书名称", "出版社名"}; //表头
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setDataVector(row,tableHeadName); //行列加入
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询