java - 如何在单元格选择过程中同时设置单元格背景和小数格式

标签 java swing matlab awt

如果标题不够解释,我很抱歉。

为了指定 cellBackgourd、cellForeground、Alignment 和 decimal 控制,我用 Java 编写了 DefaultTableRenderer 类的一个小扩展。我在 matlab 中使用此类来控制和自定义 JIDE 表。但是已经在 J​​ava 中重新创建了问题,以增加回复或可能的解决方法的机会。

当表格初始化和显示时,我能够根据需要成功设置单元格背景、前景、对齐方式和小数位。但是,一旦我选择了一个行/单元格,我就失去了对显示数据的小数点控制,如下图所示。请注意,我使用了特定的单元格选择颜色,我想,我没有正确实现它。

enter image description here

我认为,问题出在最后两行之一:

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

return cell;

Java代码:

import java.awt.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.table.*;

public class DecimalPlacesInTable extends JFrame {
    public static void main( String[] args ) {
        DecimalPlacesInTable frame = new DecimalPlacesInTable();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
    }

    public DecimalPlacesInTable() {
        Object[] columnNames = { "A", "B", "C" };
        Object[][] data = {
                {new Double( 850.503 ), new Double( 850.545 ), new Double( 80.54553 ) },
                {new Double( 50.52503 ), new Double( 36.4554 ), new Double( 50.41453 ) },
                {new Double( 80.544653 ), new Double( 8.3 ), new Double( 80.4553 ) },
                {new Double( 50.1553 ), new Double( 246.0943 ), new Double( 50.455 ) }};

        JTable table = new JTable(data, columnNames);

        // Tell the table what to use to render our column of doubles
        for (int i=0; i<3; i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(new DecimalFormatRenderer());
            getContentPane().add(new JScrollPane(table));
        }
    }

    // Custom Renderer class
    static class DecimalFormatRenderer extends DefaultTableCellRenderer
    {
        public Component getTableCellRendererComponent
                (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JComponent cell = (JComponent) super.getTableCellRendererComponent
                    (table, value, isSelected, hasFocus, row, column);
            // set color
            cell.setBackground(new Color(0xC8C8C8));
            cell.setForeground(new Color(0xFFFFFF));

            //set Alignment
            ((JLabel) cell).setHorizontalAlignment(SwingConstants.CENTER);

            //set selection colors
            if (isSelected) {
                cell.setBackground(new Color(0x3399FF));
                cell.setForeground(new Color(0x000000)); // AM
            } else {
                // set decimals
                DecimalFormat DecimalFormatter = new DecimalFormat("#.00");
                value = DecimalFormatter.format(value);
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }
            return cell;
        }
    }
}

最佳答案

import java.awt.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.table.*;

public class DecimalPlacesInTable2 extends JFrame {
  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      DecimalPlacesInTable2 frame = new DecimalPlacesInTable2();
      frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
    });
  }

  public DecimalPlacesInTable2() {
    Object[] columnNames = { "A", "B", "C" };
    Object[][] data = {
      {850.503,   850.545,  80.54553},
      {50.52503,  36.4554,  50.41453},
      {80.544653, 8.3,      80.4553},
      {50.1553,   246.0943, 50.455}
    };
    //JTable table = new JTable(data, columnNames);
    TableModel model = new DefaultTableModel(data, columnNames) {
      @Override public Class<?> getColumnClass(int column) {
        return Double.class;
      }
    };
    JTable table = new JTable(model);

    // Tell the table what to use to render our column of doubles
    for (int i = 0; i < 3; i++) {
      table.getColumnModel().getColumn(i).setCellRenderer(new DecimalFormatRenderer());
      //getContentPane().add(new JScrollPane(table));
    }
    getContentPane().add(new JScrollPane(table));
  }

  // Custom Renderer class
  static class DecimalFormatRenderer extends DefaultTableCellRenderer {
    private final DecimalFormat formatter = new DecimalFormat("#.00");
    @Override public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
      JLabel cell = (JLabel) super.getTableCellRendererComponent(
          table, value, isSelected, hasFocus, row, column);

      //set Alignment
      cell.setHorizontalAlignment(SwingConstants.CENTER);

      //set selection colors
      if (isSelected) {
        cell.setBackground(new Color(0x3399FF));
        cell.setForeground(new Color(0x000000)); // AM
      } else {
        // set color
        cell.setBackground(new Color(0xC8C8C8));
        cell.setForeground(new Color(0xFFFFFF));
      }

//       // set decimals
//       if (value instanceof Double) {
//         cell.setText(formatter.format(value));
//       }

      return cell;
    }
    @Override public void setValue(Object value) {
        setText(value instanceof Double ? formatter.format(value) : "");
    }
  }
}

关于java - 如何在单元格选择过程中同时设置单元格背景和小数格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36373058/

相关文章:

java - JComboBox 添加值

java - 带有 Swing 的 JRuby : "The OSX menu"

java - 在 Java 中编写对象的大型 if-else 语句的最佳方法?

java - 通过集成测试用例发送到服务的参数有问题:groovy.lang.MissingMethodException

java - 在 JAVA 中将列表对象添加到另一个列表中

java - 我遇到的最奇怪的 Java 问题,NoClassDefFoundError 由于注释?

matlab - pcolor 上的对角线(小平面边缘不匹配!)

java - Web Crawler 在电子邮件链接上被阻止

matlab - 在 matlab 中具有不同误差条的多维(2d 更好 3d)散点图

matlab - 如何在 Matlab 中使用 onCleanup 保存变量?