不能在JTable中返回Double Value

时间:2014-10-16 03:42:35

标签: java swing jtable double defaulttablemodel

我正在创建一个ui,其中我从文本文件中读取数据并在用户编辑后将其保存到另一个文本文件。我想为除第6列和第7列之外的所有列返回整数值。对于第6列和7我希望返回双值。当用户在其中编辑时,此程序中的所有内容仅适用于第6列和第7列,直到它们输入整数值,它在单元格中显示红色标记,而我应该是第6列和第3列的双值7.请帮助

代码:

 import java.awt.*;
 import java.awt.event.*;
 import java.io.*;
 import java.util.*;
 import javax.swing.*;
 import javax.swing.table.*;

 public class Read extends JFrame
 {
 private JTable table;
 private DefaultTableModel model;

 @SuppressWarnings("unchecked")
 public Read()
 {

      String aLine ;
      Vector columnNames = new Vector();
      Vector data = new Vector();

      try
      {
           FileInputStream fin = new FileInputStream("test1.txt");
           BufferedReader br = new BufferedReader(new InputStreamReader(fin));


           StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");

           while( st1.hasMoreTokens() )
           {
                columnNames.addElement(st1.nextToken());
           }


           while ((aLine = br.readLine()) != null)
           {
                StringTokenizer st2 = new StringTokenizer(aLine, " ");
                Vector row = new Vector();

                while(st2.hasMoreTokens())
                {
                     row.addElement(st2.nextToken());
                }
                data.addElement( row );
           }
           br.close();
      }
      catch (Exception e)
      {
           e.printStackTrace();
      }

      final JTable table = new JTable(new DefaultTableModel(data, columnNames){
      private static final long serialVersionUID = 1L;

  @Override
  public Class getColumnClass(int column) {
       return Integer.class;
       }
  });


  JScrollPane scrollPane = new JScrollPane( table );
      getContentPane().add( scrollPane );


      JPanel buttonPanel = new JPanel();
      getContentPane().add( buttonPanel, BorderLayout.SOUTH );


      JButton button1 = new JButton( "Save" );
      buttonPanel.add( button1 );
      button1.addActionListener( new ActionListener()
      {
           public void actionPerformed(ActionEvent e)
           {
               if  ( table.isEditing() )
                {
                     int row = table.getEditingRow();
                     int col = table.getEditingColumn();
                     table.getCellEditor(row, col).stopCellEditing();
                }

                int rows = table.getRowCount();
                int columns = table.getColumnCount();



     try { 
       StringBuilder con = new StringBuilder();
       for (int i = 0; i < rows; i++)
        {
          for (int j = 0; j < columns; j++)
           {
              Object Value = table.getValueAt (i, j);
              con.append(" ");
              con.append(Value);
           }
          con.append("\r\n");
        }

    FileWriter fileWriter = new FileWriter(new File("new.txt"));

    fileWriter.write(con.toString());
    fileWriter.flush();

   }  catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }
   });

  }

 public static void main(String[] args)
 {
      Read a = new Read();
      a.setDefaultCloseOperation( EXIT_ON_CLOSE );
      a.pack();
      a.setVisible(true);
 }
}

文本文件

1 2 3 4 5 6 7 8
78 12 12 45 4 0.0045 0.0078 45
45 45 69 56 7 0.0056 0.0023 21
45 89 76 42 1 0.0036 0.0023 36

1 个答案:

答案 0 :(得分:4)

TableModel#getColumnClass用于确定JTable应使用哪个渲染器和哪个编辑器。

当您使用Integer.class作为列类时,JTable会将JFormattedField设置为仅允许接受整数。您需要修改getColumnClass以返回给定列的正确数据类型,例如......

@Override
public Class getColumnClass(int column) {
    return column == 5 || column == 6 ? Double.class : Integer.class;
}

您还应该确保输入模型的数据能够满足此合同,例如......

while (st2.hasMoreTokens()) {
    Object num = null;
    String value = st2.nextToken();
    Number num = NumberFormat.getNumberInstance().parse(value);
    row.addElement(num);
}