🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
19 messages
19 total messages Started by Richard Sat, 27 Nov 2004 18:00
Setting column titles in JTable, TableDialogEditDemo example
#918
Author: Richard
Date: Sat, 27 Nov 2004 18:00
179 lines
7065 bytes
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
#921
Author: Richard
Date: Mon, 29 Nov 2004 11:07
12 lines
1452 bytes
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 
Re: Setting column titles in JTable, TableDialogEditDemo example
#922
Author: John McGrath [TeamB]
Date: Wed, 01 Dec 2004 02:05
22 lines
1830 bytes
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
Re: Setting column titles in JTable, TableDialogEditDemo example
#923
Author: Richard
Date: Wed, 01 Dec 2004 22:04
21 lines
2032 bytes
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
news:xn0dqe27g17u5no001@newsgroups.borland.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. 
Re: Setting column titles in JTable, TableDialogEditDemo example
#924
Author: John McGrath [TeamB]
Date: Thu, 02 Dec 2004 03:27
53 lines
3699 bytes
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
Re: Setting column titles in JTable, TableDialogEditDemo example
#925
Author: Richard Ulrich
Date: Thu, 02 Dec 2004 19:00
3 lines
1286 bytes
Thanks!  That sounds like it will be very helpful.

Richard 
Re: Setting column titles in JTable, TableDialogEditDemo example
#751
Author: David Bolsover
Date: Tue, 07 Dec 2004 19:50
14 lines
1713 bytes
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" <rm_ulrich@hotmail.com> wrote in message 
news:41af6646$1@newsgroups.borland.com...
> Thanks!  That sounds like it will be very helpful.
>
> Richard
> 
Re: Setting column titles in JTable, TableDialogEditDemo example
#725
Author: Richard M. Ulrich
Date: Thu, 06 Jan 2005 00:05
26 lines
2297 bytes
Hi,

"David Bolsover" <junk1@bolsover.com> wrote in message 
news:41b60985@newsgroups.borland.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 
Re: Setting column titles in JTable, TableDialogEditDemo example
#727
Author: John McGrath [TeamB]
Date: Thu, 06 Jan 2005 07:45
22 lines
2002 bytes
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
Re: Setting column titles in JTable, TableDialogEditDemo example
#730
Author: Richard M. Ulrich
Date: Thu, 06 Jan 2005 19:06
12 lines
1825 bytes
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
news:xn0dwx6zn4qt0cl008@newsgroups.borland.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
Re: Setting column titles in JTable, TableDialogEditDemo example
#731
Author: Lori M Olson [TeamB]
Date: Thu, 06 Jan 2005 19:56
40 lines
2569 bytes
Richard M. Ulrich wrote:

> "John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
> news:xn0dwx6zn4qt0cl008@newsgroups.borland.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
Re: Setting column titles in JTable, TableDialogEditDemo example
#732
Author: Richard M. Ulrich
Date: Thu, 06 Jan 2005 20:09
9 lines
1792 bytes
>
> 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
#734
Author: John McGrath [TeamB]
Date: Fri, 07 Jan 2005 00:29
27 lines
2361 bytes
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
Re: Setting column titles in JTable, TableDialogEditDemo example
#735
Author: Mike Baden
Date: Fri, 07 Jan 2005 01:55
44 lines
3359 bytes
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]" <ng-b@JPMcGrath.net> wrote in message 
news:xn0dwxxgt5qojdz00h@newsgroups.borland.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 
Re: Setting column titles in JTable, TableDialogEditDemo example
#737
Author: Richard M. Ulrich
Date: Fri, 07 Jan 2005 13:49
16 lines
1940 bytes
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
news:xn0dwxxgt5qojdz00h@newsgroups.borland.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 
Re: Setting column titles in JTable, TableDialogEditDemo example
#743
Author: Arthur Ore
Date: Fri, 07 Jan 2005 20:30
28 lines
2458 bytes
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" <RichardU@UlrichAssociates.com> wrote in message 
news:41de9366$1@newsgroups.borland.com...
> "John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
> news:xn0dwxxgt5qojdz00h@newsgroups.borland.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
> 
Re: Setting column titles in JTable, TableDialogEditDemo example
#744
Author: Richard M. Ulrich
Date: Fri, 07 Jan 2005 21:11
17 lines
1873 bytes
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 
Re: Setting column titles in JTable, TableDialogEditDemo example
#658
Author: John McGrath [TeamB]
Date: Sat, 08 Jan 2005 05:29
40 lines
3208 bytes
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
Re: Setting column titles in JTable, TableDialogEditDemo example
#665
Author: Richard M. Ulrich
Date: Sat, 08 Jan 2005 22:39
17 lines
2102 bytes
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
news:xn0dwzwku7gu3k900p@newsgroups.borland.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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads