如何使JTable中的单元格为空,直到单击?

时间:2019-01-21 14:05:18

标签: java swing jtable

我创建了一个10 x 10的乘法表。我试图使答案为空的单元格直到它们被单击为止。用户单击该单元格后,答案将停留在桌子上。

我已经查看了如何在单元格中使用按钮,但是我认为我应该使用事件监听器而不是按钮。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimpleTableDemo{
    public static void main(String args[]) {
        JFrame frame = new JFrame("Multiplication Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //create the table to output. only doing a 10 by 10 table
    String data[][] = {{"1", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"},
            {"2", "2", "4", "6", "8", "10", "12", "14", "16", "18", "20"},
            {"3", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30"},
            {"4", "4", "8", "12", "16", "20", "24", "28", "32", "36", "40"},
            {"5", "5", "10", "15", "20", "25", "30", "35", "40", "45", "50"},
            {"6", "6", "12", "18", "24", "30", "36", "42", "48", "54", "60"},
            {"7", "7", "14", "21", "28", "35", "42", "49", "56", "63", "70"},
            {"8", "8", "16", "24", "32", "40", "48", "56", "64", "72", "80"},
            {"9", "9", "18", "27", "36", "45", "54", "63", "72", "81", "90"},
            {"10", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"}};
    String column[] = {"X", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

    //makes it so that the cells are not editable by the user
    JTable jt = new JTable(data, column){
        public boolean isCellEditable(int row, int column){
            return false;
        }
    };

    jt.setBounds(300, 400, 200, 300);
    JScrollPane sp = new JScrollPane(jt);
    frame.add(sp);
    frame.setSize(1000, 580);
    frame.setVisible(true);
    jt.setRowHeight(50);
    //makes it so that the columns can't be reordered
    jt.getTableHeader().setReorderingAllowed(false);

    jt.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                JTable target = (JTable)e.getSource();
                int row = target.getSelectedRow();
                int column = target.getSelectedColumn();
                // do some action if appropriate column
                jt.setBackground(Color.cyan);
            }
         }
      });
    }
}

我当前的代码中有一部分更改了表格的背景颜色,因为我试图测试其他内容。我试图使代码仅更改单个单元格的颜色,但更改了整个表格。我希望我的代码格式正确。抱歉,如果不是,我会尝试修复它。

1 个答案:

答案 0 :(得分:1)

您要为表格提供一个渲染器,该渲染器仅在该单元格具有焦点时才显示该单元格的值,例如:

jt.setDefaultRenderer(Integer.class, new DefaultTableCellRenderer() {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        value = hasFocus ? value : "";
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
});

请注意,我还将单元格类型设置为Integer.class

JTable jt = new JTable(data, column) {
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    @Override
    public Class<?> getColumnClass(int column) {
        return Integer.class;
    }
};

我看到您也想一直显示第一行-因此您可以更改渲染器以允许这样做:value = hasFocus || column == 0 ? value : "";

jt.setDefaultRenderer(Integer.class, new DefaultTableCellRenderer() {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        value = hasFocus || column == 0 ? value : "";
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
});