Board index » jbuilder » Setting column titles in JTable, TableDialogEditDemo example

Setting column titles in JTable, TableDialogEditDemo example


2004-11-28 02:00:11 AM
jbuilder1
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();
}
});
}
 
 

Re: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:
Quote

//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
 

Re:Setting column titles in JTable, TableDialogEditDemo example

On 11/29/2004 at 6:07:57 AM, Richard wrote:
Quote
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:
www.JPMcGrath.net/newsgroups/e-mail.html
 

{smallsort}

Re:Setting column titles in JTable, TableDialogEditDemo example

"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
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.
 

Re:Setting column titles in JTable, TableDialogEditDemo example

On 12/1/2004 at 5:04:16 PM, Richard wrote:
Quote
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.
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:
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:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Setting column titles in JTable, TableDialogEditDemo example

Thanks! That sounds like it will be very helpful.
Richard
 

Re: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" < XXXX@XXXXX.COM >wrote in message
Quote
Thanks! That sounds like it will be very helpful.

Richard

 

Re:Setting column titles in JTable, TableDialogEditDemo example

Hi,
"David Bolsover" < XXXX@XXXXX.COM >wrote in message
Quote
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
 

Re:Setting column titles in JTable, TableDialogEditDemo example

On 1/5/2005 at 7:05:29 PM, Richard M. Ulrich wrote:
Quote
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:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Setting column titles in JTable, TableDialogEditDemo example

"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
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
 

Re:Setting column titles in JTable, TableDialogEditDemo example

Richard M. Ulrich wrote:
Quote
"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.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
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache/1.html
 

Re:Setting column titles in JTable, TableDialogEditDemo example

Quote

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
 

Re:Setting column titles in JTable, TableDialogEditDemo example

On 1/5/2005 at 7:05:29 PM, Richard M. Ulrich wrote:
Quote
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?
Quote
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:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Setting column titles in JTable, TableDialogEditDemo example

Hi,
I'm afraid that was my fault. I'm working with Ric{*word*156} 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]" < XXXX@XXXXX.COM >wrote in message
Quote
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:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Setting column titles in JTable, TableDialogEditDemo example

"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
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