先告诉你方法,用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;
}
}
解决方案 »
- 请问这个在ECLIPSE上运行为什么提示找不到main函数
- 一个比较基础的hql问题
- 无发正常运行!
- java获取远端文件
- 找骂的问题:用Java = 便宜的软件 + 贵重的硬件!
- 字符替换问题
- 求教高手:什么数据属于BCD.Decimal类型??
- 我登陆CSDN后,清除我IE的缓存。然后在按F5刷新。为什么为要重新登录啊。我并没有清除CSDN服务器的SESSION会话啊。
- 一段很简单很简单的jdbc源码,但就是有问题!!!
- java中怎样向文件中写入汉字
- 用javamail做了一个application,在jBuilder中很好,可是生成jar后,却不能单独运行,why?
- 有什么JAVA开发工具可以翻译的?JBuilder好象不能用东方快车翻译。。。
完全面向對象的JAVA中會變的如此複雜?...
誰告訴我....