Setting column titles in JTable, TableDialogEditDemo example  
Author Message
Richard





PostPosted: 2004-11-28 2:00:00 Top

java-programmer, Setting column titles in JTable, TableDialogEditDemo example Hello,

This is no doubt a really dumb question, but I don't see where in the
example TableDialogEditDemo.java (from Sun's Java examples) that we "tell"
the table to have one fixed row, to make it grey, and to use getColumnName()
for the column headers? (Stated another way, what would I do differently if
I didn't want the column titles to be grey, for instance?) The full java
listing is below.

Thanks,
Richard Ulrich


/*
* TableDialogEditDemo.java is a 1.4 application that requires these files:
* ColorRenderer.java
* ColorEditor.java
*/

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* This is like TableDemo, except that it substitutes a
* Favorite Color column for the Last Name column and specifies
* a custom cell renderer and editor for the color data.
*/
public class TableDialogEditDemo extends JPanel {
private boolean DEBUG = false;

public TableDialogEditDemo() {
super(new GridLayout(1,0));

JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Set up renderer and editor for the Favorite Color column.
table.setDefaultRenderer(Color.class,
new ColorRenderer(true));
table.setDefaultEditor(Color.class,
new ColorEditor());

//Add the scroll pane to this panel.
add(scrollPane);
}

class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Favorite Color",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Mary", new Color(153, 0, 153),
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", new Color(51, 51, 153),
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", new Color(51, 102, 51),
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", Color.red,
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", Color.pink,
"Pool", new Integer(10), new Boolean(false)}
};

public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 1) {
return false;
} else {
return true;
}
}

public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}

data[row][col] = value;
fireTableCellUpdated(row, col);

if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}

private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("TableDialogEditDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new TableDialogEditDemo(); //This is an
extension of JPanel
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}


 
Richard





PostPosted: 2004-11-29 19:08:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example The answer to my own question: the labels are displayed by the JScrollPane
rather than the JTable, and then only if the table is used when the
JScrollPane is created:

>
> //Create the scroll pane and add the table to it.
> JScrollPane scrollPane = new JScrollPane(table);

The comment above is misleading, as first creating the JScrollPane and then
adding the table (as one might do in the designer) doesn't suffice.

R


 
John McGrath [TeamB]





PostPosted: 2004-12-1 10:05:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example On 11/29/2004 at 6:07:57 AM, Richard wrote:

> The answer to my own question: the labels are displayed by the
> JScrollPane rather than the JTable, and then only if the table is used
> when the JScrollPane is created:

To be a little more precise: The labels are displayed by a JTableHeader
component, which is created by the JTable (although you can provide your
own). *If* the JTable is placed in the JViewport of a JScrollPane, then
the JScrollPane will install the JTableHeader into the row header of the
JScrollPane. If you do not place the JTable in a JScrollPane, then the
JTableHeader is not automatically added to the UI. But if you want, you
can do that yourself.

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
 
 
Richard





PostPosted: 2004-12-2 6:04:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example
"John McGrath [TeamB]" <email***@***.com> wrote in message
news:email***@***.com...
> On 11/29/2004 at 6:07:57 AM, Richard wrote:

> To be a little more precise: The labels are displayed by a JTableHeader
> component, which is created by the JTable (although you can provide your
> own). *If* the JTable is placed in the JViewport of a JScrollPane, then
> the JScrollPane will install the JTableHeader into the row header of the
> JScrollPane. If you do not place the JTable in a JScrollPane, then the
> JTableHeader is not automatically added to the UI. But if you want, you
> can do that yourself.
>

Ok. Is there a similarly easy (or at least elegant) way to get row headers
(i.e., a JLabel name for each row)? I've searched the newsgroups and the
help system, and it looks like the only way is to manually use column 1 for
the row labels.

Thanks,
Richard U.


 
 
John McGrath [TeamB]





PostPosted: 2004-12-2 11:27:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example On 12/1/2004 at 5:04:16 PM, Richard wrote:

> Ok. Is there a similarly easy (or at least elegant) way to get row
> headers (i.e., a JLabel name for each row)? I've searched the
> newsgroups and the help system, and it looks like the only way is to
> manually use column 1 for the row labels.

There is an analogous way to place the row headers: a JScrollPane has a
row header as well as a column header, so you just need to place the row
header component there and the JScrollPane will handle the scrolling.

There is no Swing component for row headers that is analogous to the
JTableHeader, but it can be pretty easy to write, especially if you use
fixed row heights. You just need to use another JTable, with your own
TableCellRenderer to make the headers look like you want them to look.
But if your row heights vary, then you have to deal with synchronizing
them.

One good place to figure out how to do odd things in Swing was always the
"tame" Swing examples site. A guy named Nobuo Tamemasa had a very large
site with examples of doing just about anything you might want to do with
Swing. Unfortunately, the site went off-line, and nobody seems to know
what happened to the guy. But fortunately, some people managed to save
the examples from his site, and you can find them pretty easily on the net.

The following is a nicely organized site allows you to browse through the
Tamemasa examples, with screen shots and source code for each one.

http://www.objects.com.au/java/examples.do

This site is not as well suited to browsing. It has some screen shots,
but I do not think you can see the source code directly. But it has a zip
file with the entire collection available for download:

http://www.physci.org/codes/tame/index.jsp

One last note: The exact legal status of the source code is not clear.
Nobuo Tamemasa had these examples posted on the web for several years
without any copyright notice, so it is very reasonable to assume that he
expected people to use them. But he is nowhere to be found, and AFAIK he
never gave anyone explicit permission to post them elsewhere. It is safe
to use the examples for learning Swing, since that was the intent of his
site, but I would definitely not recommend cutting a book deal where you
plan to publish them. :o)

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
 
 
Richard Ulrich





PostPosted: 2004-12-3 3:01:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example Thanks! That sounds like it will be very helpful.

Richard


 
 
David Bolsover





PostPosted: 2004-12-8 3:51:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example Just in case you missed it while working on JTable...

You should check out org.jdesktop.swing.table.JXTable - this first rate
component makes JTable look vey dull.

db


"Richard Ulrich" <email***@***.com> wrote in message
news:41af6646$email***@***.com...
> Thanks! That sounds like it will be very helpful.
>
> Richard
>


 
 
Richard M. Ulrich





PostPosted: 2005-1-6 8:05:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example Hi,

"David Bolsover" <email***@***.com> wrote in message
news:email***@***.com...
> Just in case you missed it while working on JTable...
>
> You should check out org.jdesktop.swing.table.JXTable - this first rate
> component makes JTable look vey dull.
>

We have come to the (possibly erroneous) conclusion that the
JScrollPane/JTable paradigm is hopelessly buggy, at least with respect to
the sorts of things we need to do. I'm not interested in "less dull" I'm
interested in "working." The problems we are running into are primarily
associated with scrolled column headers getting hopelessly out of synch or
with the body of the table (or downright mangled) when one scrolls to the
right and then back. Also, resizing columns doesn't work too well.

Does JXTable fix any of these problems?

Thanks

--
Richard M. Ulrich
Ulrich Associates, Inc.
Specializing in renovation and refactoring of scientific software


 
 
John McGrath [TeamB]





PostPosted: 2005-1-6 15:45:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example On 1/5/2005 at 7:05:29 PM, Richard M. Ulrich wrote:

> We have come to the (possibly erroneous) conclusion that the
> JScrollPane/JTable paradigm is hopelessly buggy, ...

> The problems we are running into are primarily associated with
> scrolled column headers getting hopelessly out of synch or with
> the body of the table (or downright mangled) when one scrolls to
> the right and then back. Also, resizing columns doesn't work too
> well.

Scrolling and resizing has always worked fine for me. Can you post
a small runnable example that exhibits the problem?

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
 
 
Richard M. Ulrich





PostPosted: 2005-1-7 3:07:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example "John McGrath [TeamB]" <email***@***.com> wrote in message
news:email***@***.com...
> Scrolling and resizing has always worked fine for me. Can you post
> a small runnable example that exhibits the problem?

John,

Yes, we prepared a stripped down (only 3K zipped) running application that
demonstrates the problem. I see that these newsgroups do not accept
attachments. Is there a Borland public FTP site that I can put it on?

R



 
 
Lori M Olson [TeamB]





PostPosted: 2005-1-7 3:57:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example Richard M. Ulrich wrote:

> "John McGrath [TeamB]" <email***@***.com> wrote in message
> news:email***@***.com...
>
>>Scrolling and resizing has always worked fine for me. Can you post
>>a small runnable example that exhibits the problem?
>
>
> John,
>
> Yes, we prepared a stripped down (only 3K zipped) running application that
> demonstrates the problem. I see that these newsgroups do not accept
> attachments. Is there a Borland public FTP site that I can put it on?
>
> R
>

There is one group just for attachments. borland.public.attachments.
Post it there, and then post back here to let us know it is there, and
what subject your posting there has so we can find it.

--

Regards,

Lori Olson [TeamB]

------------

Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.

Google Advanced Newsgroup Search
http://www.google.ca/advanced_group_search
Other Newsgroup Searches:
http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
http://www.visi.com/~gyles19/fom-serve/cache/1.html

 
 
Richard M. Ulrich





PostPosted: 2005-1-7 4:10:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example >
> There is one group just for attachments. borland.public.attachments. Post
> it there, and then post back here to let us know it is there, and what
> subject your posting there has so we can find it.

Ok, I just posted it with subject "Problematic JTable example."

Thanks,
R


 
 
John McGrath [TeamB]





PostPosted: 2005-1-7 8:29:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example On 1/5/2005 at 7:05:29 PM, Richard M. Ulrich wrote:

> The problems we are running into are primarily associated with scrolled
> column headers getting hopelessly out of synch or with the body of the
> table (or downright mangled) when one scrolls to the right and then back.

I cannot reproduce this. The table's AutoResizeMode property is set to
AUTO_RESIZE_NEXT_COLUMN, which means that the table will always be the
same width as the scroll pane's viewport. As a result, the horizontal
scrollbar is never shown, so I cannot scroll to the right. Can you give
me steps to reproduce the problem you are seeing?

> Also, resizing columns doesn't work too well.

It seems to work as expected. When a column width is changed, it adjusts
the width of the next column so as to fit in the window. This is what it
is supposed to do when the AutoResizeMode is AUTO_RESIZE_NEXT_COLUMN.
Have you tried other AutoResizeMode values?

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
 
 
Mike Baden





PostPosted: 2005-1-7 9:55:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example Hi,

I'm afraid that was my fault. I'm working with Richard on this and wrote up
a document showing the problems. The last figure in the document showed the
response when I set AutoResizeMode to "..NEXT_COLUMN". I zipped up the code
forgetting to set that back to "OFF". And yes, we see the same behavior
with that setting (all columns visible, but squeezed to fit). If you set
AutoResizeMode to "OFF" you should see the column label problems we've been
seeing (as shown in the document Richard posted in the attachments
newsgroup).

Mike



"John McGrath [TeamB]" <email***@***.com> wrote in message
news:email***@***.com...
> On 1/5/2005 at 7:05:29 PM, Richard M. Ulrich wrote:
>
>> The problems we are running into are primarily associated with scrolled
>> column headers getting hopelessly out of synch or with the body of the
>> table (or downright mangled) when one scrolls to the right and then back.
>
> I cannot reproduce this. The table's AutoResizeMode property is set to
> AUTO_RESIZE_NEXT_COLUMN, which means that the table will always be the
> same width as the scroll pane's viewport. As a result, the horizontal
> scrollbar is never shown, so I cannot scroll to the right. Can you give
> me steps to reproduce the problem you are seeing?
>
>> Also, resizing columns doesn't work too well.
>
> It seems to work as expected. When a column width is changed, it adjusts
> the width of the next column so as to fit in the window. This is what it
> is supposed to do when the AutoResizeMode is AUTO_RESIZE_NEXT_COLUMN.
> Have you tried other AutoResizeMode values?
>
> --
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
> http://www.JPMcGrath.net/newsgroups/e-mail.html


 
 
Richard M. Ulrich





PostPosted: 2005-1-7 21:50:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example "John McGrath [TeamB]" <email***@***.com> wrote in message
news:email***@***.com...

> I cannot reproduce this. The table's AutoResizeMode property is set to
> AUTO_RESIZE_NEXT_COLUMN, which means that the table will always be the
> same width as the scroll pane's viewport. As a result, the horizontal
> scrollbar is never shown, so I cannot scroll to the right. Can you give
> me steps to reproduce the problem you are seeing?

Sorry, we had changed the property to AUTO_RESIZE_NEXT_COLUMN to generate
the last figure of the doc, and forgot to switch it back. Change
AutoResizeMode to to "OFF" in order to see the problem behavior.

Thanks,

R


 
 
Arthur Ore





PostPosted: 2005-1-8 4:30:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example Sounds like this bug :-

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4473075

I played with your code and setting the header preferred size as per the
work-around seems to fix the problem. I didn't get the caveat at the end.

Arth

"Richard M. Ulrich" <email***@***.com> wrote in message
news:41de9366$email***@***.com...
> "John McGrath [TeamB]" <email***@***.com> wrote in message
> news:email***@***.com...
>
>> I cannot reproduce this. The table's AutoResizeMode property is set to
>> AUTO_RESIZE_NEXT_COLUMN, which means that the table will always be the
>> same width as the scroll pane's viewport. As a result, the horizontal
>> scrollbar is never shown, so I cannot scroll to the right. Can you give
>> me steps to reproduce the problem you are seeing?
>
> Sorry, we had changed the property to AUTO_RESIZE_NEXT_COLUMN to generate
> the last figure of the doc, and forgot to switch it back. Change
> AutoResizeMode to to "OFF" in order to see the problem behavior.
>
> Thanks,
>
> R
>


 
 
Richard M. Ulrich





PostPosted: 2005-1-8 5:12:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example Arthur,

> Sounds like this bug :-
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4473075
>

I think it is EXACTLY that bug! I wonder why it is that a bug this
important, reported in version 1.3 back in 2001, hasn't been fixed yet!
We've wasted a LOT of time.

> I played with your code and setting the header preferred size as per the
> work-around seems to fix the problem. I didn't get the caveat at the end.

Thanks, we'll give it a try.

Richard


 
 
John McGrath [TeamB]





PostPosted: 2005-1-8 13:30:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example On 1/7/2005 at 3:30:16 PM, Arthur Ore wrote:

> Sounds like this bug :-
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4473075

That does seem to describe the problem with the header, which is certainly
rather strange. However, it does not deal with the column resize problem.

I think the problem there is that it really does not make sense to specify
a preferred size for a JTable with AutoResizeMode set to OFF. The JTable
is placed inside the JViewport of a JScrollPane. The JViewPort will
always try to accommodate the preferred size of the component that it
contains, so when it is validated, it sets the table to its preferred
size. And when its size is set, the JTable will distribute the columns so
as to fit within
its width.

When you try to resize a column and AutoResizeMode is OFF, that causes the
table size to change, which results in a revalidation. Again, the
viewport will set the table to its preferred size and the table will fit
the columns to that size. So AutoResizeMode=OFF pretty much requires that
the table be allowed to set its own preferred width.

What I do not understand is, what is the point of setting the preferred
size of the JTable? If this is designed to control the initial width of
the columns, that is really not the way to do it. Instead, you can set
the preferred width of the individual columns, and the JTable will
determine its own preferred width based on that. And then, when the user
resizes a column, the preferred width will change as well, so the viewport
will not change the size back to its old preferred size.

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
 
 
Richard M. Ulrich





PostPosted: 2005-1-9 6:40:00 Top

java-programmer >> Setting column titles in JTable, TableDialogEditDemo example "John McGrath [TeamB]" <email***@***.com> wrote in message
news:email***@***.com...
> What I do not understand is, what is the point of setting the preferred
> size of the JTable? If this is designed to control the initial width of
> the columns, that is really not the way to do it. Instead, you can set
> the preferred width of the individual columns, and the JTable will
> determine its own preferred width based on that. And then, when the user
> resizes a column, the preferred width will change as well, so the viewport
> will not change the size back to its old preferred size.

John,

Setting the preferred width of each column is a much better solution! It
seems to work fine.

Thanks,
Richard