Results 1 to 5 of 5
  1. #1
    gjforte is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Jtable with column separators from top to bottom regardless the number of rows

    I have a JTable and I would like to have column separators from top to bottom regardless the number of rows.

    Does anyone know how can I do this?

    Something like this:

  2. #2
    gjforte is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default


  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    You would need to extend JTable and override paintComponent(...) to draw the column lines. Also, you need to setFillsViewportHeight(true) to force the table to occupy the entire viewport.

    In pseudocode
    Java Code:
    paintComponent
      invoke super implementation
      compute cell rectangle of a cell in the last row [at getRowCount() -1]
      compute bottom of that rectangle [y + height] ... say Y1
      compute bottom of visible rectangle [getVisibleRect().height] ... say Y2
      IF Y2 > Y1 THEN
        set the Graphics color to the grid color [gridColor or getGridColor()]
        iterate over the columns in the column model
                  incrementing a X coordinate by the column's width
        draw lines from (X, Y1) to (X, Y2)
    Hm, there's a 1-pixel discrepancy I can't quite pin down. Give it a shot and lets see what you can come up with.

    db
    Last edited by DarrylBurke; 03-25-2011 at 10:59 AM.

  4. #4
    gjforte is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much!

    After some experiences I was able to solve the problem!

    Here is the code:
    Java Code:
    table = new JTable(){
    			@Override
    			  public void paintComponent( Graphics g )
    			  {
    			    super.paintComponent( g );
    
    			    paintEmptyRows( g );
    			  }
    			
    			public void paintEmptyRows( Graphics g ){
    			    Graphics newGraphics = g.create();
    			    newGraphics.setColor( UIManager.getColor( "Table.gridColor" ) );
    
    			    Rectangle rectOfLastRow = getCellRect( getRowCount() - 1, 0, true );
    			    int firstNonExistentRowY = rectOfLastRow.y; //the top Y-coordinate of the first empty tablerow
    			    if ( getVisibleRect().height > firstNonExistentRowY ) //only paint the grid if empty space is visible
    			    {
    
    			      //paint the column-lines:
    			      int x = 0;
    			      for ( int i = 0; i < getColumnCount(); i++ )
    			      {
    			        TableColumn column = getColumnModel().getColumn( i );
    			        x += column.getWidth(); //add the column width to the x-coordinate
    			        newGraphics.setColor( Color.LIGHT_GRAY );
    			        
    			        newGraphics.drawLine( x - 1, firstNonExistentRowY, x - 1, getHeight() );
    			      }
    
    			      newGraphics.dispose();
    
    			    } //if empty space is visible
    
    			  } //paintEmptyRows
    		};

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Comments:
    -- The top of the first nonexistent row is rectOfLastRow.y + rectOfLastRow.height
    -- First you set the color to UIManager.getColor( "Table.gridColor" ), and never use the Graphics with that color setting. Then you set it to Color.LIGHT_GRAY, every time through the loop, which is inefficient. Neither will be the correct color if setGridColor has been invoked. I gave you the correct approach in the pseudocode.
    -- Instead of subtracting 1 from x twice on every pass through the loop, isn't it better to initialize x to -1?
    -- In your vocabulary, is 'experience' equivalent to 'searching the net'?
    jtable empty rows grid +paintEmptyRows - Google Search

    First hit:
    java - JTable with striped background - Stack Overflow

    db

Similar Threads

  1. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 06:24 AM
  2. Remove Jtable rows
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2010, 08:03 AM
  3. Fix the rows and column of JTextPane
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 09-09-2010, 08:16 PM
  4. Listening rows in JTable ??
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-10-2010, 04:45 PM
  5. Highlight rows of a jtable
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2009, 01:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •