Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Highlighting both row and column in JTable

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Highlighting both row and column in JTable

    I am writing an applet which contains a JTable. The JTable displays Z-Scores for a Statistics application. Because it can be hard to determine the row and column of the highlighted cell, I want to highlight each when the user selects a given cell. I know I can use JTable.setColumnSelectionAllowed(true) to accomplish column highlighting. However if I do the same for the rows, using JTable.setRowSelectionAllowed(true), then I get no highlighting and only the single selected cell ends up with a border. Is there a way to use both?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Highlighting both row and column in JTable

    Take a look at the prepareRenderer method in JTable. I'd suggest extending the JTable and overriding this method, customizing the returned component based upon the JTable selection. Then add a ListSelectionListener and repaint the table upon selection:
    	private JTable table = new JTable(){
    		@Override public Component prepareRenderer(TableCellRenderer renderer,
                    int row,
                    int col){
    			Component c = super.prepareRenderer(renderer, row, col);
    			int selCol = table.getSelectedColumn();
    			int selRow = table.getSelectedRow();
    			if ( selCol != -1 && selRow != -1 ){
    				if ( row == selRow || col == selCol){
    					c.setBackground(Color.BLACK);
    				}else{
    					c.setBackground(Color.WHITE);
    				}				
    			}
    			return c;
    		}
    	};
    ////////
    //add a listener
    		table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
    			public void valueChanged(ListSelectionEvent e){
    				table.repaint();
    			}
    		});

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Highlighting both row and column in JTable

    1. Overriding prepareRenderer can do the job, but I feel the programming idiom here is better represented by overriding isCellSelected.
    2. You need to repaint the table when a cell in a different column in the selected row is selected. Thus, you need to add a listener to the table's columnModel's selectionModel also.
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
     
    public class RowColumnSelectedTable {
     
      static final int ROWS = 100;
      static final int COLUMNS = 3;
     
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
     
          @Override
          public void run() {
            new RowColumnSelectedTable().makeUI();
          }
        });
      }
     
      public void makeUI() {
        Object[][] data = new Object[ROWS][COLUMNS];
        String[] headers = new String[COLUMNS];
        for (int column = 0; column < COLUMNS; column++) {
          headers[column] = "Header " + column;
          for (int row = 0; row < data.length; row++) {
            data[row][column] = "Row " + row + ", Column " + column;
          }
        }
        final JTable table = new JTable(data, headers) {
     
          @Override
          public boolean isCellSelected(int row, int column) {
            return isColumnSelected(column) || isRowSelected(row);
          }
        };
        ListSelectionListener listener = new ListSelectionListener() {
     
          @Override
          public void valueChanged(ListSelectionEvent e) {
            table.repaint();
          }
        };
        table.getSelectionModel().addListSelectionListener(listener);
        table.getColumnModel().getSelectionModel().addListSelectionListener(listener);
     
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.add(new JScrollPane(table));
        frame.setVisible(true);
      }
    }
    db

  4. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    copeg (May 29th, 2010)

  5. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Highlighting both row and column in JTable

    Shorter, and doesn't require listeners. Also, more efficient as only the visibleRect of the table is repainted.
    import javax.swing.*;
     
    public class RowColumnSelectedTable {
     
      static final int ROWS = 100;
      static final int COLUMNS = 3;
     
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
     
          @Override
          public void run() {
            new RowColumnSelectedTable().makeUI();
          }
        });
      }
     
      public void makeUI() {
        Object[][] data = new Object[ROWS][COLUMNS];
        String[] headers = new String[COLUMNS];
        for (int column = 0; column < COLUMNS; column++) {
          headers[column] = "Header " + column;
          for (int row = 0; row < data.length; row++) {
            data[row][column] = "Row " + row + ", Column " + column;
          }
        }
        final JTable table = new JTable(data, headers) {
     
          @Override
          public boolean isCellSelected(int row, int column) {
            return isColumnSelected(column) || isRowSelected(row);
          }
     
          @Override
          public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
            super.changeSelection(rowIndex, columnIndex, false, false);
            repaint(getVisibleRect());
          }
        };
     
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.add(new JScrollPane(table));
        frame.setVisible(true);
      }
    }
    db

  6. #5
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Highlighting both row and column in JTable

    Thanks for the replies! I will be trying these out soon.

Similar Threads

  1. Populating a JTable
    By crism85 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 20th, 2010, 01:57 PM
  2. JTable in JScrollPane
    By alwayslearner in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 29th, 2010, 11:42 AM
  3. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 08:15 AM
  4. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM