Jtable in swing sets the foreground color and background color of the selected row in the table

Source: Internet
Author: User

Jtable itself has no way to directly set the foreground color and background color of the selected row, but we can implement the functions we need by writing tablecellrenderer. This interface defines the methods required to become any object in the jtable cell Renderer.

By Rewriting
Gettablecellrenderercomponent (jtable table, object value, Boolean isselected, Boolean hasfocus, int row, int column)

Returns the component used to draw cells.
Table-The jtable to be drawn by the Renderer; it can be null.
Value-the value of the cell to be rendered. This value is interpreted and drawn by the specific Renderer. For example, if the value is a string "true", it can be rendered as a string or as a selected check box. Null is a valid value.

Isselected-if you use the highlighted display of the selected style to present the cell, the value is true; otherwise, the value is false.
Hasfocus-if it is true, the cell is properly rendered. For example, place a special border on a cell. If you can edit the cell, it is displayed in color to indicate that the cell is being edited.
Row-row index of the cell to be drawn. When the header is drawn, the row value is-1
Column-column index of the cell to be drawn

.

import java.awt.BorderLayout;   import java.awt.Color;   import java.awt.Component;     import javax.swing.JFrame;   import javax.swing.JScrollPane;   import javax.swing.JTable;   import javax.swing.table.DefaultTableCellRenderer;   import javax.swing.table.TableCellRenderer;     class EvenOddRenderer implements TableCellRenderer {       public static final DefaultTableCellRenderer DEFAULT_RENDERER =       new DefaultTableCellRenderer();       public Component getTableCellRendererComponent(JTable table, Object value,         boolean isSelected, boolean hasFocus, int row, int column) {       Component renderer =         DEFAULT_RENDERER.getTableCellRendererComponent(table, value,         isSelected, hasFocus, row, column);       Color foreground, background;       if (isSelected) {         foreground = Color.YELLOW;         background = Color.GREEN;       }  else {         if (row % 2 == 0) {           foreground = Color.BLUE;           background = Color.WHITE;         }  else {           foreground = Color.WHITE;           background = Color.BLUE;         }       }       renderer.setForeground(foreground);       renderer.setBackground(background);       return renderer;     }   }   public class ResizeTable {     public static void main(String args[]) {         final Object rowData[][] = {            { "1", "one",  "I" },           { "2", "two",  "II" },           { "3", "three", "III" }};       final String columnNames[] = { "#", "English", "Roman" };         final JTable table = new JTable(rowData, columnNames);       JScrollPane scrollPane = new JScrollPane(table);         table.setDefaultRenderer(Object.class,new EvenOddRenderer());                  JFrame frame = new JFrame("Resizing Table");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         frame.add(scrollPane, BorderLayout.CENTER);         frame.setSize(300, 150);       frame.setVisible(true);       }   }  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.