<bdo id='lYasH'></bdo><ul id='lYasH'></ul>

  1. <small id='lYasH'></small><noframes id='lYasH'>

      <legend id='lYasH'><style id='lYasH'><dir id='lYasH'><q id='lYasH'></q></dir></style></legend>
    1. <tfoot id='lYasH'></tfoot>
      <i id='lYasH'><tr id='lYasH'><dt id='lYasH'><q id='lYasH'><span id='lYasH'><b id='lYasH'><form id='lYasH'><ins id='lYasH'></ins><ul id='lYasH'></ul><sub id='lYasH'></sub></form><legend id='lYasH'></legend><bdo id='lYasH'><pre id='lYasH'><center id='lYasH'></center></pre></bdo></b><th id='lYasH'></th></span></q></dt></tr></i><div id='lYasH'><tfoot id='lYasH'></tfoot><dl id='lYasH'><fieldset id='lYasH'></fieldset></dl></div>
    2. 用逗号格式化 JTable 列单元格中的整数

      Formatting Integers in JTable Column Cells With Commas(用逗号格式化 JTable 列单元格中的整数)
      <tfoot id='jueXU'></tfoot>
        • <legend id='jueXU'><style id='jueXU'><dir id='jueXU'><q id='jueXU'></q></dir></style></legend>
          <i id='jueXU'><tr id='jueXU'><dt id='jueXU'><q id='jueXU'><span id='jueXU'><b id='jueXU'><form id='jueXU'><ins id='jueXU'></ins><ul id='jueXU'></ul><sub id='jueXU'></sub></form><legend id='jueXU'></legend><bdo id='jueXU'><pre id='jueXU'><center id='jueXU'></center></pre></bdo></b><th id='jueXU'></th></span></q></dt></tr></i><div id='jueXU'><tfoot id='jueXU'></tfoot><dl id='jueXU'><fieldset id='jueXU'></fieldset></dl></div>

            <tbody id='jueXU'></tbody>

          <small id='jueXU'></small><noframes id='jueXU'>

            <bdo id='jueXU'></bdo><ul id='jueXU'></ul>

                本文介绍了用逗号格式化 JTable 列单元格中的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个价格列,它以普通格式显示整数,例如 1000000.我想知道如何在使用 table.getValueAt() 检索时使用逗号对其进行格式化而不影响其值?

                I have a Price column that displays integers in plain format like 1000000. I would like to know how can I format it with commas without affecting its value when retrieving with table.getValueAt()?

                有没有像table.setColumnCellFormat(decimalFormat)这样的方法?

                Is there a method like table.setColumnCellFormat(decimalFormat)?

                推荐答案

                您需要一个自定义的 TableCellRenderer,它可以按照您需要的方式格式化值.有关详细信息,请参阅使用自定义渲染器

                You need a custom TableCellRenderer which can format the value the way you need it. See Using Custom Renderers for more details

                import java.awt.BorderLayout;
                import java.awt.Component;
                import java.awt.EventQueue;
                import java.text.NumberFormat;
                import javax.swing.JFrame;
                import javax.swing.JLabel;
                import javax.swing.JPanel;
                import javax.swing.JScrollPane;
                import javax.swing.JTable;
                import javax.swing.UIManager;
                import javax.swing.UnsupportedLookAndFeelException;
                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() {
                                try {
                                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                                    ex.printStackTrace();
                                }
                
                                JFrame frame = new JFrame("Testing");
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                frame.add(new TestPane());
                                frame.pack();
                                frame.setLocationRelativeTo(null);
                                frame.setVisible(true);
                            }
                        });
                    }
                
                    public class TestPane extends JPanel {
                
                        public TestPane() {
                
                            DefaultTableModel model = new DefaultTableModel(0, 1);
                            for (int index = 10000; index < 11000; index++) {
                                model.addRow(new Object[]{index});
                            }
                
                            JTable table = new JTable(model);
                            table.getColumnModel().getColumn(0).setCellRenderer(new NumberTableCellRenderer());
                
                            setLayout(new BorderLayout());
                            add(new JScrollPane(table));
                
                        }
                
                        public class NumberTableCellRenderer extends DefaultTableCellRenderer {
                
                            public NumberTableCellRenderer() {
                                setHorizontalAlignment(JLabel.RIGHT);
                            }
                
                            @Override
                            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                                if (value instanceof Number) {
                                    value = NumberFormat.getNumberInstance().format(value);
                                }
                                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                            }
                
                        }
                
                    }
                }
                

                这篇关于用逗号格式化 JTable 列单元格中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <tfoot id='T35Mp'></tfoot>
                  • <i id='T35Mp'><tr id='T35Mp'><dt id='T35Mp'><q id='T35Mp'><span id='T35Mp'><b id='T35Mp'><form id='T35Mp'><ins id='T35Mp'></ins><ul id='T35Mp'></ul><sub id='T35Mp'></sub></form><legend id='T35Mp'></legend><bdo id='T35Mp'><pre id='T35Mp'><center id='T35Mp'></center></pre></bdo></b><th id='T35Mp'></th></span></q></dt></tr></i><div id='T35Mp'><tfoot id='T35Mp'></tfoot><dl id='T35Mp'><fieldset id='T35Mp'></fieldset></dl></div>

                      <tbody id='T35Mp'></tbody>
                      <bdo id='T35Mp'></bdo><ul id='T35Mp'></ul>

                        <small id='T35Mp'></small><noframes id='T35Mp'>

                        1. <legend id='T35Mp'><style id='T35Mp'><dir id='T35Mp'><q id='T35Mp'></q></dir></style></legend>