jTable中的jFormattedText

时间:2014-02-03 01:30:29

标签: java swing jtable tablecelleditor jformattedtextfield

我一直在尝试在jTable中插入一个jFormatted Text。

然而,到目前为止,我将此作为输出。

javax.swing.JFormattedTextField中[,0,0,0x0,无效,布局= javax.swing.plaf.basic.BasicTextUI中$ UpdateHandler,alignmentX = 0.0,alignmentY = 0.0,边界= com.sun.java .swing.plaf.windows.XPStyle $ XPFillBorder @ 12bd4a6,旗帜= 296,MAXIMUMSIZE =,=的minimumSize,首选大小=,caretColor = javax.swing.plaf.ColorUIResource [R = 0,G = 0,b = 0],disabledTextColor = javax.swing.plaf.ColorUIResource [R = 109,G = 109,b = 109],可编辑=真,裕度= javax.swing.plaf.InsetsUIResource [顶= 2,左= 2,底= 2,右= 2],selectedTextColor = javax.swing.plaf.ColorUIResource [R = 255,G = 255,b = 255],selectionColor设置= javax.swing.plaf.ColorUIResource [R = 51,G = 153,b = 255],列= 0,columnWidth = 0,command =,horizo​​ntalAlignment = LEADING] 20

我使用的代码如下:

    DefaultTableModel dm = (DefaultTableModel) jTable1.getModel();  
    MaskFormatter mf1;
   try {
       mf1 = new MaskFormatter("*##-##-####-*****");

    JFormattedTextField abc1 = new JFormattedTextField(mf1);



  //        String header[] = new String[]{"ACCOUNT NUMBER","Col2","AMOUNT PAYABLE"};  
  //        dm.setColumnIdentifiers(header);  
  //        jTable1.setModel(dm);


    //TableColumn abc7 = jTable1.getColumnModel().getColumn(0);
    //abc1.setValue("");
    Vector<Object> data = new Vector<Object>();  

    data.add(abc1);  
    data.add(20);  
    data.add(false);  

    dm.addRow(data);  

     TableColumn abc2 = jTable1.getColumnModel().getColumn(0);
    jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(abc1));
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setToolTipText("Click to change the values");
    DefaultCellEditor defaultCellEditor=new DefaultCellEditor(abc1);
    abc2.setCellEditor(defaultCellEditor);
    abc2.setCellRenderer(renderer);
    jTable1.setIntercellSpacing(new Dimension(10, 4));
    jTable1.repaint();

    //abc7.setCellEditor(new DefaultCellEditor(abc1));
   // jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(abc1));

   } catch (Exception ex) {
       java.util.logging.Logger.getLogger(Multiple_Utility_Bill.class.getName()).log(Level.SEVERE, null, ex);
       log.debug(ex);
   }

有人可以教我如何将格式化文本字段输入jTable吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

无需在表格中添加JFormattedTextField。只需在表格模型中添加要在JFormattedTextField

中表示的值

您应该实现自己的TableCellRenderer,它应该从方法中返回JFormattedTextField组件

Component getTableCellRendererComponent(JTable table, Object value,
                                        boolean isSelected, boolean hasFocus,
                                        int row, int column);

然后将渲染器设置为您的表格。