当前位置: 技术问答>java相关
請教Jtable的使用,如何編程插入一行,如何設置和取得任意單元格的數据?最好可以給個例程序.
来源: 互联网 发布时间:2015-02-19
本文导语: | 先告诉你方法,用tablemodel来定制table,至于table地行数,你可以用一个参数n,想插入一行时,就n+1,然后将tablemodel重新设置一下,这时table就多了一行, 用一个二维数组来设置table里面每个单元的值,这样,你就可...
|
先告诉你方法,用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;
}
}