![]() This object is thrown away once JTable finishes rendering. Renderers always return a new rendering component object when getTableCellRendererComponent Instead, it always returns the same cached JLabel object. TableHeaderIcons' getTableCellRendererComponent method doesn'tĬreate a separate copy of the JLabel object each time this method isĬalled. Return the cached JLabel a the renderer for this column L.setBorder (((JComponent) c).getBorder ()) JLabel so that the rendered header will look the same as the Establish the font, foreground color, and border for the Extract the component used to render the column header.Ĭomponent c = tcr.getTableCellRendererComponent (table, value, TableCellRenderer tcr = table.getTableHeader () Extract the original header renderer for this column. Public Component getTableCellRendererComponent (JTable table, Private JLabel l = new JLabel (new ImageIcon ("icon.gif")) Create a JLabel for use as a renderer and pre-load this label ![]() New TableHeaderIcons ("Table Header Icons") Ĭlass MyRenderer implements TableCellRenderer Add the JScrollPane to the JFrame's content pane. tPreferredScrollableViewportSize (new Dimension (250, Set the JTable's preferred "viewport" size for viewing JScrollPane sp = new JScrollPane (table) Create a JScrollPane and add the JTable to this container. Table.getColumn ("A").setHeaderRenderer (new MyRenderer ()) Assign a custom renderer to column A, so that an icon can Create the JTable and assign it the predefined data model. Public void setValueAt (Object value, int row, int col) Public boolean isCellEditable (int row, int col) Public Object getValueAt (int row, int col) is created from this subclass of the AbstractTableModel This instance initializer creates the data when an object TableModel dataModel = new AbstractTableModel () SetDefaultCloseOperation (EXIT_ON_CLOSE) either selects Close from the System menu or clicks the X Exit the application by calling System.exit when the user contents of title appear in the JFrame's title bar. Pass the title argument to the JFrame superclass so that the Listing 5 The TableHeaderIcons application source code // TableHeaderIcons.java ThisĪpplication was used to generate the GUI in Figure 8 and shows how easy it is Listing 5 presents source code to a TableHeaderIcons application. With an object created from the renderer class. Then call TableColumn's setHeaderRenderer method Method to retrieve a reference to a TableColumn object that describes To specify an icon for a particular column, call JTable's getColumn To a JLabel constructor during the creation of a JLabel object.Īfter making a few other changes, this JLabel object can be returned The above code fragment creates an ImageIcon object that loads an icon Represents the rendering component: JLabel l = new JLabel (new ImageIcon ("icon.gif")) The following code fragment creates a JLabel object that What component can be used to perform this rendering? How about a componentīased on the JLabel class? After all, JLabel is capable of renderingĪn icon image. In turn, this method returns a component capable of rendering the column That implements the TableCellRenderer interface by providing a getTableCellRendererComponent To display an icon in a column header, first establish a renderer (aĬomponent that draws graphics) for that column header. Special Edition Using Java 2, Standard Editionĭid you know that icon images can be displayed in a table component's columnĪssigning icons to a JTable component's header columns makes them
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |