1

highlight the cell in jtable

Continuing from my earlier post, just help me to give the idea of selecting just the cell on clicking the button.

The idea was to make use of getTableCellRendererComponent , but TableColumn class is being used which selects the whole Column. So how to make use of just highlighting the cell

Prob1 : Need to highlight just a cell on a button click.
Prob 2: I couldnt move on with the project without solving the above. please help.

Courtesy :@Zyion
package helped;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.*;

public class Main extends JFrame {

public Main() {
    super("Table Demo");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(300, 300));
    setLocationRelativeTo(null);
    setLayout(new BorderLayout());

    DefaultTableModel model = new DefaultTableModel();
    model.setColumnCount(5);
    model.setRowCount(5);

    JTable table = new JTable();
    table.setModel(model);

    //Get an instance of the column and the style to apply and hold a default style instance
    final TableColumn column = table.getColumnModel().getColumn(1);
    final CellHighlighterRenderer cellRenderer = new CellHighlighterRenderer();
    final TableCellRenderer defaultRenderer = column.getCellRenderer();

    //Now in your button listener you can toggle between the styles 
    JButton button = new JButton("Click!");
    button.addActionListener(new ActionListener() {
        private boolean clickedd = false;


        public void actionPerformed(ActionEvent e) {

            if (clickedd) {
                column.setCellRenderer(cellRenderer);
                clickedd = false;
            } else {
                column.setCellRenderer(defaultRenderer);
                clickedd = true;
            }
            repaint(); //edit
        }
    });

    getContentPane().add(table, BorderLayout.CENTER);
    getContentPane().add(button, BorderLayout.NORTH);
    pack();
    setVisible(true);
}

public static void main(String[] args) {
     new Main();
}
}
 class CellHighlighterRenderer extends DefaultTableCellRenderer {

      @Override
      public Component getTableCellRendererComponent(JTable table, Object obj,
              boolean isSelected, boolean hasFocus, int row, int column) {

          Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, 1, 0);

          //add condition for desired cell
        //  if (row == 1 && column == 1)
              cell.setBackground(Color.YELLOW);

          return cell;
      }
}
Community
  • 1
  • 1
sneha nambiar
  • 113
  • 1
  • 3
  • 12
  • That's already a long chain of questions with many answers and comments and discussion. Without reading them all, **this** question will hardly be understood by anyone. However, maybe the `ColoringCellRenderer` in http://stackoverflow.com/a/24556135/3182664 is helpful for you...? – Marco13 Aug 06 '14 at 15:49
  • Please understand that i just need to highlight a cell using a button action. Sorry, if i make the question complex. – sneha nambiar Aug 06 '14 at 16:50
  • @Marco13 : That example from the link was the best. But studying on that code, i am not clear when is the " getTableCellRendererComponent " is called. – sneha nambiar Aug 06 '14 at 16:52
  • When the `JTable` has to be repainted (for example, because its contents or the window size changed), then it will paint all its cells. In order to paint its cells, it will call the `getTableCellRendererComponent` method, once for each cell. The component that is returned there will be used to "fill" the cell area. But note again: It is always the *same* component for *all* cells. The component is only modified to show the contents that is required for the current cell. Also see http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender – Marco13 Aug 06 '14 at 19:29
  • [for example](http://stackoverflow.com/q/16814512/714968), or to use prepareRenderer – mKorbel Aug 06 '14 at 20:28
  • @Marco13: Sir, a problem comes with the implementation of that example. Behaviour is highlighting perfectly for the first input of (row_position, column_position) .But then it always the same row position for other tables that i have added. can you please help on it. – sneha nambiar Aug 07 '14 at 08:04
  • Each table needs its own *instance* of the table cell renderer (there are other options, but this is the simplest one) – Marco13 Aug 07 '14 at 08:05

0 Answers0