一个不错的Jtable实例
来源:互联网 发布:手机淘宝在那里改地址 编辑:程序博客网 时间:2024/05/04 21:06
import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.util.Vector;
import
javax.swing.DefaultCellEditor;
import
javax.swing.JButton;
import
javax.swing.JComboBox;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
import
javax.swing.JScrollPane;
import
javax.swing.JTable;
import
javax.swing.table.AbstractTableModel;
import
javax.swing.table.TableColumnModel;
/**
* JTable的实用小例子
*
* @author 五斗米 <如转载请保留作者和出处>
* @blog http://blog.csdn.net/mq612
*/
public
class
Test {
private
JFrame frame =
null
;
private
JTable table =
null
;
private
Table_Model model =
null
;
private
JScrollPane s_pan =
null
;
private
JButton button_1 =
null
, button_2 =
null
, button_3 =
null
;
private
JPanel pane =
null
;
public
Test() {
frame =
new
JFrame(
"JTableTest"
);
pane =
new
JPanel();
button_1 =
new
JButton(
"清除数据"
);
button_1.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
removeData();
}
});
button_2 =
new
JButton(
"添加数据"
);
button_2.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
addData();
}
});
button_3 =
new
JButton(
"保存数据"
);
button_3.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
saveData();
}
});
pane.add(button_1);
pane.add(button_2);
pane.add(button_3);
model =
new
Table_Model(
20
);
table =
new
JTable(model);
table.setBackground(Color.white);
String[] age = {
"16"
,
"17"
,
"18"
,
"19"
,
"20"
,
"21"
,
"22"
};
JComboBox com =
new
JComboBox(age);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(
3
).setCellEditor(
new
DefaultCellEditor(com));
tcm.getColumn(
0
).setPreferredWidth(
50
);
tcm.getColumn(
1
).setPreferredWidth(
100
);
tcm.getColumn(
2
).setPreferredWidth(
50
);
s_pan =
new
JScrollPane(table);
frame.getContentPane().add(s_pan, BorderLayout.CENTER);
frame.getContentPane().add(pane, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(
300
,
200
);
frame.setVisible(
true
);
}
private
void
addData() {
model.addRow(
"李逵"
,
true
,
"19"
);
table.updateUI();
}
private
void
removeData() {
model.removeRows(
0
, model.getRowCount());
table.updateUI();
}
// 保存数据,暂时是将数据从控制台显示出来
private
void
saveData() {
int
col = model.getColumnCount();
int
row = model.getRowCount();
for
(
int
i =
0
; i < col; i++) {
System.out.print(model.getColumnName(i) +
"\t"
);
}
System.out.print(
"\r\n"
);
for
(
int
i =
0
; i < row; i++) {
for
(
int
j =
0
; j < col; j++) {
System.out.print(model.getValueAt(i, j) +
"\t"
);
}
System.out.print(
"\r\n"
);
}
System.out.println(
"------------------------------------"
);
}
public
static
void
main(String args[]) {
new
Test();
System.out
.println(
"按下保存按钮将会把JTable中的内容显示出来\r\n------------------------------------"
);
}
}
/**
* TableModel类,继承了AbstractTableModel
*
* @author 五斗米
*
*/
class
Table_Model
extends
AbstractTableModel {
private
static
final
long
serialVersionUID = -7495940408592595397L;
private
Vector content =
null
;
private
String[] title_name = {
"ID"
,
"姓名"
,
"性别"
,
"年龄"
};
public
Table_Model() {
content =
new
Vector();
}
public
Table_Model(
int
count) {
content =
new
Vector(count);
}
public
void
addRow(String name,
boolean
sex, String age) {
Vector v =
new
Vector(
4
);
v.add(
0
,
new
Integer(content.size()));
v.add(
1
, name);
v.add(
2
,
new
Boolean(sex));
v.add(
3
, age);
content.add(v);
}
public
void
removeRow(
int
row) {
content.remove(row);
}
public
void
removeRows(
int
row,
int
count) {
for
(
int
i =
0
; i < count; i++) {
if
(content.size() > row) {
content.remove(row);
}
}
}
/**
* 让表格中某些值可修改,但需要setValueAt(Object value, int row, int col)方法配合才能使修改生效
*/
public
boolean
isCellEditable(
int
rowIndex,
int
columnIndex) {
if
(columnIndex ==
0
) {
return
false
;
}
return
true
;
}
/**
* 使修改的内容生效
*/
public
void
setValueAt(Object value,
int
row,
int
col) {
((Vector) content.get(row)).remove(col);
((Vector) content.get(row)).add(col, value);
this
.fireTableCellUpdated(row, col);
}
public
String getColumnName(
int
col) {
return
title_name[col];
}
public
int
getColumnCount() {
return
title_name.length;
}
public
int
getRowCount() {
return
content.size();
}
public
Object getValueAt(
int
row,
int
col) {
return
((Vector) content.get(row)).get(col);
}
/**
* 返回数据类型
*/
public
Class getColumnClass(
int
col) {
return
getValueAt(
0
, col).getClass();
}
}
import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.util.Vector;
import
javax.swing.DefaultCellEditor;
import
javax.swing.JButton;
import
javax.swing.JComboBox;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
import
javax.swing.JScrollPane;
import
javax.swing.JTable;
import
javax.swing.table.AbstractTableModel;
import
javax.swing.table.TableColumnModel;
/**
* JTable的实用小例子
*
* @author 五斗米 <如转载请保留作者和出处>
* @blog http://blog.csdn.net/mq612
*/
public
class
Test {
private
JFrame frame =
null
;
private
JTable table =
null
;
private
Table_Model model =
null
;
private
JScrollPane s_pan =
null
;
private
JButton button_1 =
null
, button_2 =
null
, button_3 =
null
;
private
JPanel pane =
null
;
public
Test() {
frame =
new
JFrame(
"JTableTest"
);
pane =
new
JPanel();
button_1 =
new
JButton(
"清除数据"
);
button_1.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
removeData();
}
});
button_2 =
new
JButton(
"添加数据"
);
button_2.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
addData();
}
});
button_3 =
new
JButton(
"保存数据"
);
button_3.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
saveData();
}
});
pane.add(button_1);
pane.add(button_2);
pane.add(button_3);
model =
new
Table_Model(
20
);
table =
new
JTable(model);
table.setBackground(Color.white);
String[] age = {
"16"
,
"17"
,
"18"
,
"19"
,
"20"
,
"21"
,
"22"
};
JComboBox com =
new
JComboBox(age);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(
3
).setCellEditor(
new
DefaultCellEditor(com));
tcm.getColumn(
0
).setPreferredWidth(
50
);
tcm.getColumn(
1
).setPreferredWidth(
100
);
tcm.getColumn(
2
).setPreferredWidth(
50
);
s_pan =
new
JScrollPane(table);
frame.getContentPane().add(s_pan, BorderLayout.CENTER);
frame.getContentPane().add(pane, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(
300
,
200
);
frame.setVisible(
true
);
}
private
void
addData() {
model.addRow(
"李逵"
,
true
,
"19"
);
table.updateUI();
}
private
void
removeData() {
model.removeRows(
0
, model.getRowCount());
table.updateUI();
}
// 保存数据,暂时是将数据从控制台显示出来
private
void
saveData() {
int
col = model.getColumnCount();
int
row = model.getRowCount();
for
(
int
i =
0
; i < col; i++) {
System.out.print(model.getColumnName(i) +
"\t"
);
}
System.out.print(
"\r\n"
);
for
(
int
i =
0
; i < row; i++) {
for
(
int
j =
0
; j < col; j++) {
System.out.print(model.getValueAt(i, j) +
"\t"
);
}
System.out.print(
"\r\n"
);
}
System.out.println(
"------------------------------------"
);
}
public
static
void
main(String args[]) {
new
Test();
System.out
.println(
"按下保存按钮将会把JTable中的内容显示出来\r\n------------------------------------"
);
}
}
/**
* TableModel类,继承了AbstractTableModel
*
* @author 五斗米
*
*/
class
Table_Model
extends
AbstractTableModel {
private
static
final
long
serialVersionUID = -7495940408592595397L;
private
Vector content =
null
;
private
String[] title_name = {
"ID"
,
"姓名"
,
"性别"
,
"年龄"
};
public
Table_Model() {
content =
new
Vector();
}
public
Table_Model(
int
count) {
content =
new
Vector(count);
}
public
void
addRow(String name,
boolean
sex, String age) {
Vector v =
new
Vector(
4
);
v.add(
0
,
new
Integer(content.size()));
v.add(
1
, name);
v.add(
2
,
new
Boolean(sex));
v.add(
3
, age);
content.add(v);
}
public
void
removeRow(
int
row) {
content.remove(row);
}
public
void
removeRows(
int
row,
int
count) {
for
(
int
i =
0
; i < count; i++) {
if
(content.size() > row) {
content.remove(row);
}
}
}
/**
* 让表格中某些值可修改,但需要setValueAt(Object value, int row, int col)方法配合才能使修改生效
*/
public
boolean
isCellEditable(
int
rowIndex,
int
columnIndex) {
if
(columnIndex ==
0
) {
return
false
;
}
return
true
;
}
/**
* 使修改的内容生效
*/
public
void
setValueAt(Object value,
int
row,
int
col) {
((Vector) content.get(row)).remove(col);
((Vector) content.get(row)).add(col, value);
this
.fireTableCellUpdated(row, col);
}
public
String getColumnName(
int
col) {
return
title_name[col];
}
public
int
getColumnCount() {
return
title_name.length;
}
public
int
getRowCount() {
return
content.size();
}
public
Object getValueAt(
int
row,
int
col) {
return
((Vector) content.get(row)).get(col);
}
/**
* 返回数据类型
*/
public
Class getColumnClass(
int
col) {
return
getValueAt(
0
, col).getClass();
}
}
0 0
- 一个不错的Jtable实例
- 一个典型的JTable实例,好好品味~
- 一个相当不错的Eclipse RCP实例
- 一个JTable的例子
- jtable 加入按钮等,一个实例
- 一个不错的HTML5 Canvas多层点击事件监听实例
- jTable实例
- 不错的实例(automake)
- java Swing JTable的复杂应用实例
- 一个不错的计数器
- 一个不错的效果
- 一个不错的网站
- 一个不错的序言
- 一个不错的网站
- 一个不错的万年历
- 一个不错的TAB
- 不错的一个网站
- 一个不错的CSS
- java两个时间段之间取每个年月
- 漫漫长路,我还在起点徘徊
- Letter Combinations of a Phone Number —— Leetcode
- ubuntu 安装hadoop_cdh5.4.1步骤
- eclipse svn 切换项目svn地址
- 一个不错的Jtable实例
- CUBE 和 ROLLUP 之间的具体区别
- SpringMVC接收复杂集合参数
- java 设计模式的开场浅谈
- ORA-06575: Package or function SPLIT is in an invalid state
- OptionsMenu创建方式 以及invalidateOptionsMenu
- 欢迎使用CSDN-markdown编辑器
- android开发之记录ListView滚动位置
- HDU 1274 展开字符串(深搜+字符串,显示缩写的字符串)