Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JTable Column names

Reply
Thread Tools

JTable Column names

 
 
Ike
Guest
Posts: n/a
 
      09-20-2004
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(editorCompone nt, 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[i]==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[i]==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;
}


}


 
Reply With Quote
 
 
 
 
Paul Lutus
Guest
Posts: n/a
 
      09-20-2004
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

 
Reply With Quote
 
 
 
 
Ike
Guest
Posts: n/a
 
      09-20-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      09-21-2004
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.convertColu mnIndexToView(colNum));

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


BK
 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      09-21-2004
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.convertColu mnIndexToView(colNum));
>
> where "colNum" now refers to the model rather than the view.


Thank you, an excellent follow-up.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting a JTable inside a JTable cell? Tivo Escobar Java 1 04-12-2007 11:09 AM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
Different Renderer type per row in same column of JTable? RobE Java 2 08-12-2003 06:12 AM
cannot print JTable column header cluenek Java 0 07-18-2003 03:40 PM
Adjust column widths of JTable Gerhard Rapp Java 0 07-18-2003 02:58 PM



Advertisments