JTable Column names

I

Ike

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;
}


}
 
P

Paul Lutus

Ike said:
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);
 
I

Ike

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
 
B

Babu Kalakrishnan

Paul said:
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
 
P

Paul Lutus

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top