2

I have a JTable and I want when selecting a row in the table it will be colored blue and have a green border marked around the row. I was able to make the color blue but can not make a green border marked around the row . I have attached a picture -This is what it should be.

enter image description here

Thanks in advance After editing: I add to my code :

 JTable jTable1 = new JTable( model )
{
  //  Returning the Class of each column will allow different
  //  renderers to be used based on Class
  public Class getColumnClass(int column)
  { 
       return columns[column];
  }
  @Override
    public boolean isCellEditable(int row, int column) {
       //all cells false
       return false;
    }

  @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
      Component c = super.prepareRenderer(renderer, row, col);
              JComponent jc = (JComponent)c;
            if (isRowSelected(row)){
                int top = (row > 0 && isRowSelected(row-1))?1:2;
              int left = col == 0?2:0;
              int bottom = (row < getRowCount()-1 && isRowSelected(row + 1))?1:2;
              int right = col == getColumnCount()-1?2:0;
                jc.setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, new Color(210,236,204))); 
                jc.setForeground(new Color(164, 164, 172));
            }
            else{
              jc.setBorder(null);
            }             
        int selCol = -2;
        int selRow = jTable1.getSelectedRow();

        if ( selCol != -1 && selRow != -1 ){
            if ( row == selRow || col == selCol){
                c.setBackground(new Color(249,250,254));    
            }else
                c.setBackground(Color.WHITE);
        }
        return c;
    }
};

And I almost got What I wanted, but if you look closely the color missing between each cell. What should I do?

This is what I have now

enter image description here

gal
  • 71
  • 1
  • 12
  • 2
    http://stackoverflow.com/questions/1772764/how-do-you-add-a-border-to-a-row-in-a-jtable – Madhawa Priyashantha Mar 12 '16 at 17:29
  • Thank you still have a problem I'd be happy if I did see my question. Edited – gal Mar 12 '16 at 18:00
  • for me it works i don't see any missing color effect .see this image http://i.imgur.com/kOWW4rk.png .can you share your code so we can find the problem .may be you have vertical lines .try `table.setShowVerticalLines(false);` – Madhawa Priyashantha Mar 12 '16 at 18:44
  • I edited and added everything to do with it. If you do not find I can send you by email the entire project – gal Mar 12 '16 at 18:52
  • i still get the same output .no color problem – Madhawa Priyashantha Mar 12 '16 at 18:58
  • In some look-and-feels, the cells do not touch, and there's nothing you can do about it. However, instead of altering cell renderer borders, you could just override the JTable's paintComponent method, and draw a border around the entire row after calling super.paintComponent. – VGR Mar 13 '16 at 00:02
  • jTable1.setIntercellSpacing(new Dimension(0, jTable1.getRowMargin())); – gal Mar 14 '16 at 14:23
  • It worked for me, thanks – gal Mar 14 '16 at 14:24

0 Answers0