Solved

JTable color

Posted on 2011-09-29
8
210 Views
Last Modified: 2012-05-12
I am trying to set the grid color of my JTable using
.setSelectionForeground(Color.red);

However, this seems to set the color for every grid line, except the leftmost gridline.

Please let me know what I could be missing. Thanks!
0
Comment
Question by:dshrenik
  • 5
  • 3
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36818572
Do you want to change the font color of the selected cells?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36818574
what means grid color ?
0
 

Author Comment

by:dshrenik
ID: 36818575
No. I want to change the color of the table border (only the outline - a rectangle).
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:dshrenik
ID: 36818581
Sorry, I used the wrong word - it is not grid color, but I must say "outer border" (rectangle)
0
 

Author Comment

by:dshrenik
ID: 36818584
This is working partially - but does not add a border to the header:
Border tableBorder = BorderFactory.createLineBorder(Color.gray);
            insulineCoverageTable .setBorder(tableBorder);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36818586
Maybe then you need to use
setGridColor(Color...)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36818611
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36818750
This is also example about the borders in the table - it is simpler, but
although shows how to modify the border of
selected cell, not the header , but it gives the general idea


This code is from

http://stackoverflow.com/questions/1772764/how-do-you-add-a-border-to-a-row-in-a-jtable:

The better approach is to override the prepareRenderer(...) method JTable so you can add the code in one place. Here is an example to get you started. In reality you would want to use a CompoundBorder that contains a MatteBorder for the top/bottom and and EmptyBorder for the left/right and you would create a single instance of the Border.


import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.border.*;

public class TablePrepareRenderer extends JFrame
{
    JTable table;

    public TablePrepareRenderer()
    {
        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
        Object[][] data =
        {
                {"Buy", "IBM", new Double(1000), new Double(80.5), Boolean.TRUE},
                {"Sell", "MicroSoft", new Double(2000), new Double(6.25), Boolean.TRUE},
                {"RSell", "Apple", new Double(3000), new Double(7.35), Boolean.TRUE},
                {"Buy", "Nortel", new Double(4000), new Double(20), Boolean.TRUE}
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = 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 getValueAt(0, column).getClass();
                }

                public Component prepareRenderer(
                        TableCellRenderer renderer, int row, int column)
                {
                        Component c = super.prepareRenderer(renderer, row, column);
                        JComponent jc = (JComponent)c;

                        //  Color row based on a cell value
                        //  Alternate row color

                        if (!isRowSelected(row))
                                c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
                        else
                                jc.setBorder(new MatteBorder(1, 0, 1, 0, Color.RED) );


                        //  Use bold font on selected row

                        return c;
                }
        };

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.changeSelection(0, 0, false, false);
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

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

Open in new window

table-borders.PNG
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 60
eclipse shortcuts 9 56
JavaFX TableView not displaying correctly 3 20
eclipse console opening separately 2 21
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question