jtbale属性编辑器 JCheckBox显示问题

问题描述

目前在用jtable做一个 属性编辑器,table就二列,一列属性名,一列属性值。 需要根据属性值动态生成编辑器(JComboBox、JCheckBox、JTextArea)。 基本功能已实现 现在存在如下问题: 1.JCheckBox显示没问题,但是点击时位置不正确(会跑到单元格最左边)。2.JTextArea显示有问题,没有一开始就换行显示,要编辑的时候才换行显示。 请大家帮忙看一下,谢谢大家了!  import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Vector;import javax.swing.AbstractCellEditor;import javax.swing.DefaultCellEditor;import javax.swing.DefaultListSelectionModel;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.ListSelectionModel;import javax.swing.WindowConstants;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellEditor;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumnModel;public class PropertyJtable extends JTable {public PropertyJtable(Vector rowData, Vector columnNames) {super(rowData, columnNames);}public JComboBox getJComboBox() {JComboBox comboBox = new JComboBox();comboBox.addItem("red");comboBox.addItem("green");comboBox.addItem("blue");return comboBox;}public static void main(String[] args) {JFrame frame = new JFrame("属性编辑器");frame.setSize(250, 200);frame.setPreferredSize(new Dimension(250, 200));frame.setLayout(new BorderLayout());frame.setLocationRelativeTo(null);Vector title = new Vector();title.add("Property");title.add("Value");JTable jtable = new PropertyJtable(new Vector(), title);JScrollPane scrollPane = new JScrollPane(jtable);DefaultTableModel model = (DefaultTableModel) jtable.getModel();model.addRow(new Object[] { "name", "阿呆" });model.addRow(new Object[] { "是否显示", new Boolean(true) });model.addRow(new Object[] { "颜色1", "red" });model.addRow(new Object[] { "颜色2", "blue" });model.addRow(new Object[] { "年龄", "24" });model.addRow(new Object[] { "备注", "山东分n舵是否公司的n故事啊发放" });jtable.getColumnModel().getColumn(0).setPreferredWidth(50);jtable.getColumnModel().getColumn(1).setPreferredWidth(150);frame.add(scrollPane, BorderLayout.CENTER);JButton printButton = new JButton("关闭");frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);printButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {System.exit(0);}});frame.add(printButton, BorderLayout.SOUTH);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}// 根据值来设置单元格类型public TableCellEditor getCellEditor(int row, int col) {TableCellEditor editor = super.getCellEditor(row, col);// JCheckBoxif ("true".equals(getValueAt(row, col).toString())|| "false".equals(getValueAt(row, col).toString())) {// editor=super.getDefaultEditor(JCheckBox.class);editor = new DefaultCellEditor(new JCheckBox());}// JComboBoxif ("red".equals(getValueAt(row, col).toString())|| "green".equals(getValueAt(row, col).toString())|| "blue".equals(getValueAt(row, col).toString())) {editor = new DefaultCellEditor(getJComboBox());}// 字数大于10 使用JTextAreaif (getValueAt(row, col).toString().length() > 10) {editor = new JTextAreaEditor();}return editor;}// 单元格渲染public TableCellRenderer getCellRenderer(int row, int col) {TableCellRenderer renderer = super.getCellRenderer(row, col);if ("true".equals(getValueAt(row, col).toString())|| "false".equals(getValueAt(row, col).toString())) {renderer = super.getDefaultRenderer(Boolean.class);}return renderer;}// 第一行不可编辑public boolean isCellEditable(int row, int col) {return (col != 0);}/* * 重写方法 单选模式 (non-Javadoc) * * @see javax.swing.JTable#createDefaultSelectionModel() */protected ListSelectionModel createDefaultSelectionModel() {DefaultListSelectionModel defaultListSelectionModel = new DefaultListSelectionModel();defaultListSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);return defaultListSelectionModel;}class JTextAreaEditor extends AbstractCellEditor implements TableCellEditor {protected JTextArea myEditor;public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column) {if (null == myEditor) {myEditor = new JTextArea();}myEditor.setText((String) value);TableColumnModel columnModel = table.getColumnModel();myEditor.setSize(columnModel.getColumn(column).getWidth(), 5);int heightWanted = (int) myEditor.getPreferredSize().getHeight();if (heightWanted != table.getRowHeight(row)) {table.setRowHeight(row, heightWanted);}return myEditor;}@Overridepublic Object getCellEditorValue() {return myEditor.getText();}}} 问题补充:多谢huahuashijie888 的帮助!<br /><br /> JTextArea显示问题已解决,单独写了个渲染器。<br /><br /><br />2.关于jcheckbox跳到左边的问题,我以前也遇到到,好像只要该列值为boolean型,就不需要另写个render或者editor了。。会默认用jcheckbox展示 <br /><br />你说的这个问题 只能针对一列都是jcheckbox情况,我这要的是针对某个单元格来渲染。<br /><pre name="code" class="java">public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}</pre><br /><br />现在我单独写了jcheckbox的Editor和Renderer,显示还是有问题:初始化正常,点击时也正常,不过一选择其他行jcheckbox直接就不见了,貌似变成 JTextField了 还可以录入东西。<br /><br /><br />现在的代码如下:<br /><br /><pre name="code" class="java">import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Vector;import javax.swing.AbstractCellEditor;import javax.swing.DefaultCellEditor;import javax.swing.DefaultListSelectionModel;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ListSelectionModel;import javax.swing.SwingConstants;import javax.swing.WindowConstants;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellEditor;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumnModel;public class PropertyJtable extends JTable {public PropertyJtable(Vector rowData, Vector columnNames) {super(rowData, columnNames);}public JComboBox getJComboBox() {JComboBox comboBox = new JComboBox();comboBox.addItem("red");comboBox.addItem("green");comboBox.addItem("blue");return comboBox;}public static void main(String[] args) {JFrame frame = new JFrame("属性编辑器");frame.setSize(300, 250);frame.setPreferredSize(new Dimension(300, 250));frame.setLayout(new BorderLayout());frame.setLocationRelativeTo(null);Vector title = new Vector();title.add("Property");title.add("Value");JTable jtable = new PropertyJtable(new Vector(), title);JScrollPane scrollPane = new JScrollPane(jtable);DefaultTableModel model = (DefaultTableModel) jtable.getModel();model.addRow(new Object[] { "name", "阿呆" });model.addRow(new Object[] { "是否显示", new Boolean(true) });model.addRow(new Object[] { "颜色1", "red" });model.addRow(new Object[] { "颜色2", "blue" });model.addRow(new Object[] { "年龄", "24" });model.addRow(new Object[] { "备注", "山东分n舵是否公司的n故事啊发放" });jtable.getColumnModel().getColumn(0).setPreferredWidth(50);jtable.getColumnModel().getColumn(1).setPreferredWidth(150);frame.add(scrollPane, BorderLayout.CENTER);JButton printButton = new JButton("关闭");frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);printButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {System.exit(0);}});frame.add(printButton, BorderLayout.SOUTH);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}// 根据值来设置单元格类型public TableCellEditor getCellEditor(int row, int col) {TableCellEditor editor = super.getCellEditor(row, col);// JCheckBoxif ("true".equals(getValueAt(row, col).toString())|| "false".equals(getValueAt(row, col).toString())) {// editor=super.getDefaultEditor(JCheckBox.class);//editor = new DefaultCellEditor(new JCheckBox());editor = new JCheckBoxEditor();}// JComboBoxif ("red".equals(getValueAt(row, col).toString())|| "green".equals(getValueAt(row, col).toString())|| "blue".equals(getValueAt(row, col).toString())) {editor = new DefaultCellEditor(getJComboBox());}// 字数大于10 使用JTextAreaif (getValueAt(row, col).toString().length() &gt; 10) {editor = new JTextAreaEditor();}return editor;}// 单元格渲染public TableCellRenderer getCellRenderer(int row, int col) {TableCellRenderer renderer = super.getCellRenderer(row, col);if ("true".equals(getValueAt(row, col).toString())|| "false".equals(getValueAt(row, col).toString())) {//renderer = super.getDefaultRenderer(Boolean.class);renderer = new JCheckBoxRenderer();}if (getValueAt(row, col).toString().length() &gt; 10) {renderer = new JTextAreaRenderer();}return renderer;}// 第一行不可编辑public boolean isCellEditable(int row, int col) {return (col != 0);}/* * 重写方法 单选模式 (non-Javadoc) * * @see javax.swing.JTable#createDefaultSelectionModel() */protected ListSelectionModel createDefaultSelectionModel() {DefaultListSelectionModel defaultListSelectionModel = new DefaultListSelectionModel();defaultListSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);return defaultListSelectionModel;}class JTextAreaEditor extends AbstractCellEditor implements TableCellEditor {protected JTextArea myEditor;public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column) {if (null == myEditor) {myEditor = new JTextArea();}myEditor.setText((String) value);TableColumnModel columnModel = table.getColumnModel();myEditor.setSize(columnModel.getColumn(column).getWidth(), 5);int heightWanted = (int) myEditor.getPreferredSize().getHeight();if (heightWanted != table.getRowHeight(row)) {table.setRowHeight(row, heightWanted);}return myEditor;}@Overridepublic Object getCellEditorValue() {return myEditor.getText();}}class JTextAreaRenderer extends JTextArea implements TableCellRenderer {public JTextAreaRenderer() { setLineWrap(true); setWrapStyleWord(true); } public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) { // 计算当下行的最佳高度 int maxPreferredHeight = 0; for (int i = 0; i &lt; table.getColumnCount(); i++) { setText("" + table.getValueAt(row, i)); setSize(table.getColumnModel().getColumn(column).getWidth(), 5); maxPreferredHeight = Math.max(maxPreferredHeight, getPreferredSize().height); } if (table.getRowHeight(row) != maxPreferredHeight) // 少了这行则不能重绘 table.setRowHeight(row, maxPreferredHeight); setText(value == null ? "" : value.toString()); return this; } } class JCheckBoxEditor extends AbstractCellEditor implements TableCellEditor {protected JCheckBox myEditor;public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column) {if (null == myEditor) {myEditor = new JCheckBox();}boolean selected = false; if (value instanceof Boolean) { selected = ((Boolean)value).booleanValue();}else if (value instanceof String) { selected = value.equals("true");}myEditor.setSelected(selected);myEditor.setHorizontalAlignment(SwingConstants.CENTER);return myEditor;}@Overridepublic Object getCellEditorValue() {return myEditor.getText();}}class JCheckBoxRenderer extends JCheckBox implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) { boolean selected = false; if (value instanceof Boolean) { selected = ((Boolean)value).booleanValue();}else if (value instanceof String) { selected = value.equals("true");}this.setSelected(selected); this.setHorizontalAlignment(SwingConstants.CENTER); return this; } }}</pre>

解决方案

1.显示的时候你要自适应显示宽度高度,还需要对该列写个render,你写的render只是显示一个textarea组件,具体可以参考我写的class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer { public TableCellTextAreaRenderer() { setLineWrap(true); setWrapStyleWord(true); } public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) { // 计算当下行的最佳高度 int maxPreferredHeight = 0; for (int i = 0; i < table.getColumnCount(); i++) { setText("" + table.getValueAt(row, i)); setSize(table.getColumnModel().getColumn(column).getWidth(), 0); maxPreferredHeight = Math.max(maxPreferredHeight, getPreferredSize().height); } if (table.getRowHeight(row) != maxPreferredHeight) // 少了这行则不能重绘 table.setRowHeight(row, maxPreferredHeight); setText(value == null ? "" : value.toString()); return this; }}2.关于jcheckbox跳到左边的问题,我以前也遇到到,好像只要该列值为boolean型,就不需要另写个render或者editor了。。会默认用jcheckbox展示

时间: 2024-11-07 11:19:36

jtbale属性编辑器 JCheckBox显示问题的相关文章

springMVC4(9)属性编辑器剖析入参类型转换原理

我们通过Http请求提交的参数都以字符串的形式呈现,但最终在springMVC的方法入参中,我们却能得到各种类型的数据,包括Number.Boolean.复杂对象类型.集合类型.Map类型等,这些都是springMVC内置的数据类型转换器帮我们完成的.springMVC的将请求数据绑定到方法入参的流程如下所示: Created with Raphaël 2.1.0数据绑定流程图解ServletRequestServletRequestDataBinderDataBinderConversionS

WinForm控件开发总结(九) 为属性提下拉式属性编辑器

在上一篇文章,我介绍了如何编写模态对话框属性编辑器,这篇文章我将介绍如何编写下拉式属性编 辑器.下拉式(DropDown)属性编辑器和模态对话框属性编辑器的不同之处就是,当你点击属性值修改的 时候,模态对话框编辑器是弹出一个模态对话框,而下拉式属性编辑器却是在紧贴着属性值的地方显示一 个下拉的控件.不知道大家注意到了没有,这里我说的是显示一个下拉的控件,而这个控件也是需要你去 开发的,接下来我还是以Scope属性为例,介绍一下具体的实现. 首先我们要创建一个用于编辑属性的控件,在本系列文章的开始

wi-fi-本地连接属性里共享显示 wmi信息损害

问题描述 本地连接属性里共享显示 wmi信息损害 本地连接属性里共享显示 wmi信息损害我想用电脑wifi发射器,这样怎么设置网络共享啊??? 系统是win7 64位的 解决方案 wmi服务有问题了,所有网卡这个属性页查询不到数据. 估计要修复,或重装系统了.

visual studio-vs2013 工具 选项 文本编辑器内容显示不全是怎么回事啊

问题描述 vs2013 工具 选项 文本编辑器内容显示不全是怎么回事啊 ,vs2013 工具 选项 文本编辑器内容显示不全是怎么回事啊 解决方案 你安装完整版本的vs2013了么?建议修复安装下.

js-关于JS重复更改某标签属性,只显示最后一次更改内容

问题描述 关于JS重复更改某标签属性,只显示最后一次更改内容 嗯,我从Handler里读出Json: [{Num:2,Color:'Red'},Num:2,Color:'Yellow'},Num:2,Color:'Bule'},] 循环Json,读取颜色,把相关的div背景色改成颜色. 然而循环的话,div只会变成蓝色. 我想变成红色几秒,再变成黄色几秒,最后蓝色.请问有什么好方法吗? 解决方案 <div id='divcolor' >测试颜色</div> </body>

iis7编辑器正常显示输入,无法保存发布

问题描述 iis7编辑器正常显示输入,无法保存发布 本地没有问题,发布到服务器后 就出现编辑器 正常可以显示.编辑. 但拷贝的内容无法 发布成功,输入简单的数字.汉字发布可以保存成功...什么原因呀 各位大神!! 解决方案 去掉安全限制,在网页头部加 百度关键字.net可以提交html 解决方案二: 有可能是文件不完整导致的 也有可能是权限问题 检查下代码 如果找不出问题 网上下个DEMO 重新写一遍 解决方案三: 有可能是文件不完整导致的 也有可能是权限问题 检查下代码 如果找不出问题 网上下

bean-spring4.0.6 注入Date类型时,属性编辑器无效

问题描述 spring4.0.6 注入Date类型时,属性编辑器无效 这是MyDate类 package org.xjs.pojo; import java.util.Date; public class MyDate { private Date date; public Date getDate() { return this.date; } public void setDate(Date date) { this.date = date; } } 这是自定义属性编辑器类 package

c++ vc-在dc中画线,设置了Cpen画笔的属性后只能显示画笔宽度不能显示颜色

问题描述 在dc中画线,设置了Cpen画笔的属性后只能显示画笔宽度不能显示颜色 sdc.CreateCompatibleDC(NULL); bm.CreateCompatibleBitmap(&sdc,width,height); sdc.SelectObject(&bm); sdc.FillSolidRect(0,0,width,height,RGB(0,255,255)); pNewPen2.CreatePen(PS_SOLID ,2, RGB(255,0,0)); sdc.Selec

discuz二次开发调用编辑器发帖显示html何解?

discuz的开发文档很少,在二次开发的时候用discuz的编辑器,显示的是html代码,查看源码和各种莫名其妙的问题着实令人摸不着头脑.而discuz帖子存储在数据库的却是自定义的bbcode而不是正常的html代码.这也就导致直接调用discuz的编辑器发帖显示出来的帖子成了html源码. 网上找了很久也没找到相应的问题,最后通过数据库的字段对比才知道问题的所在. 1.问题所在 调用编辑器编辑的源码 顶顶顶顶顶<b><i>的顶顶顶顶顶</i></b>顶顶