JTable Column names

Discussion in 'Java' started by Ike, Sep 20, 2004.

  1. Ike

    Ike Guest

    I'm interested in using the following JTable - I forget where I found this
    code, BUT, it really shows nicely how to use controls in a JTable (in case
    someone searches the archives for that expression in the future - I am
    posting the whole code here). However, it does not use a TableModel (?) or
    at least I don;t see where it does. I'm wondering if someone can show me how
    to use the following to change the column names, in the seeming absence of a
    TableModel. Thanks, Ike

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


    public class Test {

    /** Creates a new instance of Test */
    public Test() {
    }
    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 );

    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();
    }
    --------------------------------------------------------------------------
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;

    public class JComponentCellRenderer implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object
    value,
    boolean isSelected, boolean hasFocus, int row, int column) {
    return (JComponent)value;
    }
    }
    ---------------------------------------------------------------------------
    import java.awt.Component;
    import java.awt.event.*;
    import java.awt.AWTEvent;
    import java.lang.Boolean;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.util.EventObject;
    import javax.swing.tree.*;
    import java.io.Serializable;
    import javax.swing.*;

    public class JComponentCellEditor implements TableCellEditor,
    TreeCellEditor, Serializable {

    protected EventListenerList listenerList = new EventListenerList();
    transient protected ChangeEvent changeEvent = null;

    protected JComponent editorComponent = null;
    protected JComponent container = null; // Can be tree or table
    public Component getComponent() {
    return editorComponent;
    }


    public Object getCellEditorValue() {
    return editorComponent;
    }

    public boolean isCellEditable(EventObject anEvent) {
    return true;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
    if( editorComponent != null && anEvent instanceof MouseEvent
    && ((MouseEvent)anEvent).getID() == MouseEvent.MOUSE_PRESSED ) {
    Component dispatchComponent =
    SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3 );
    MouseEvent e = (MouseEvent)anEvent;
    MouseEvent e2 = new MouseEvent( dispatchComponent,
    MouseEvent.MOUSE_RELEASED,
    e.getWhen() + 100000, e.getModifiers(), 3, 3, e.getClickCount(),
    e.isPopupTrigger() );
    dispatchComponent.dispatchEvent(e2);
    e2 = new MouseEvent( dispatchComponent,
    MouseEvent.MOUSE_CLICKED,
    e.getWhen() + 100001, e.getModifiers(), 3, 3, 1,
    e.isPopupTrigger() );
    dispatchComponent.dispatchEvent(e2);
    }
    return false;
    }

    public boolean stopCellEditing() {
    fireEditingStopped();
    return true;
    }

    public void cancelCellEditing() {
    fireEditingCanceled();
    }

    public void addCellEditorListener(CellEditorListener l) {
    listenerList.add(CellEditorListener.class, l);
    }

    public void removeCellEditorListener(CellEditorListener l) {
    listenerList.remove(CellEditorListener.class, l);
    }

    protected void fireEditingStopped() {
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
    if (listeners==CellEditorListener.class) {
    // Lazily create the event:
    if (changeEvent == null)
    changeEvent = new ChangeEvent(this);

    ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
    }
    }
    }

    protected void fireEditingCanceled() {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
    if (listeners==CellEditorListener.class) {
    // Lazily create the event:
    if (changeEvent == null)
    changeEvent = new ChangeEvent(this);

    ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
    }
    }
    }

    // implements javax.swing.tree.TreeCellEditor
    public Component getTreeCellEditorComponent(JTree tree, Object value,
    boolean isSelected, boolean expanded, boolean leaf, int row) {
    String stringValue = tree.convertValueToText(value,
    isSelected,
    expanded, leaf, row, false);

    editorComponent = (JComponent)value;
    container = tree;
    return editorComponent;
    }

    // implements javax.swing.table.TableCellEditor
    public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {

    editorComponent = (JComponent)value;
    container = table;
    return editorComponent;
    }


    }
     
    Ike, Sep 20, 2004
    #1
    1. Advertising

  2. Ike

    Paul Lutus Guest

    Ike wrote:

    > I'm interested in using the following JTable - I forget where I found this
    > code, BUT, it really shows nicely how to use controls in a JTable (in case
    > someone searches the archives for that expression in the future - I am
    > posting the whole code here). However, it does not use a TableModel (?) or
    > at least I don;t see where it does.


    Of course it does. It is a question of visibility, not presence.

    > I'm wondering if someone can show me
    > how to use the following to change the column names, in the seeming
    > absence of a TableModel.


    Your assumption is flawed, but the header value is not set by the table
    model anyway.

    TableColumn column = table.getColumnModel().getColumn(colNum);

    column.setHeaderValue(newValue);

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 20, 2004
    #2
    1. Advertising

  3. Ike

    Ike Guest

    Paul,

    Thanks Paul. I was under the (obviously mistaken) impression from a lengthy
    google search on the matter that I had to override getColumnName() in an
    AbstractTableModel derived TableModel to set the column names. Thanks a lot
    here for enlightening me - Ike

    "Paul Lutus" <> wrote in message
    news:...
    > Ike wrote:
    >
    > > I'm interested in using the following JTable - I forget where I found

    this
    > > code, BUT, it really shows nicely how to use controls in a JTable (in

    case
    > > someone searches the archives for that expression in the future - I am
    > > posting the whole code here). However, it does not use a TableModel (?)

    or
    > > at least I don;t see where it does.

    >
    > Of course it does. It is a question of visibility, not presence.
    >
    > > I'm wondering if someone can show me
    > > how to use the following to change the column names, in the seeming
    > > absence of a TableModel.

    >
    > Your assumption is flawed, but the header value is not set by the table
    > model anyway.
    >
    > TableColumn column = table.getColumnModel().getColumn(colNum);
    >
    > column.setHeaderValue(newValue);
    >
    > --
    > Paul Lutus
    > http://www.arachnoid.com
    >
     
    Ike, Sep 20, 2004
    #3
  4. Paul Lutus wrote:
    >
    >>I'm wondering if someone can show me
    >>how to use the following to change the column names, in the seeming
    >>absence of a TableModel.

    >
    >
    > Your assumption is flawed, but the header value is not set by the table
    > model anyway.
    >
    > TableColumn column = table.getColumnModel().getColumn(colNum);
    >
    > column.setHeaderValue(newValue);
    >


    One caveat here :

    The OP needs to also remember that "colNum" here would refer to the
    column number in the View - not the model. So if the user reorders the
    columns by dragging on the headers, you might end up assigning the
    header value to the wrong column if you aren't careful. (This can of
    course happen only if the above code is executed at some later phase
    after the table has been shown).

    A more robust approach would be to modify the first line to :

    TableColumn column =
    table.getColumnModel().getColumn(table.convertColumnIndexToView(colNum));

    where "colNum" now refers to the model rather than the view.


    BK
     
    Babu Kalakrishnan, Sep 21, 2004
    #4
  5. Ike

    Paul Lutus Guest

    Babu Kalakrishnan wrote:

    / ...

    > One caveat here :
    >
    > The OP needs to also remember that "colNum" here would refer to the
    > column number in the View - not the model. So if the user reorders the
    > columns by dragging on the headers, you might end up assigning the
    > header value to the wrong column if you aren't careful. (This can of
    > course happen only if the above code is executed at some later phase
    > after the table has been shown).
    >
    > A more robust approach would be to modify the first line to :
    >
    > TableColumn column =
    > table.getColumnModel().getColumn(table.convertColumnIndexToView(colNum));
    >
    > where "colNum" now refers to the model rather than the view.


    Thank you, an excellent follow-up.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Sep 21, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerhard Rapp

    Adjust column widths of JTable

    Gerhard Rapp, Jul 18, 2003, in forum: Java
    Replies:
    0
    Views:
    2,194
    Gerhard Rapp
    Jul 18, 2003
  2. cluenek
    Replies:
    0
    Views:
    730
    cluenek
    Jul 18, 2003
  3. Tivo Escobar
    Replies:
    1
    Views:
    7,348
    manusa
    Apr 12, 2007
  4. Tintin92
    Replies:
    1
    Views:
    1,716
    Andrew Thompson
    Feb 14, 2007
  5. Haircuts Are Important
    Replies:
    3
    Views:
    332
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page