Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

How to define the border color for each column in JTable?

Thread view: 
Roland Zitzke - 06 Feb 2006 11:49 GMT
Hi,
On a per-column bases I want to change the color of the lines separating the
table columns i.e. the grid color. Is there any easy way to acheve this?

Thanks and regards
Roland
Bart Cremers - 06 Feb 2006 12:27 GMT
I would do this by disabling the painting of lines on the table and
then install a custom cell renderer on the table doing the required
work. Not that hard.

Bart
Roland Zitzke - 06 Feb 2006 12:55 GMT
>I would do this by disabling the painting of lines on the table and
> then install a custom cell renderer on the table doing the required
> work. Not that hard.

Well, but in this case I need to make sure my renderer draws lines and
deligates the rest of the work to the defaultcellrenderer which looks kind
of tricky.
Do you know any example code?
One thing that doesn't work is to create a Border using the boarder factory
and then use the setBorder on the component to change it. This has absolutly
no effect even though I know that the cell renderer itself is called - it
also changes the background color for every other row to create a contrast.

Thanks and regards
Roland
Bart Cremers - 06 Feb 2006 13:55 GMT
import java.awt.Component;
import java.awt.Color;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;

/**
*
*
* @author Bart Cremers
* @date Feb 6, 2006
*/
public class TableBorders extends JFrame {

   private JTable table;

   protected void frameInit() {
       super.frameInit();

       table = new JTable(5, 6);
       table.setShowVerticalLines(false);
       table.setDefaultRenderer(Object.class, new
BorderColorRenderer());
       add(table);
   }

   public static void main(String[] args) {
       JFrame f = new TableBorders();
       f.pack();
       f.setLocationRelativeTo(null);
       f.setDefaultCloseOperation(EXIT_ON_CLOSE);
       f.setVisible(true);
   }

   private static class BorderColorRenderer extends
DefaultTableCellRenderer {

       public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
                                                      int row, int
column) {
           Border border;
           switch (column) {
               case 1:
                   border = BorderFactory.createMatteBorder(0, 2, 0,
0, Color.GREEN);
                   break;
               case 2:
                   border = BorderFactory.createMatteBorder(0, 2, 0,
0, Color.RED);
                   break;
               case 3:
                   border = BorderFactory.createMatteBorder(0, 2, 0,
0, Color.BLUE);
                   break;
               case 4:
                   border = BorderFactory.createMatteBorder(0, 2, 0,
0, Color.PINK);
                   break;
               case 5:
                   border = BorderFactory.createMatteBorder(0, 2, 0,
0, Color.CYAN);
                   break;
               default:
                   border = BorderFactory.createEmptyBorder(0, 0, 0,
0);
                   break;
           }
           JComponent comp = (JComponent)
super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, column);
           comp.setBorder(border);
           return comp;
       }
   }
}
zero - 06 Feb 2006 14:09 GMT
"Bart Cremers" <bcremers@gmail.com> wrote in news:1139234143.188865.191040
@g43g2000cwa.googlegroups.com:

> table.setShowVerticalLines(false);

would it still work without this call?
Bart Cremers - 06 Feb 2006 14:14 GMT
Yes, but you would see an ugly line in the gridcolor next to the custom
colored lines.

Bart
zero - 06 Feb 2006 17:45 GMT
"Bart Cremers" <bcremers@gmail.com> wrote in news:1139235250.976703.235850
@g14g2000cwa.googlegroups.com:

> Yes, but you would see an ugly line in the gridcolor next to the custom
> colored lines.
>
> Bart

I see.  Thanks for the info :-)
Vova Reznik - 06 Feb 2006 16:07 GMT
> Hi,
> On a per-column bases I want to change the color of the lines separating the
> table columns i.e. the grid color. Is there any easy way to acheve this?
>
> Thanks and regards
> Roland

You may create your implementation of Border and TableCellRenderer

class MultiColorBorder implements Border {
        private final Insets    insets    = new Insets(1, 1, 1, 1);
        private Color            color    = Color.LIGHT_GRAY;
        public boolean isBorderOpaque() {
            return false;
        }
        public void paintBorder(Component c, Graphics g, int x, int y,
                int width, int height) {
            g.setColor(color);
            g.drawRect(x, y, width, height);
        }
        public Insets getBorderInsets(Component c) {
            return insets;
        }
        void setColor(Color color) {
            this.color = color;
        }
    }

==================================================================
class MyRenderer extends DefaultTableCellRenderer {
        private MultiColorBorder    b            = new MultiColorBorder();
        private Color                oddColor    = Color.BLUE;
        private Color                evenColor    = Color.RED;
        public MyRenderer() {
            setBorder(b);
        }
        public Component getTableCellRendererComponent(JTable t, Object o,
                boolean b1, boolean b2, int r, int c) {
            JLabel comp = (JLabel) super.getTableCellRendererComponent(t, o,
                    b1, b2, r, c);
            b.setColor((c % 2 == 0 || c == 0) ? evenColor : oddColor);
            comp.setBorder(b);
            return comp;
        }
    }

or instead of TableCellRenderer you may override
JTable:public TableCellRenderer prepareRenderer(TableCellRenderer, int,
int);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.