Topic: 请问如何在JTabbedPane中加入多个JTable?

  Print this page

1.请问如何在JTabbedPane中加入多个JTable? Copy to clipboard
Posted by: sy0300014460
Posted on: 2005-03-13 10:47

请各位大侠帮帮忙,以下这段代码为何表中的标题总是显示最后一个标题,我希望还是用AbstractTableModel() 这个类来实现显示标题,因为我还得通过它来返回数据库中的信息,然后通过table显示出数据。如果你们有更好的方法,请多多指教,谢谢!!

package student;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import com.borland.dbswing.*;
import java.util.*;
public class Untitled1 extends JFrame{
Vector vector;
AbstractTableModel tm;
String head1[]={"id","name","age","class"};
String head2[]={"hello"};
String title[];
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
TableScrollPane tableScrollPane1 = new TableScrollPane();
TableScrollPane tableScrollPane2 = new TableScrollPane();
public Untitled1() {
try {

jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,400);
this.setVisible(true);
}

public static void main(String[] args) {
Untitled1 untitled1 = new Untitled1();
}
private void jbInit() throws Exception {

title=head1;
ininTable();
JdbTable jdbTable1 = new JdbTable(tm);
jdbTable1.setAutoResizeMode(jdbTable1.AUTO_RESIZE_ALL_COLUMNS);
this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
jTabbedPane1.add(jPanel1, "jPanel1");
jPanel1.add(tableScrollPane1, null);
tableScrollPane1.getViewport().add(jdbTable1, null);

title=head2;
ininTable();
JdbTable jdbTable2 = new JdbTable(tm);
jTabbedPane1.add(jPanel2, "jPanel2");
jPanel2.add(tableScrollPane2, null);
tableScrollPane2.getViewport().add(jdbTable2, null);
}
public void ininTable(){
vector=new Vector();
tm = new AbstractTableModel() {
public int getColumnCount() {
return title.length;
}
public int getRowCount() {
return vector.size();
}
public Object getValueAt(int row, int column) {
if (!vector.isEmpty()) {
return ( (Vector) vector.elementAt(row)).elementAt(column);
}
else {
return null;
}
}
public void setValueAt(Object value, int row, int column) {
}
public String getColumnName(int column) {
return title[column];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int column) {
return false;
}
};
}
}


image1.bmp (16.58k)

2.Re:请问如何在JTabbedPane中加入多个JTable? [Re: sy0300014460] Copy to clipboard
Posted by: sy0300014460
Posted on: 2005-03-13 10:52

这是第二个视图,两个图为什么第一个标题与第二个标题一样,请版主及各位大侠帮忙,小弟万分感激!!

image2.bmp (16.83k)

3.Re:请问如何在JTabbedPane中加入多个JTable? [Re: sy0300014460] Copy to clipboard
Posted by: Duncan
Posted on: 2005-03-15 18:18

sy0300014460 wrote:

private void jbInit() throws Exception {

title=head1;
ininTable();
JdbTable jdbTable1 = new JdbTable(tm);
jdbTable1.setAutoResizeMode(jdbTable1.AUTO_RESIZE_ALL_COLUMNS);
this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
jTabbedPane1.add(jPanel1, "jPanel1");
jPanel1.add(tableScrollPane1, null);
tableScrollPane1.getViewport().add(jdbTable1, null);

title=head2;
ininTable();
JdbTable jdbTable2 = new JdbTable(tm);
jTabbedPane1.add(jPanel2, "jPanel2");
jPanel2.add(tableScrollPane2, null);
tableScrollPane2.getViewport().add(jdbTable2, null);
}


JTable 並不會自己管理表格中的資料(含標題),表格資料的來源是 TableModel object,你的程式中的兩個 JTable instance 使用了同一個 TableModel 理當兩個 table 會顯示一模一樣的數據包括標題。再來看看你的 TableModel implementation 的 getColumnName method 的實做方式是傳回 Untitled1 的 instance field: title 所參考的陣列的內容,trace 一下你的碼當程式 stable 後 title 所參考的陣列是 head2 所參考的那一個,於是兩個 table 的標題都只顯示一欄 named "hello"。

4.Re:请问如何在JTabbedPane中加入多个JTable? [Re: sy0300014460] Copy to clipboard
Posted by: sy0300014460
Posted on: 2005-03-16 18:48

多谢Duncan的帮忙,不知Duncan 能否给我举个实例,小弟在此感激不尽。
不知版主能否帮忙找个实例,十分感谢!!!

5.Re:请问如何在JTabbedPane中加入多个JTable? [Re: sy0300014460] Copy to clipboard
Posted by: mochow
Posted on: 2005-04-15 23:25

google


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923