<legend id='53vEv'><style id='53vEv'><dir id='53vEv'><q id='53vEv'></q></dir></style></legend>

    • <bdo id='53vEv'></bdo><ul id='53vEv'></ul>
  • <small id='53vEv'></small><noframes id='53vEv'>

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

        <tfoot id='53vEv'></tfoot>

        用逗号格式化 JTable 列单元格中的整数

        时间:2023-09-29

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

          • <bdo id='mMkl0'></bdo><ul id='mMkl0'></ul>

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

                <legend id='mMkl0'><style id='mMkl0'><dir id='mMkl0'><q id='mMkl0'></q></dir></style></legend>

                    <tbody id='mMkl0'></tbody>
                  本文介绍了用逗号格式化 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 列单元格中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:带有 dateStyle 和 timeStyle 的 Joda-Time 格式化程序 下一篇:格式,Java 中双精度和整数的 2 位小数和 0

                  相关文章

                  最新文章

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

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

                    <legend id='ImNBz'><style id='ImNBz'><dir id='ImNBz'><q id='ImNBz'></q></dir></style></legend>