Results 1 to 5 of 5
  1. #1
    Angie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Question [SOLVED] How do you add cell padding to a JTable?

    This is very frustrating, I have been trying to figure out how to add cell padding to all the cells (including the header) in my JTable. I tried setIntercellSpacing but that adds padding to the space between cells. I am looking to add the equivalent of "1 space" of padding to the left and the right side of the inside of the cells so that the text is nicer to read when the text is left or right aligned. Anybody have any ideas on how to accomplish this?

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    setRowMargin(), setRowHeight() etc maybe

    Or you could add a blank cell(if JTable is similar to working with buttons on a gridLayout)

    Java 2 Platform SE 5.0 look there for the proper method to use
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    In your custom TableCellRenderer you want to do:

    Java Code:
    
    
    public class MyColumnTableCellRenderer extends JLabel implements TableCellRenderer {
    
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, int column) {
    
                   JLabel cellSpacingLabel= (JLabel) (this);
    
                   if (hasFocus) {
                               setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
    
                               cellSpacingLabel=null;
                   }
                   else{
                               setBackground(table.getBackground());
                 
                               stBorder(null);
    
    
                    }
    
                    if (isSelected) {     
    
    
                           setBackground(table.getSelectionBackground());
                    
                           setBorder(null);
    
    
                   }
                   else{
    
                           setBackground(table.getBackground());
                    
                           setBorder(null);
    
    
                  }
    
                    if (cellSpacingLabel != null) {
                              cellSpacingLabel .setBorder(new CompoundBorder(new EmptyBorder(new Insets(1, 4, 1, 4)), oLabel.getBorder()));
    
                    }
    
    
                    this.setOpaque(true);
                    setText((String) value);
    
    
                     return this;
    
            }
    
    
    
    }

    That should work for you...
    Who Cares... As Long As It Works...

  4. #4
    Angie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Thumbs up You are some kind of genius!

    I would not have guessed

    "l.setBorder(new CompoundBorder(new EmptyBorder(new Insets(1,4,1,4)), l.getBorder()));"

    on my own.

    Thank you, it looks great now!!!!!!

  5. #5
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    That's why I make the big bucks...
    Who Cares... As Long As It Works...

Similar Threads

  1. add a check box to only one cell in a JTable
    By aruna1 in forum New To Java
    Replies: 4
    Last Post: 04-03-2009, 06:28 PM
  2. Color cell in JTable
    By ippacciani in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 11:53 AM
  3. set different font for each cell in JTable
    By success21061985 in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2008, 02:06 PM
  4. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 08:17 AM
  5. Jframe In Jtable cell
    By Clarion in forum AWT / Swing
    Replies: 4
    Last Post: 06-23-2008, 04:42 AM

Posting Permissions

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