KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Best books about JAVA programming:

JAVA JTable JComponent

The following code show to add any JComponent in a JTablecell. Sample shows Jbuttons, Jlabels, Jcombobox, Jscrolbar, JCheckbox and radio buttons . These components behave in the regular manner to mouse actions.

The JComponentCellRenderer implements the TableCellRenderer and its single method simply returns the component in the cell to be rendered.

  • Table.java
  • JComponentCellEditor.java



    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;

    public class Table
    {
    public static void main(String[] args)
    {
    JFrame frame = new JFrame("Table");
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing(WindowEvent e)
    {
    Window win = e.getWindow();
    win.setVisible(false);
    win.dispose();
    System.exit(0);
    }
    } );

    JTable table = new JTable( 6,4 )
    {
    public TableCellRenderer getCellRenderer(int row, int column) {
    TableColumn tableColumn = getColumnModel().getColumn(column);
    TableCellRenderer renderer = tableColumn.getCellRenderer();
    if (renderer == null) {
    Class c = getColumnClass(column);
    if( c.equals(Object.class) )
    {
    Object o = getValueAt(row,column);
    if( o != null )
    c = getValueAt(row,column).getClass();
    }
    renderer = getDefaultRenderer(c);
    }
    return renderer;
    }

    public TableCellEditor getCellEditor(int row, int column) {
    TableColumn tableColumn = getColumnModel().getColumn(column);
    TableCellEditor editor = tableColumn.getCellEditor();
    if (editor == null) {
    Class c = getColumnClass(column);
    if( c.equals(Object.class) )
    {
    Object o = getValueAt(row,column);
    if( o != null )
    c = getValueAt(row,column).getClass();
    }
    editor = getDefaultEditor(c);
    }
    return editor;
    }

    };

    // Buttons
    table.setValueAt( new JButton("Button"), 0, 0 );
    table.setValueAt( new JButton("Button"), 0, 1 );


    // Combobox
    JComboBox combo = new JComboBox( new String[] {"First", "Second", "Third"} );
    table.setValueAt( combo, 1, 1 );

    // Labels
    table.setValueAt( new JLabel("Label"), 1, 0 );
    JLabel label = new JLabel( "Label", new ImageIcon( table.getClass().getResource("new.gif") ),
    JLabel.LEFT);
    table.setValueAt( label, 1, 2 );

    // Scrollbar
    table.setValueAt( new JScrollBar(JScrollBar.HORIZONTAL), 2,1 );

    // RadioButtons
    JRadioButton b1 = new JRadioButton( "Group1 Button1" );
    JRadioButton b2 = new JRadioButton( "Group1 Button2" );
    JRadioButton b3 = new JRadioButton( "Group1 Button3" );

    ButtonGroup g1 = new ButtonGroup();
    g1.add( b1 );
    g1.add( b2 );
    g1.add( b3 );

    table.setValueAt( b1, 3, 0 );
    table.setValueAt( b2, 3, 1 );
    table.setValueAt( b3, 3, 2 );

    //checkbox
    JCheckBox c1 = new JCheckBox( "Check box1 " );
    JCheckBox c2 = new JCheckBox( "Check box2" );
    JCheckBox c3 = new JCheckBox( "Check box3" );
    table.setValueAt( c1, 4, 0 );
    table.setValueAt( c2, 4, 1 );
    table.setValueAt( c3,4, 2 );

    table.setDefaultRenderer( JComponent.class, new JComponentCellRenderer() );
    table.setDefaultEditor( JComponent.class, new JComponentCellEditor() );
    JScrollPane sp = new JScrollPane(table);


    frame.getContentPane().add( sp );
    frame.pack();
    frame.show();
    }
    }

    class JComponentCellRenderer implements TableCellRenderer
    {
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column) {
    return (JComponent)value;
    }
    }

Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer