先告诉你方法,用tablemodel来定制table,至于table地行数,你可以用一个参数n,想插入一行时,就n+1,然后将tablemodel重新设置一下,这时table就多了一行,
用一个二维数组来设置table里面每个单元的值,这样,你就可以确定任何一个单元的值了,
具体的例子有一个,但比较复杂,希望你能好好研究研究:
Listing 10.3 Custom Table Model (TTableModel.java)
// Demonstrates a custom table model.import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;public class TTableModel extends JFrame {
Container container;
JTable table;
JScrollPane scrollPane;
JLabel label;
JTextField textField;
public TTableModel() {
// 1. Assign a title to the frame and get
// the handle on the content pane.
super("TTableModel");
container = this.getContentPane(); // 2. Create a label and text field and add
// them to a panel.
label = new JLabel(
"Enter A Valid Directory Name and Press Return",
JLabel.CENTER);
textField = new JTextField();
textField.addActionListener(new TextFieldListener());
JPanel panel = new JPanel(new GridLayout(2,1));
panel.add(label);
panel.add(textField); // 3. Get the root/system home. Use this home directory
// to create a file object that is used by the directory
// or file system model construct the table model. Also
// display the home directory in the text field.
String home = System.getProperty("user.home");
table = new JTable(new DirectoryModel(new File(home)));
table.createDefaultColumnsFromModel();
textField.setText(home); // 4. Add the panel and table to the container.
container.add(BorderLayout.NORTH, panel);
container.add(new JScrollPane(table));
// 5. Frame settings.
// Add the window closing listener.
addWindowListener(new WindowEventHandler());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBackground(Color.white);
setSize(350, 300); // Frame width=350, height=300
show(); // Display the frame
} // 6. Window event handler.
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
} // 7. The main method.
public static void main(String[] args) {
TTableModel frame = new TTableModel();
} // 8. Text field listener.
class TextFieldListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 9. Get the next directory name entered in
// the text field, prepare the model, and display
// the table by assigning the new data.
DirectoryModel model = new DirectoryModel(
new File(textField.getText()));
table.setModel(model);
}
}
}
// 10. The "Directory" or "FileSystem" model.
class DirectoryModel extends AbstractTableModel {
File directory;
String[] members;
int rowCount; // 11. Model constructor.
public DirectoryModel(File dir) {
directory = dir; // Hold the directory
members = dir.list(); // Get the list of files
// and subdirectories
if (members != null)
// Table rows = No. of entities inside the directory
rowCount = members.length;
else {
// If the memeber list is null, row count should be zero.
rowCount = 0;
// This can happen if an invalid directory is entered
// in the text field.
System.out.println("Not a valid directory!");
}
} // 12. Retrieve the number of rows for the table to be prepared.
public int getRowCount() {
return members != null? rowCount:0;
} // 13. Similarly, retrieve the column count.
public int getColumnCount() {
return members != null? 3:0;
} // 14. Retrieve each of the table values at the specified
// row and column.
public Object getValueAt(int row, int column) {
if (directory == null || members == null) {
return null;
}
File fileSysEntity = new File(directory, members[row]);
switch(column) {
case 0:
return fileSysEntity.getName();
case 1:
if (fileSysEntity.isDirectory()) {
return "...";
}
else {
return new Long(fileSysEntity.length());
}
case 2:
return fileSysEntity.isDirectory()? new Boolean(true):
new Boolean(false);
default:
return "";
}
} // 15. Retrieve the column names to be used in the table header.
public String getColumnName(int column) {
switch(column) {
case 0:
return "Name";
case 1:
return "Bytes";
case 2:
return "Directory";
default:
return "";
}
} // 16. Retrieve the class types of the entries in
// each of the table columns.
public Class getColumnClass(int column) {
Class returnClass = String.class;
if (column == 2)
returnClass = Boolean.class;
return returnClass;
}
}
用一个二维数组来设置table里面每个单元的值,这样,你就可以确定任何一个单元的值了,
具体的例子有一个,但比较复杂,希望你能好好研究研究:
Listing 10.3 Custom Table Model (TTableModel.java)
// Demonstrates a custom table model.import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;public class TTableModel extends JFrame {
Container container;
JTable table;
JScrollPane scrollPane;
JLabel label;
JTextField textField;
public TTableModel() {
// 1. Assign a title to the frame and get
// the handle on the content pane.
super("TTableModel");
container = this.getContentPane(); // 2. Create a label and text field and add
// them to a panel.
label = new JLabel(
"Enter A Valid Directory Name and Press Return",
JLabel.CENTER);
textField = new JTextField();
textField.addActionListener(new TextFieldListener());
JPanel panel = new JPanel(new GridLayout(2,1));
panel.add(label);
panel.add(textField); // 3. Get the root/system home. Use this home directory
// to create a file object that is used by the directory
// or file system model construct the table model. Also
// display the home directory in the text field.
String home = System.getProperty("user.home");
table = new JTable(new DirectoryModel(new File(home)));
table.createDefaultColumnsFromModel();
textField.setText(home); // 4. Add the panel and table to the container.
container.add(BorderLayout.NORTH, panel);
container.add(new JScrollPane(table));
// 5. Frame settings.
// Add the window closing listener.
addWindowListener(new WindowEventHandler());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBackground(Color.white);
setSize(350, 300); // Frame width=350, height=300
show(); // Display the frame
} // 6. Window event handler.
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
} // 7. The main method.
public static void main(String[] args) {
TTableModel frame = new TTableModel();
} // 8. Text field listener.
class TextFieldListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 9. Get the next directory name entered in
// the text field, prepare the model, and display
// the table by assigning the new data.
DirectoryModel model = new DirectoryModel(
new File(textField.getText()));
table.setModel(model);
}
}
}
// 10. The "Directory" or "FileSystem" model.
class DirectoryModel extends AbstractTableModel {
File directory;
String[] members;
int rowCount; // 11. Model constructor.
public DirectoryModel(File dir) {
directory = dir; // Hold the directory
members = dir.list(); // Get the list of files
// and subdirectories
if (members != null)
// Table rows = No. of entities inside the directory
rowCount = members.length;
else {
// If the memeber list is null, row count should be zero.
rowCount = 0;
// This can happen if an invalid directory is entered
// in the text field.
System.out.println("Not a valid directory!");
}
} // 12. Retrieve the number of rows for the table to be prepared.
public int getRowCount() {
return members != null? rowCount:0;
} // 13. Similarly, retrieve the column count.
public int getColumnCount() {
return members != null? 3:0;
} // 14. Retrieve each of the table values at the specified
// row and column.
public Object getValueAt(int row, int column) {
if (directory == null || members == null) {
return null;
}
File fileSysEntity = new File(directory, members[row]);
switch(column) {
case 0:
return fileSysEntity.getName();
case 1:
if (fileSysEntity.isDirectory()) {
return "...";
}
else {
return new Long(fileSysEntity.length());
}
case 2:
return fileSysEntity.isDirectory()? new Boolean(true):
new Boolean(false);
default:
return "";
}
} // 15. Retrieve the column names to be used in the table header.
public String getColumnName(int column) {
switch(column) {
case 0:
return "Name";
case 1:
return "Bytes";
case 2:
return "Directory";
default:
return "";
}
} // 16. Retrieve the class types of the entries in
// each of the table columns.
public Class getColumnClass(int column) {
Class returnClass = String.class;
if (column == 2)
returnClass = Boolean.class;
return returnClass;
}
}
解决方案 »
- 有人用JADE框架做MAS吗?想讨教,因为毕业设计要用到
- TIJ中的一个用于数组比较的程序,请看过这个程序的大虾帮忙看看。
- 菜鸟问题:关于栈的基本使用
- hibernate中关于varchar2()类型插入汉字的问题
- 初学者100分求助:关于applet GUI的问题!
- 如何从根节点删除一个tree并重新建立一个新的tree
- namowen(寒号不已)、adolfdi(牛肉火锅)快来领分~
- 有关applet的通讯问题
- 还是applet的socket的问题,请多指教。
- 装了weblogic 6.1,照说明,PETSTORE总是出不来,求救各位
- 用javamail做了一个application,在jBuilder中很好,可是生成jar后,却不能单独运行,why?
- 有什么JAVA开发工具可以翻译的?JBuilder好象不能用东方快车翻译。。。
完全面向對象的JAVA中會變的如此複雜?...
誰告訴我....