View unanswered posts | View active topics It is currently 2022-07-06 8:37



This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
 再问TTable中添加JComboBox的问题 
Author Message
新手上路

Joined: 2014-04-14 22:07
Posts: 28
Post 再问TTable中添加JComboBox的问题
谢谢。提供的代码实现了功能。但是在自己尝试定义一个JComboBox的列和一个JButton的列却出现了问题。双击JButton时,JButton消失,单击JComboBox下拉项不显示,还请大神帮忙。另外:上面功能是用TElementTable实现的,由于用户手册主要讲如何使用TTable,所有我们也主要使用的是TTable,
目前对TElementTable了解得很少,请问可以用TTable实现吗?

下面是我们在原代码的基础上修改后的代码
package test2;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;

import twaver.ElementAttribute;
import twaver.EnumTypeManager;
import twaver.Node;
import twaver.TWaverConst;
import twaver.table.TElementTable;
import twaver.table.editor.EnumTypeEditor;

public class TestTableRenderer extends JFrame
{
private static final long serialVersionUID = 1L;

TElementTable table = new TElementTable();

public TestTableRenderer()
{
this.add(new JScrollPane(table));
this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

table.setEditable(false);
table.setRowHeight(23);

initBox();

initTable();
}

private void initTable()
{
ArrayList attributes = new ArrayList();

ElementAttribute seqAttr = createClientProperty("1","序号",60);
seqAttr.setEditable(false);
attributes.add(seqAttr);

ElementAttribute attribute = createClientProperty("2", "形状", 100);
attribute.setRendererClass(JComboBoxRenderer.class.getName());
attribute.setEditorClass(EnumTypeEditor.class.getName() + "@" + TWaverConst.ENUM_GROUPTYPE);
attributes.add(attribute);

ElementAttribute tsAttr = createClientProperty("3", "时隙", 100);
tsAttr.setRendererClass(MyJComboBoxRenderer.class.getName());
tsAttr.setEditorClass(EnumTypeEditor.class.getName() + "@" + MyJComboBoxRenderer.class.getName());

attributes.add(tsAttr);



ElementAttribute timeAttr = createClientProperty("4", "时间", 150);
timeAttr.setEditable(false);
attributes.add(timeAttr);

ElementAttribute checkAttr = createClientProperty("5", "选择", 80);
checkAttr.setRendererClass(JButtonRenderer.class.getName());
attributes.add(checkAttr);

table.registerElementClassAttributes(Node.class, attributes);
table.setElementClass(Node.class);
table.setEditable(true);

// table.getColumnByIndex(1).setEditable(false);
}

private void initBox()
{
for (int i = 0; i < 10; i++)
{
Node node = new Node();
node.putClientProperty("1", (i+1));
node.putClientProperty("2", i % 2 == 0 ? TWaverConst.GROUP_TYPE_RECTANGLE : TWaverConst.GROUP_TYPE_ELLIPSE);
node.putClientProperty("3", i % 2 == 0 ? 1 : 2);
node.putClientProperty("4", currentTime());
table.getDataBox().addElement(node);
}
}
private String currentTime()
{
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
}
static public ElementAttribute createClientProperty(String key, String displayName, int width)
{
ElementAttribute attribute = new ElementAttribute();
attribute.setClientPropertyKey(key);
attribute.setDisplayName(displayName);
attribute.setWidth(width);
return attribute;
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TestTableRenderer().setVisible(true);
}
});
}

public static class JComboBoxRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
private JComboBox delegate = new JComboBox(EnumTypeManager.getInstance().getEnumTypes(TWaverConst.ENUM_GROUPTYPE));

public JComboBoxRenderer()
{
super();
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
delegate.setSelectedItem(EnumTypeManager.getInstance().getEnumType(TWaverConst.ENUM_GROUPTYPE, value));
return delegate;
}
}

public static class MyJComboBoxRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
private final String[] items = {"1","2","3","4"};
private JComboBox delegate = new JComboBox(items);

public MyJComboBoxRenderer()
{
super();
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
delegate.setSelectedItem(items[(int)value-1]);
return delegate;
}
}

public static class JButtonRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
private JButton delegate = new JButton("OK");

public JButtonRenderer()
{
super();

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
delegate.setSize(70, 20);
delegate.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0)
{
String msg = "clicked row:"+row+"column:"+column+"seled time:"+table.getValueAt(row, 3);
System.err.println(msg);
JOptionPane.showMessageDialog(table, msg);
}
});
return delegate;
}
}
}


2015-07-30 22:03
Profile
TWaver开发组
User avatar

Joined: 2009-04-20 18:37
Posts: 3115
Post Re: 再问TTable中添加JComboBox的问题
建议直接使用更易用的TElementTable,建议您再学习一下表格Renderer和Editor的概念。
Renderer里的JButton添加点击动作没用的,需要用Editor,Renderer就像个邮戳,只用于绘制,点击单元格开始编辑后,editor会放在renderer的地方,用于编辑。
https://docs.oracle.com/javase/tutorial ... table.html
Code:
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

import twaver.ElementAttribute;
import twaver.EnumType;
import twaver.EnumTypeManager;
import twaver.Node;
import twaver.TWaverConst;
import twaver.table.TElementTable;
import twaver.table.editor.EnumTypeEditor;

public class TestTableRenderer extends JFrame {
   private static final long serialVersionUID = 1L;

   TElementTable table = new TElementTable();
   
   static {
      EnumTypeManager.getInstance().registerEnumTypes("test", new EnumType[] {
            new EnumType(1, "1"),
            new EnumType(2, "2"),
            new EnumType(3, "3"),
            new EnumType(4, "4"),
         });
   }

   public TestTableRenderer() {
      this.add(new JScrollPane(table));
      this.setSize(800, 600);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      table.setEditable(false);
      table.setRowHeight(23);

      initBox();

      initTable();
   }

   private void initTable() {
      ArrayList attributes = new ArrayList();

      ElementAttribute seqAttr = createClientProperty("1", "序号", 60);
      seqAttr.setEditable(false);
      attributes.add(seqAttr);

      ElementAttribute attribute = createClientProperty("2", "形状", 100);
      attribute.setRendererClass(JComboBoxRenderer.class.getName());
      attribute.setEditorClass(EnumTypeEditor.class.getName() + "@" + TWaverConst.ENUM_GROUPTYPE);
      attributes.add(attribute);

      ElementAttribute tsAttr = createClientProperty("3", "时隙", 100);
      tsAttr.setRendererClass(MyJComboBoxRenderer.class.getName());
      tsAttr.setEditorClass(EnumTypeEditor.class.getName() + "@test");

      attributes.add(tsAttr);

      ElementAttribute timeAttr = createClientProperty("4", "时间", 150);
      timeAttr.setEditable(false);
      attributes.add(timeAttr);

      ElementAttribute checkAttr = createClientProperty("5", "选择", 80);
      checkAttr.setRendererClass(JButtonRenderer.class.getName());
      checkAttr.setEditorClass(JButtonEditor.class.getName());
      attributes.add(checkAttr);

      table.registerElementClassAttributes(Node.class, attributes);
      table.setElementClass(Node.class);
      table.setEditable(true);

      //   table.getColumnByIndex(1).setEditable(false);
   }

   private void initBox() {
      for (int i = 0; i < 10; i++) {
         Node node = new Node();
         node.putClientProperty("1", (i + 1));
         node.putClientProperty("2", i % 2 == 0 ? TWaverConst.GROUP_TYPE_RECTANGLE : TWaverConst.GROUP_TYPE_ELLIPSE);
         node.putClientProperty("3", i % 2 == 0 ? 1 : 2);
         node.putClientProperty("4", currentTime());
         table.getDataBox().addElement(node);
      }
   }

   private String currentTime() {
      return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
   }

   static public ElementAttribute createClientProperty(String key, String displayName, int width) {
      ElementAttribute attribute = new ElementAttribute();
      attribute.setClientPropertyKey(key);
      attribute.setDisplayName(displayName);
      attribute.setWidth(width);
      return attribute;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TestTableRenderer().setVisible(true);
         }
      });
   }

   public static class JComboBoxRenderer extends DefaultTableCellRenderer {
      private static final long serialVersionUID = 1L;
      private JComboBox delegate = new JComboBox(EnumTypeManager.getInstance().getEnumTypes(TWaverConst.ENUM_GROUPTYPE));

      public JComboBoxRenderer() {
         super();
      }

      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         delegate.setSelectedItem(EnumTypeManager.getInstance().getEnumType(TWaverConst.ENUM_GROUPTYPE, value));
         return delegate;
      }
   }

   public static class MyJComboBoxRenderer extends DefaultTableCellRenderer {
      private static final long serialVersionUID = 1L;
      private JComboBox delegate = new JComboBox(EnumTypeManager.getInstance().getEnumTypes("test"));

      public MyJComboBoxRenderer() {
         super();
      }

      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         delegate.setSelectedItem(EnumTypeManager.getInstance().getEnumType("test", value));
         return delegate;
      }
   }

   public static class JButtonRenderer extends DefaultTableCellRenderer {
      private static final long serialVersionUID = 1L;
      private JButton delegate = new JButton("OK");

      public JButtonRenderer() {
         super();

      }

      public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, final int column) {
         delegate.setSize(70, 20);
      }
   }
   
   public static class JButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
      private JTable table;
      private Object value;
      private JButton button;
      private int row;
      private int column;
      
      public JButtonEditor() {
           button = new JButton("OK");
           button.addActionListener(this);
      }
      
      public void actionPerformed(ActionEvent e) {
         System.out.println(value);
      }
      
      public Object getCellEditorValue() {
           return value;
       }

       public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
          this.table = table;
           this.value = value;
           this.row = row;
           this.column = column;
          
           return button;

       }
   }
}


2015-07-31 14:29
Profile
新手上路

Joined: 2014-04-14 22:07
Posts: 28
Post Re: 再问TTable中添加JComboBox的问题
非常感谢,上面的代码满足了我们的需要。
还想问一下,原来在TTable中,在单元格中加入JCheckBox很方便,直接使用TWaver提供的BooleanRenderer就可以了,现在在TElementTable中,我试图直接使用setRenderClass(BooleanRenderer.class.getName()),setEditorClass(BooleanEditor.class.getName()),但是要点击一下单元格才出现JCheckBox,如何在开始就显示JCheckBox呢?谢谢!


2015-08-04 12:50
Profile
TWaver开发组
User avatar

Joined: 2009-04-20 18:37
Posts: 3115
Post Re: 再问TTable中添加JComboBox的问题
可能是你没有设置true或者false值造成的,必须设置初始化值,要不然就是个空白:
Code:
/*import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;

import twaver.ElementAttribute;
import twaver.EnumTypeManager;
import twaver.Node;
import twaver.TWaverConst;
import twaver.table.TElementTable;
import twaver.table.editor.EnumTypeEditor;


public class TestTableRenderer extends JFrame {
   TElementTable table = new TElementTable();

   public TestTableRenderer() {
      this.add(new JScrollPane(table));
      this.setSize(800, 600);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      table.setEditable(false);
      initBox();
      initTable();
   }
   
   private void initTable() {
      ArrayList attributes = new ArrayList();
      ElementAttribute attribute = createClientProperty("a", "a", false);
      attribute.setRendererClass(JComboBoxRenderer.class.getName());
      attribute.setEditorClass(EnumTypeEditor.class.getName()+"@"+TWaverConst.ENUM_GROUPTYPE);
      attributes.add(attribute);
      attributes.add(createClientProperty("b", "b", true));
      table.registerElementClassAttributes(Node.class, attributes);
      table.setElementClass(Node.class);
      table.setEditable(true);
   }
   
   private void initBox() {
      for(int i=0; i<10; i++){
         Node node = new Node();
         node.putClientProperty("a", i % 2 == 0 ? TWaverConst.GROUP_TYPE_RECTANGLE : TWaverConst.GROUP_TYPE_ELLIPSE);
         node.putClientProperty("b", "bb"+i);
         table.getDataBox().addElement(node);
      }
   }
   
   static public ElementAttribute createClientProperty(String key, String displayName, boolean extra) {
      ElementAttribute attribute = new ElementAttribute();
      attribute.setClientPropertyKey(key);
      attribute.setDisplayName(displayName);
      attribute.setExtraWidthAssignable(extra);
      if(extra){
         attribute.setMaxPackWidth(10);
         attribute.setMinPackWidth(10);
      }
      return attribute;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TestTableRenderer().setVisible(true);
         }
      });
   }

   public static class JComboBoxRenderer extends DefaultTableCellRenderer {
      
      private JComboBox delegate = new JComboBox(EnumTypeManager.getInstance().getEnumTypes(TWaverConst.ENUM_GROUPTYPE));
      
      public JComboBoxRenderer () {
         super();
      }

       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
          delegate.setSelectedItem(EnumTypeManager.getInstance().getEnumType(TWaverConst.ENUM_GROUPTYPE, value));
          return delegate;
       }
   }
}
*/

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

import twaver.ElementAttribute;
import twaver.EnumType;
import twaver.EnumTypeManager;
import twaver.Node;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.table.TElementTable;
import twaver.table.editor.BooleanEditor;
import twaver.table.editor.EnumTypeEditor;
import twaver.table.renderer.BooleanRenderer;

public class TestTableRenderer extends JFrame {
   private static final long serialVersionUID = 1L;

   TElementTable table = new TElementTable();
   
   static {
      EnumTypeManager.getInstance().registerEnumTypes("test", new EnumType[] {
            new EnumType(1, "1"),
            new EnumType(2, "2"),
            new EnumType(3, "3"),
            new EnumType(4, "4"),
         });
   }

   public TestTableRenderer() {
      this.add(new JScrollPane(table));
      this.setSize(800, 600);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      table.setEditable(false);
      table.setRowHeight(23);

      initBox();

      initTable();
   }

   private void initTable() {
      ArrayList attributes = new ArrayList();

      ElementAttribute seqAttr = createClientProperty("1", "序号", 60);
      seqAttr.setEditable(false);
      attributes.add(seqAttr);

      ElementAttribute attribute = createClientProperty("2", "形状", 100);
      attribute.setRendererClass(JComboBoxRenderer.class.getName());
      attribute.setEditorClass(EnumTypeEditor.class.getName() + "@" + TWaverConst.ENUM_GROUPTYPE);
      attributes.add(attribute);

      ElementAttribute tsAttr = createClientProperty("3", "时隙", 100);
      tsAttr.setRendererClass(MyJComboBoxRenderer.class.getName());
      tsAttr.setEditorClass(EnumTypeEditor.class.getName() + "@test");

      attributes.add(tsAttr);

      ElementAttribute timeAttr = createClientProperty("4", "时间", 150);
      timeAttr.setEditable(false);
      attributes.add(timeAttr);

      ElementAttribute checkAttr = createClientProperty("5", "选择", 80);
      checkAttr.setRendererClass(JButtonRenderer.class.getName());
      checkAttr.setEditorClass(JButtonEditor.class.getName());
      attributes.add(checkAttr);

      ElementAttribute attr = createClientProperty("6", "check", 80);
      attr.setRendererClass(BooleanRenderer.class.getName());
      attr.setEditorClass(BooleanEditor.class.getName());
      attributes.add(attr);

      table.registerElementClassAttributes(Node.class, attributes);
      table.setElementClass(Node.class);
      table.setEditable(true);

      //   table.getColumnByIndex(1).setEditable(false);
   }

   private void initBox() {
      for (int i = 0; i < 10; i++) {
         Node node = new Node();
         node.putClientProperty("1", (i + 1));
         node.putClientProperty("2", i % 2 == 0 ? TWaverConst.GROUP_TYPE_RECTANGLE : TWaverConst.GROUP_TYPE_ELLIPSE);
         node.putClientProperty("3", i % 2 == 0 ? 1 : 2);
         node.putClientProperty("4", currentTime());
         node.putClientProperty("6", TWaverUtil.getRandomBool());
         table.getDataBox().addElement(node);
      }
   }

   private String currentTime() {
      return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
   }

   static public ElementAttribute createClientProperty(String key, String displayName, int width) {
      ElementAttribute attribute = new ElementAttribute();
      attribute.setClientPropertyKey(key);
      attribute.setDisplayName(displayName);
      attribute.setWidth(width);
      return attribute;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TestTableRenderer().setVisible(true);
         }
      });
   }

   public static class JComboBoxRenderer extends DefaultTableCellRenderer {
      private static final long serialVersionUID = 1L;
      private JComboBox delegate = new JComboBox(EnumTypeManager.getInstance().getEnumTypes(TWaverConst.ENUM_GROUPTYPE));

      public JComboBoxRenderer() {
         super();
      }

      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         delegate.setSelectedItem(EnumTypeManager.getInstance().getEnumType(TWaverConst.ENUM_GROUPTYPE, value));
         return delegate;
      }
   }

   public static class MyJComboBoxRenderer extends DefaultTableCellRenderer {
      private static final long serialVersionUID = 1L;
      private JComboBox delegate = new JComboBox(EnumTypeManager.getInstance().getEnumTypes("test"));

      public MyJComboBoxRenderer() {
         super();
      }

      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         delegate.setSelectedItem(EnumTypeManager.getInstance().getEnumType("test", value));
         return delegate;
      }
   }

   public static class JButtonRenderer extends DefaultTableCellRenderer {
      private static final long serialVersionUID = 1L;
      private JButton delegate = new JButton("OK");

      public JButtonRenderer() {
         super();

      }

      public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, final int column) {
         delegate.setSize(70, 20);
         delegate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
               String msg = "clicked row:" + row + "column:" + column + "seled time:" + table.getValueAt(row, 3);
               System.err.println(msg);
               JOptionPane.showMessageDialog(table, msg);
            }
         });
         return delegate;
      }
   }
   
   public static class JButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
      private JTable table;
      private Object value;
      private JButton button;
      private int row;
      private int column;
      
      public JButtonEditor() {
           button = new JButton("OK");
           button.addActionListener(this);
      }
      
      public void actionPerformed(ActionEvent e) {
         System.out.println(value);
      }
      
      public Object getCellEditorValue() {
           return value;
       }

       public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
          this.table = table;
           this.value = value;
           this.row = row;
           this.column = column;
          
           return button;

       }
   }
}

Attachment:
Screen Shot 2015-08-07 at 3.02.12 PM.png
Screen Shot 2015-08-07 at 3.02.12 PM.png [ 52.56 KiB | Viewed 17368 times ]


2015-08-07 15:01
Profile
Display posts from previous:  Sort by  
This topic is locked, you cannot edit posts or make further replies.   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 50 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron