Редактировать ячейку JTable не имеет никакого эффекта - PullRequest
0 голосов
/ 28 ноября 2018

Я хочу выделить определенные ячейки в JTable и внести необходимые изменения, но программа их не видит.

Когда я отлаживал код, я заметил, что программа отображала таблицу, ноэто не относится к выражению if.

Было бы замечательно, если бы кто-то мог сказать мне, что происходит не так.

    public class Frame extends JFrame {

        private JPanel contentPane;
        private static JTable table;
        final DefaultTableModel model;

    String data[][] = { {"E","S","M","I","S","T","P","G","L","E","I","C","H","Y","M"},
        {"G","H","J","K","F","K","N","F","Z","I","G","W","X","Z","T"} };

        String[] columns =  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };

    public static void main(String[] args) {
        try {
            Frame frame = new Frame();
            centerWindow(frame);
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        } }
//build the frame
        public Frame() {
        model = new DefaultTableModel();
        setBackground(Color.LIGHT_GRAY);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 570, 570);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

//build the table               
        table = new JTable();
        JTable table = new JTable(data, columns);
        table.setBackground(Color.LIGHT_GRAY);
        table.setFont(new Font("Verdana", Font.PLAIN, 14));
        table.setSize(800, 400);
        table.setRowHeight(30);
        table.setForeground(Color.WHITE);
        contentPane.add(table, BorderLayout.CENTER);

        //in this part, the program should change the color of certain cells ... but it does not :-(
        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
//highlight the cells with the value "s"
                    if (value instanceof String) {
                        if (value.equals("s")){
                            System.out.println(row + " " + column);
                            cell.setBackground(Color.BLUE);} else {
                            cell.setBackground(Color.WHITE);}  }  
            return cell;
            }  
        });  
    }  

1 Ответ

0 голосов
/ 28 ноября 2018

Изменить строку:

if (value.equals("s")){

на

if (value.equals("S")){

Равен с учетом регистра.

...