其实很容易,给你个例子看看。/*
* <applet code = TJTable2 width = 350 height = 100>
* </applet>
*/import javax.swing.*;
import java.awt.*;
import java.util.*;public class TJTable2 extends JApplet {
// Create an array of names to be displayed in the table
// header.
String[] columnNames = {"Name", "Size (Bytes)", "Date", "Directory"}; // Create an array of row data (each row is an array) to be
// displayed in the rows of the table.
Object[][] rowData = {
{"AUTOEXEC.BAT", "149", "09-11-98", new Boolean(false)},
{"REAL", "DIR", "12-11-97", new Boolean(true)},
{"WINDOWS", "DIR", "03-24-97", new Boolean(true)},
{"COMMAND.COM", "92879", "07-11-97", new Boolean(false)}}; public void init() {
// 1. Create a table by using the row and column data.
JTable table = new JTable(rowData, columnNames); // 2. Add the table column header and the
// table to the content pane of the applet.
getContentPane().add(table.getTableHeader(), BorderLayout.NORTH);
getContentPane().add(table);
}
}
* <applet code = TJTable2 width = 350 height = 100>
* </applet>
*/import javax.swing.*;
import java.awt.*;
import java.util.*;public class TJTable2 extends JApplet {
// Create an array of names to be displayed in the table
// header.
String[] columnNames = {"Name", "Size (Bytes)", "Date", "Directory"}; // Create an array of row data (each row is an array) to be
// displayed in the rows of the table.
Object[][] rowData = {
{"AUTOEXEC.BAT", "149", "09-11-98", new Boolean(false)},
{"REAL", "DIR", "12-11-97", new Boolean(true)},
{"WINDOWS", "DIR", "03-24-97", new Boolean(true)},
{"COMMAND.COM", "92879", "07-11-97", new Boolean(false)}}; public void init() {
// 1. Create a table by using the row and column data.
JTable table = new JTable(rowData, columnNames); // 2. Add the table column header and the
// table to the content pane of the applet.
getContentPane().add(table.getTableHeader(), BorderLayout.NORTH);
getContentPane().add(table);
}
}
解决方案 »
- 望高手指点:线程并行操作!
- Java和数据库连接
- 求一个给定数组中,和为某一定值的子数组
- 关于List中对象按照属性进行分类(属性相同的对象放入新的arrayList里面,不能覆盖)
- 如何用java实现读取一个目录下的所有文件(包括子目录)?
- 关于JDBC的问题,劳烦各位朋友指教一下经验
- jdom中文帮助文档,另给一百分
- 用java连接打印机 打印中文乱码怎么解决,谢谢
- 向高手请教import语句用法!!!
- 请高手给讲解一下堆栈、堆、静态存储等概念
- 执行TOMCAT时的报错。。
- 请问:BufferedReader keyin=new BufferedReader(new InputStreamReader(new System.in))是什么意思?
The first line of the data file contains the column names. Fields are separated by the "|" character. [customers.dat]
Id|Name|City|Phone
102|Beth Reiser|New York|(212)5558725
111|Dylan Ricci|Syracuse|(315)5554486
116|Brian Gugliuzza|Mamaroneck|(914)5553817
120|Gertrude Stein|Elmsford|(914)5553476
131|Daljit Sinnot|Bohemia|(516)5559811
First we need a TableModel to define the data structure to used by the JTable. [DataFileTableModel.java]
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;public class DataFileTableModel extends AbstractTableModel {
protected Vector data;
protected Vector columnNames ;
protected String datafile;
public DataFileTableModel(String f){
datafile = f;
initVectors();
} public void initVectors() {
String aLine ;
data = new Vector();
columnNames = new Vector();
try {
FileInputStream fin = new FileInputStream(datafile);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
// extract column names
StringTokenizer st1 =
new StringTokenizer(br.readLine(), "|");
while(st1.hasMoreTokens())
columnNames.addElement(st1.nextToken());
// extract data
while ((aLine = br.readLine()) != null) {
StringTokenizer st2 =
new StringTokenizer(aLine, "|");
while(st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
}
catch (Exception e) {
e.printStackTrace();
}
} public int getRowCount() {
return data.size() / getColumnCount();
} public int getColumnCount(){
return columnNames.size();
} public String getColumnName(int columnIndex) {
String colName = ""; if (columnIndex <= getColumnCount())
colName = (String)columnNames.elementAt(columnIndex); return colName;
}
public Class getColumnClass(int columnIndex){
return String.class;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String)data"404.php3" tppabs="http://www.rgagnon.com/javadetails/.elementAt(" (rowIndex * getColumnCount()) + columnIndex);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
return;
}
}
[DataFileTable.java]
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;public class DataFileTable extends JPanel {
public DataFileTable(String dataFilePath) {
JTable table;
DataFileTableModel model;
Font f; f = new Font("SanSerif",Font.PLAIN,24);
setFont(f);
setLayout(new BorderLayout()); model = new DataFileTableModel(dataFilePath); table = new JTable();
table.setModel(model);
table.createDefaultColumnsFromModel(); JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
} public Dimension getPreferredSize(){
return new Dimension(400, 300);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Data File Table");
DataFileTable panel;
panel = new DataFileTable("customers.dat"); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
frame.addWindowListener(new WindowCloser());
}
}class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}