Tania
Tania

Reputation: 23

How to make cells in JTable empty until clicked?

I have created a 10 by 10 multiplication table. I am trying to make the cells with the answer empty until they are clicked. Once the user clicks the cell, the answer would stay up on the table.

I have looked up how to use a button in the cells but I am thinking that I should use an event listener rather than a button.

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);
            }
         }
      });
    }
}

My current code has a portion where it changes the background colour of the table because I was trying to test different things. I was trying to make the code change just a single cell colour but it changed the whole table. I hope my code is formatted properly. Sorry if it isn't, I'll try to fix it if it isn't.

Upvotes: 1

Views: 62

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You want to give your table a renderer that only displays the cell's value if that cell has focus, something like:

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);
    }
});

Note that I've also set the cell type to be 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;
    }
};

I see that you also want to show the first row at all times -- so you can change the renderer to allow this: 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);
    }
});

Upvotes: 1

Related Questions