JTable怎样在双精度列类中显示数字,以(#.00)格式显示?

huangapple go评论135阅读模式
英文:

JTable how can i show numbers as (#.00) format in double column class?

问题

我的 JTable 模型

DefaultTableModel modelim = new DefaultTableModel() {
    public Class getColumnClass(int column) {
        switch (column) {
            case 0:
                return Integer.class;
            case 1:
            case 2:
            case 3:
                return String.class;
            case 4:
                return Date.class;
            case 5:
                return Double.class;
            default:
                return String.class;
        }
    }
};

我有一个 JTable。第 5 列是用于双精度数的。例如,

1234.25
177.12
1745.02
1748.10
1899.00

这是第 5 列的项目。但当我启动程序时,它们看起来像这样,

1234.25
177.12
1745.02
1748.1 //不同
1899.0 //不同

所以它至少显示小数点后的一个数字。但我想看到小数点后有 2 个数字,

1748.10
1899.00

像这样。如果你们需要,我可以分享更多代码。

英文:

My JTable model

DefaultTableModel modelim = new DefaultTableModel() {
			public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return Integer.class;
                    case 1:
                        return String.class;
                    case 2:
                        return String.class;
                    case 3:
                        return String.class;
                    case 4:
                        return Date.class;
                    case 5:
                        return Double.class;
                    default:
                        return String.class;
                }
            }
		};

I have a JTable. Column 5 is for double numbers. For example.

1234.25
177.12
1745.02
1748.10
1899.00

This is the items of column 5. But when i start the program they look like this.

1234.25
177.12
1745.02
1748.1 //different
1899.0 //different

So it shows atleast 1 number after the dot. But i want to see the number with 2 nubmers after the dot.

1748.10
1899.00

As this. I can share more code if you guys need.

答案1

得分: 2

使用自定义单元格渲染器。请参阅如何使用表格使用自定义渲染器

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.text.NumberFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JTable table = new JTable();
            DefaultTableModel modelim = new DefaultTableModel() {
                public Class getColumnClass(int column) {
                    switch (column) {
                        case 0:
                            return Integer.class;
                        case 1:
                            return String.class;
                        case 2:
                            return String.class;
                        case 3:
                            return String.class;
                        case 4:
                            return Date.class;
                        case 5:
                            return Double.class;
                        default:
                            return String.class;
                    }
                }

                @Override
                public int getColumnCount() {
                    return 6;
                }
            };

            modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 1.234567});
            modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 2.345678});
            modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 3.456789});
            modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 4.567890});
            modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 5.678901});
            modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 6.789012});

            table.setAutoCreateRowSorter(true);
            table.setModel(modelim);
            table.getColumnModel().getColumn(5).setCellRenderer(new DoubleFormatTableCellRenderer());

            add(new JScrollPane(table));
        }

    }

    public class DoubleFormatTableCellRenderer extends DefaultTableCellRenderer {

        private NumberFormat numberFormat;

        public NumberFormat getNumberFormat() {
            if (numberFormat == null) {
                numberFormat = NumberFormat.getNumberInstance();
                numberFormat.setMinimumFractionDigits(2);
                numberFormat.setMaximumFractionDigits(2);
            }
            return numberFormat;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value instanceof Double) {
                value = getNumberFormat().format(value);
            } 

            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }

    }
}
英文:

Use a custom cell renderer. See How to Use Tables and Using Custom Renderers

JTable怎样在双精度列类中显示数字,以(#.00)格式显示?
JTable怎样在双精度列类中显示数字,以(#.00)格式显示?

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.text.NumberFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTable table = new JTable();
DefaultTableModel modelim = new DefaultTableModel() {
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Integer.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
case 4:
return Date.class;
case 5:
return Double.class;
default:
return String.class;
}
}
@Override
public int getColumnCount() {
return 6;
}
};
modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 1.234567});
modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 2.345678});
modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 3.456789});
modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 4.567890});
modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 5.678901});
modelim.addRow(new Object[] {1, "1", "1", "1", new Date(), 6.789012});
table.setAutoCreateRowSorter(true);
table.setModel(modelim);
table.getColumnModel().getColumn(5).setCellRenderer(new DoubleFormatTableCellRenderer());
add(new JScrollPane(table));
}
}
public class DoubleFormatTableCellRenderer extends DefaultTableCellRenderer {
private NumberFormat numberFormat;
public NumberFormat getNumberFormat() {
if (numberFormat == null) {
numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMinimumFractionDigits(2);
numberFormat.setMaximumFractionDigits(2);
}
return numberFormat;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Double) {
value = getNumberFormat().format(value);
} 
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
}

> i do use one. It was ok but when i try to sort the numbers i does not sort right. So i hade to use Double column class to make them sort right. But after i use Double column class the numbers became as this

Next time, provide a minimal reproducible example - it will prevent use from running over ground you might have already gone and/or see why your solution didn't work

huangapple
  • 本文由 发表于 2020年8月24日 17:38:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63558396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定