How to set a JTable column as String and sort as Double?

一笑奈何 提交于 2019-12-06 14:22:02

HFOE is right. However, this may be tricky for a Java newbie. Pardon me for using anonymous inner classes. See the following example to get some hints...


package test;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;


public class TableTest {

  public static void main(String[] args) {
    TableTest tt = new TableTest();
    tt.start();
  }

  private void start() {
    JTable t = new JTable(new AbstractTableModel() {
      private static final long serialVersionUID = 1L;
      private List<Double> values = new ArrayList<Double>();
      {
        values.add(Double.valueOf(179.70d));
        values.add(Double.valueOf(299.40d));
        values.add(Double.valueOf(478.80d));
        values.add(Double.valueOf(89.85d));
      }
      @Override
      public String getColumnName(int column) {
        return "Double";
      }
      @Override
      public Class<?> getColumnClass(int column) {
        return Double.class;
      }
      @Override
      public int getRowCount() {
        return values.size();
      }
      @Override
      public int getColumnCount() {
        return 1;
      }
      @Override
      public Object getValueAt(int rowIndex, int columnIndex) {
        return values.get(rowIndex);
      }
    });
    t.setDefaultRenderer(Double.class, new DefaultTableCellRenderer() {
      private static final long serialVersionUID = 1L;
      @Override
      public Component getTableCellRendererComponent(JTable table,
          Object value, boolean isSelected, boolean hasFocus, int row,
          int column) {
        Double d = (Double)value;
        String s = "$" + String.valueOf(d.doubleValue());
        Component c = super.getTableCellRendererComponent(table, s, isSelected, hasFocus,
            row, column);
        return c;
      }
    });
    t.setAutoCreateRowSorter(true);
    JFrame f = new JFrame();
    f.setSize(320, 200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JScrollPane sp = new JScrollPane(t);
    f.getContentPane().add(sp);
    f.setVisible(true);
  }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!