[Original] Set JTABLE

xiaoxiao2021-03-06  74

[Original] Set JTABLE

Q: How do I set color and fonts for a specified Cell in JTABLE?

A: To set color or fonts to the specified table Cell, you must customize TableCellrenderer. The easiest way is to expand DefaultRenderenderer. The following custom TableCellRendere supports the Cell to support an integer, and the Cell color is red when an integer is negative:

Import java.awt.component; import java.awt.color; import javax.swing.jtable; import javax.swing.table.defaultTableCellRenderer;

public class CustomTableCellRenderer extends DefaultTableCellRenderer {public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {Component cell = super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);

IF (value instanceof integer) {integer amount = (integer) value; if (amount.intValue () <0) {cell.setback; // you can also customize the font and foreground this way // Cell .setForeground (); // cell.setfont (); else {cell.setBackground (color.white);}}

Return Cell;

}

The above Cell renderer first checks to make sure it works integer. As a result, the renderer only supports an integer instance. Once the renderer determines the appropriate type of Cell, it will detect if the value in the cell is less than zero. If less than zero, the renderer will render Cell with red. If not, the renderer will use white to render Cell. You also notice that you can also change the foreground color and fonts of Cell. The above custom TableCellrendere is quite simple, but the renderer can be complicated according to your needs. GetTableCellrendererComponent () is passed to the table, the parameter is: the object owned by Cell, whether the Cell is selected, whether the Cell gets the focus, the number of Cell numbers and the column of Cell. You can use these values ​​or use other values ​​already in the renderer instance. Let our rendere begin to work. First, we need a TableModel to save the displayed data:

Import javax.swing.table.abstractTableModel;

public class ExampleTableModel extends AbstractTableModel {private final String [] columnNames = { "Month", "Income"}; final Object [] [] data = {{ "January", new Integer (150)}, { "February", new Integer (500)}, {"march", new integer (54)}, {"april", new integer (-50)}, {"may", new integer (52)}, {"june", new integer (74)}, {"july", new integer (-25)}, {"August", new integer (62)}, {"september", new integer (15)}, {"October", new integer -5)}} {"November", new integer (5)}, {"decEmber", new integer (59)}}

Public Class getColumnClass (int column) {return getValueat (0, column) .getclass ();

Public int getColumncount () {return columnnames.length;}

Public String getColumnName (int column) {return columnnames [color ";

Public int getRowcount () {return data.length;}

Public Object GetValueat (int Row, int column) {return data [row] [column];}}

This TableModel saves a fictional commercial transaction income for a year. The following main () method creates a Table and has used previous custom Cell renderers:

Import java.awt.event.windowadapter; import java.awt.event.windowEvent; import javax.swing.table.tablecellrendere; import javax.swing.jframe; import javax.swing.jtable;

Public class TableCellexample {public static void main (string [] args) {jtable table = new jtable (new exampletablemodel ()); TableCellrenderer renderer = New CustomTableCellrenderer ();

try {table.setDefaultRenderer (Class.forName ( "java.lang.Integer"), renderer);} catch (ClassNotFoundException ex) {System.exit (0);} JFrame frame = new JFrame (); frame.addWindowListener (new Windowadapter () {public void windowclosing (windowevent e) {system.exit (0);}});

Frame.getContentPane (). add (table); frame.pack (); frame.setvisible (TRUE);

}

Table.SetDefaultRenderer ("Java.LANG.INTEGER"), the renderer code snippet tells Table to any Cell renderer that contains integer data. You can also set the Table RENDERER by setting the appropriate TableColumn object directly.

转载请注明原文地址:https://www.9cbs.com/read-121020.html

New Post(0)