表格控件使用类JTableWord文档下载推荐.docx

上传人:b****5 文档编号:16690478 上传时间:2022-11-25 格式:DOCX 页数:12 大小:18.73KB
下载 相关 举报
表格控件使用类JTableWord文档下载推荐.docx_第1页
第1页 / 共12页
表格控件使用类JTableWord文档下载推荐.docx_第2页
第2页 / 共12页
表格控件使用类JTableWord文档下载推荐.docx_第3页
第3页 / 共12页
表格控件使用类JTableWord文档下载推荐.docx_第4页
第4页 / 共12页
表格控件使用类JTableWord文档下载推荐.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

表格控件使用类JTableWord文档下载推荐.docx

《表格控件使用类JTableWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《表格控件使用类JTableWord文档下载推荐.docx(12页珍藏版)》请在冰豆网上搜索。

表格控件使用类JTableWord文档下载推荐.docx

JPanelcustomTablePane=newJPanel();

JScrollPaneTableScrollPane2=newJScrollPane();

BorderLayoutborderLayout1=newBorderLayout();

publicTableDemo(){

try{jbInit();

}catch(Exceptione){}

}

publicstaticvoidmain(String[]args){

TableDemoframe=newTableDemo();

frame.setVisible(true);

//当窗口关闭时的处理代码

protectedvoidprocessWindowEvent(WindowEvente){

if(e.getID()==WindowEvent.WINDOW_CLOSING){

System.exit(0);

privatevoidjbInit()throwsException{

//设置窗口的标题,大小,布局

setTitle("

列表实例演示"

);

setSize(newDimension(565,366));

contentPane=(JPanel)this.getContentPane();

contentPane.setLayout(newBoxLayout(contentPane,BoxLayout.Y_AXIS));

Object[][]data={

{"

小李"

"

男"

滑雪"

newInteger(20),"

英文"

newBoolean(true)},

小青"

女"

保龄球"

newInteger(17),"

绘画"

小吴"

跑步"

newInteger(23),"

管理"

小和"

演讲"

newInteger(25),"

会计"

newBoolean(false)},

小许"

游泳"

newInteger(15),"

数字"

newBoolean(false)}

};

String[]columnNames={"

姓名"

性别"

爱好"

年龄"

专业"

是否被录取"

};

//创建一个简单的表格

finalJTabletable=newJTable(data,columnNames);

//使选择框成为一个单元格

table.setColumnSelectionAllowed(true);

//将表格的列排列自动功能设置为OFF,这样便可以使用代码定义列的长度

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

table.setRowHeight(20);

//设置表格的行高度

//设置列表的列长度

TableColumncolumn=null;

for(inti=0;

i<

5;

i++){

//取得列表的列对象

column=table.getColumnModel().getColumn(i);

if(i==1){

column.setPreferredWidth(30);

//sportcolumnisbigger

}else{

column.setPreferredWidth(50);

//取得表格的行

ListSelectionModelrowSM=table.getSelectionModel();

//加入行选择接收器

rowSM.addListSelectionListener(newListSelectionListener(){

publicvoidvalueChanged(ListSelectionEvente){

//当多种事件被激发的时候,不执行行接收器后面的代码

if(e.getValueIsAdjusting())return;

//取得行对象

ListSelectionModellsm=(ListSelectionModel)e.getSource();

intselectedRow=lsm.getMinSelectionIndex();

System.out.println("

行"

+selectedRow);

});

//取得表格的列

ListSelectionModelcolSM=table.getColumnModel()

.getSelectionModel();

//加入列选择接收器

colSM.addListSelectionListener(newListSelectionListener(){

//当多种事件被激发的时候,不执行列接收器后面的代码

//取得列对象

intselectedCol=lsm.getMinSelectionIndex();

列"

+selectedCol);

//创建带有模式类的列表框,并加入数据改变接收器

MyTableModelmyModel=newMyTableModel();

finalJTabletable1=newJTable(myModel);

table1.setColumnSelectionAllowed(true);

table1.setRowHeight(30);

//当表格的内容改变的时候取得表格的内容

finalTableModelmodel=table1.getModel();

model.addTableModelListener(newTableModelListener(){

publicvoidtableChanged(TableModelEvente){

introw=e.getFirstRow();

intcolumn=e.getColumn();

//取得列表框的数据

Objectdata=model.getValueAt(row,column);

try{System.out.println(data);

table1.requestFocus();

}

catch(Exceptione1){}

//为数据表中"

一列加入下拉列表框

TableColumnsportColumn=table1.getColumnModel().getColumn(4);

JComboBoxcomboBox=newJComboBox(

newString[]{"

});

sportColumn.setCellEditor(newDefaultCellEditor(comboBox));

//为数据表的数字列加入数字检证框

finalJTextFieldintegerField=newJTextField();

integerField.setHorizontalAlignment(JTextField.RIGHT);

DefaultCellEditorintegerEditor=

newDefaultCellEditor(integerField){

//重载返回值的方法,当输入文字的时候,将文字转变为0

publicObjectgetCellEditorValue(){

try{returnnewDouble(integerField.getText());

catch(Exceptione){returnnewDouble(0);

table1.setDefaultEditor(Integer.class,integerEditor);

//设置颜色接收器

setUpColorEditor(table1);

//设置颜色渲染器

table1.setDefaultRenderer(Color.class,

newColorRenderer(true));

//设置容器的布局及加入各种控件

customTablePane.setLayout(borderLayout1);

TableScrollPane.getViewport().add(table);

TableScrollPane2.getViewport().add(table1);

contentPane.add(TableScrollPane);

contentPane.add(customTablePane,null);

customTablePane.add(TableScrollPane2,BorderLayout.CENTER);

//设置颜色编辑框的方法

privatevoidsetUpColorEditor(JTabletable){

//创建按钮,用于激活颜色选择框

finalJButtonbutton=newJButton("

"

){

publicvoidsetText(Strings){

//重新定义SetText方法,使按钮不会显示文字

//设置按钮的背景颜色,不绘画按钮的边框,并将按钮的边框设为0

button.setBackground(Color.white);

button.setBorderPainted(false);

button.setMargin(newInsets(0,0,0,0));

//创建颜色编辑框,并将该编辑框赋给表格

finalColorEditorcolorEditor=newColorEditor(button);

table.setDefaultEditor(Color.class,colorEditor);

//创建颜色选择框

finalJColorChoosercolorChooser=newJColorChooser();

ActionListenerokListener=newActionListener(){

publicvoidactionPerformed(ActionEvente){

//将颜色选择框的颜色赋给颜色编辑框

colorEditor.currentColor=colorChooser.getColor();

finalJDialogdialog=JColorChooser.createDialog(button,

"

请选择喜欢的颜色"

true,

colorChooser,

okListener,

null);

button.addActionListener(newActionListener(){

//设置按钮的背景颜色

button.setBackground(colorEditor.currentColor);

//将颜色选择器的颜色设为原先的显示颜色

colorChooser.setColor(colorEditor.currentColor);

//在按钮的旁边显示颜色选择框,省略该命令,颜色选择框会显示在Window

//窗口的中央

dialog.setLocationRelativeTo(button);

dialog.show();

//表格模式类

classMyTableModelextendsAbstractTableModel{

finalObject[][]data={

newColor(56,78,82),newImageIcon("

face1.gif"

),

face2.gif"

face3.gif"

face4.gif"

face5.gif"

finalString[]columnNames={"

喜欢的颜色"

图片"

//下面三个方法是一定要加入AbstractTableModel类

//分别是设置列数,行数与列表的数据

publicintgetColumnCount(){returncolumnNames.length;

publicintgetRowCount(){returndata.length;

publicObjectgetValueAt(introw,intcol){returndata[row][col];

//该方法不一定要加入AbstractTableModel类,该方法设置列表的标题

publicStringgetColumnName(intcol){returncolumnNames[col];

//通过getClass()方法设置默认的控件,如true与false的默认控件是复选框,

//该方法不一定要加入AbstractTableModel类

publicClassgetColumnClass(intc){

returngetValueAt(0,c).getClass();

//定义可以编辑的单元,true表示可编辑,false表示不可编辑

publicbooleanisCellEditable(introw,intcol){

returntrue;

//将改变后的值保存在表格中

publicvoidsetValueAt(Objectvalue,introw,intcol){

data[row][col]=value;

fireTableCellUpdated(row,col);

//颜色渲染框的代码

classColorRendererextendsJLabel

implementsTableCellRenderer{

BorderunselectedBorder=null;

BorderselectedBorder=null;

booleanisBordered=true;

publicColorRenderer(booleanisBordered){

super();

this.isBordered=isBordered;

setOpaque(true);

//每当颜色改变时,该方法被激活

publicComponentgetTableCellRendererComponent(

JTabletable,Objectcolor,

booleanisSelected,booleanhasFocus,

introw,intcolumn){

setBackground((Color)color);

if(isBordered){

if(isSelected){

if(selectedBorder==null){

selectedBorder=BorderFactory.createMatteBorder(2,5,2,5,

table.getSelectionBackground());

setBorder(selectedBorder);

if(unselectedBorder==null){

unselectedBorder=BorderFactory.createMatteBorder(2,5,2,5,

table.getBackground());

setBorder(unselectedBorder);

returnthis;

//颜色编辑框的代码

classColorEditorextendsDefaultCellEditor{

ColorcurrentColor=null;

publicColorEditor(JButtonb){

//由于数据表的编辑器只能是复选框,编辑框与标签,所以首先将数据表的编辑器设为

//复选框

super(newJCheckBox());

//利用DefaultCellEditor类的editorComponent属性将编辑器设为按钮

editorComponent=b;

setClickCountToStart

(1);

//单击按钮时动作事件激活

//让编辑器停止事件的处理,让按钮处理动作事件

b.addActionListener(newActionListener(){

fireEditingStopped();

protectedvoidfireEditingStopped(){

super.fireEditingStopped();

returncurrentColor;

//当颜色改变时,该方法被激活

publicComponentgetTableCellEditorComponent(JTabletable,

Objectvalue,

booleanisSelected,

introw,

intcolumn){

currentColor=(Color)value;

returneditorComponent;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 其它课程

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1