Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

setToolTips in a JTable

Thread view: 
Elmar Brauch - 29 Aug 2005 11:10 GMT
Hello,

I have a JTable and I want to give every tableheader a diffrent tooltip.
But I don't know, how I get the tableheaders for using the
setToolTip-method.
Can anyone help me?

Regards,
Elmar
Thomas Hawtin - 29 Aug 2005 11:55 GMT
> I have a JTable and I want to give every tableheader a diffrent tooltip.
> But I don't know, how I get the tableheaders for using the
> setToolTip-method.

By the looks of JTableHeader.getToolTipText you need to do replace the
TableColumn/JTableHeader header renderers with something like this:

        new TableCellRenderer() {
            public Component getTableCellRendererComponent(
                JTable table, Object value,
                boolean isSelected, boolean hasFocus,
                int row, int column
            ) {
                Component component =
                    oldRenderer.getTableCellRendererComponent(
                        table, value,
                        isSelected, hasFocus,
                        row, column
                    );
                if (row == -1) {
                    component.setToolTipText("Column: "+column);
                }
                return component;
            }
        }

(Usual disclaimer.)

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

IchBin - 29 Aug 2005 14:41 GMT
> Hello,
>
[quoted text clipped - 5 lines]
> Regards,
> Elmar

The following code implements the tool tips. Basically, it creates a
subclass of JTableHeader that overrides the getToolTipText(MouseEvent)
method so that it returns the text for the current column. To associate
the revised table header with the table, the JTable method
createDefaultTableHeader is overridden so that it returns an instance of
the JTableHeader subclass.

    protected String[] columnToolTips = {
        null,
        null,
        "The person's favorite sport to participate in",
        "The number of years the person has played the sport",
        "If checked, the person eats no meat"};
    ...

    JTable table = new JTable(new MyTableModel()) {
        ...

        //Implement table header tool tips.
        protected JTableHeader createDefaultTableHeader() {
            return new JTableHeader(columnModel) {
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int index = columnModel.getColumnIndexAtX(p.x);
                    int realIndex =
                            columnModel.getColumn(index).getModelIndex();
                    return columnToolTips[realIndex];
                }
            };
        }
    }

See http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Signature


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA  http://weconsultants.servebeer.com
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Christian Kaufhold - 01 Sep 2005 11:56 GMT
>         //Implement table header tool tips.
>         protected JTableHeader createDefaultTableHeader() {
>             return new JTableHeader(columnModel) {
>                 public String getToolTipText(MouseEvent e) {
>                     String tip = null;

                     int index = columnAtPoint(e.getPoint());

>                     int realIndex =
>                             columnModel.getColumn(index).getModelIndex();
[quoted text clipped - 3 lines]
>         }
>     }

Christian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.