Solved

JTable color

Posted on 2011-09-29
8
203 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
Comment Utility
Do you want to change the font color of the selected cells?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
what means grid color ?
0
 

Author Comment

by:dshrenik
Comment Utility
No. I want to change the color of the table border (only the outline - a rectangle).
0
 

Author Comment

by:dshrenik
Comment Utility
Sorry, I used the wrong word - it is not grid color, but I must say "outer border" (rectangle)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:dshrenik
Comment Utility
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
Comment Utility
Maybe then you need to use
setGridColor(Color...)
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now