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 / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Hiding table columns?

Thread view: 
Luc Van Bogaert - 16 Mar 2006 21:40 GMT
Hi,

I'm no professional programmer, but I suppose I know enough about the
Table API to do most of the things I need. Now I'm trying to figure
out a good way to implement a functionality into my apps to toggle the
visibility of table columns.

I wonder if anyone here can provide some tips on how to handle this as
the TableColumn class doesn't seem to provide a setVisible() method.

All my tables use a TableModel, so I suspect that's where I need to do
most of the stuff. I'm thinking of setting up some kind of
two-dimensional array to hold the column names, and a visibility flag
for each column. In the TableModel methods I could then use that flag
to determine what columns and data should be processed.

Any help is much appreciated.

Signature

Luc Van Bogaert

IchBin - 16 Mar 2006 21:58 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>
> Any help is much appreciated.

I have not tried this but here goes..

Making cells/rows/columns visible

Single cells can be made visible with
table.scrollRectToVisible(table.getCellRect(row, column, true));

For rows/columns or cell ranges the actual works can be delegated to
Scrolling. You only need to get the rectangle in question.

public static void makeRowVisible(JTable table, int row)
{
    Scrolling.scrollVertically(table, getRowBounds(table, row));
}
public static void makeRowsVisible(JTable table, int first, int last,
int bias)
{
    Scrolling.scrollVertically(table, getRowBounds(table, first, last),
bias);
}

and completely analogous for columns.

Testing whether cells/rows/columns are visible

public static boolean areRowsVisible(JTable table, int first, int last)
{
    return Scrolling.isVerticallyVisible(table, getRowBounds(table,
first, last));
}

Example I found:_____________________________

import java.awt.Rectangle;
import java.awt.Insets;
import javax.swing.JTable;

public abstract class JTableScrolling
{
    private JTableScrolling()
    {
    }

    public static Rectangle getRowBounds(JTable table, int row)
    {
        checkRow(table, row);

        Rectangle result = table.getCellRect(row, -1, true);
        Insets i = table.getInsets();

        result.x = i.left;
        result.width = table.getWidth() - i.left - i.right;

        return result;
    }

    public static Rectangle getRowBounds(JTable table, int first, int last)
    {
        checkRows(table, first, last);

        Rectangle result = table.getCellRect(first, -1, true);
        result = result.union(table.getCellRect(last, -1, true));
        Insets i = table.getInsets();

        result.x = i.left;
        result.width = table.getWidth() - i.left - i.right;

        return result;
    }

    public static Rectangle getColumnBounds(JTable table, int column)
    {
        checkColumn(table, column);

        Rectangle result = table.getCellRect(-1, column, true);
        Insets i = table.getInsets();

        result.y = i.top;
        result.height = table.getHeight() - i.top - i.bottom;

        return result;
    }

    public static Rectangle getColumnBounds(JTable table, int first,
int last)
    {
        checkColumns(table, first, last);

        Rectangle result = table.getCellRect(-1, first, true);
        result = result.union(table.getCellRect(-1, last, true));
        Insets i = table.getInsets();

        result.y = i.top;
        result.height = table.getHeight() - i.top - i.bottom;

        return result;
    }

    /** For completeness. Only allows valid rows/columns. */
    public static Rectangle getCellBounds(JTable table, int row, int
column)
    {
        checkCell(table, row, column);

        return table.getCellRect(row, column, true);
    }

    public static Rectangle getCellBounds(JTable table, int firstRow,
int lastRow, int firstColumn, int lastColumn)
    {
        checkCells(table, firstRow, lastRow, firstColumn, lastColumn);

        Rectangle result = table.getCellRect(firstRow, firstColumn, true);
        return result.union(table.getCellRect(lastRow, lastColumn, true));
    }

    public static void makeRowVisible(JTable table, int row)
    {
        Scrolling.scrollVertically(table, getRowBounds(table, row));
    }

    public static void makeColumnVisible(JTable table, int column)
    {
        Scrolling.scrollHorizontally(table, getColumnBounds(table,
column));
    }

    public static void makeRowsVisible(JTable table, int first, int last)
    {
        Scrolling.scrollVertically(table, getRowBounds(table, first,
last));
    }

    public static void makeRowsVisible(JTable table, int first, int
last, int bias)
    {
        Scrolling.scrollVertically(table, getRowBounds(table, first,
last), bias);
    }

    public static void makeColumnsVisible(JTable table, int first, int
last)
    {
        Scrolling.scrollHorizontally(table, getColumnBounds(table,
first, last));
    }

    public static void makeColumnsVisible(JTable table, int first, int
last, int bias)
    {
        Scrolling.scrollHorizontally(table, getColumnBounds(table,
first, last), bias);
    }

    public static void makeCellsVisible(JTable table, int firstRow, int
lastRow, int firstColumn, int lastColumn)
    {
        table.scrollRectToVisible(getCellBounds(table, firstRow,
lastRow, firstColumn, lastColumn));
    }

    public static void makeCellsVisible(JTable table, int firstRow, int
lastRow, int firstColumn, int lastColumn, int bias)
    {
        Scrolling.scroll(table, getCellBounds(table, firstRow, lastRow,
firstColumn, lastColumn), bias);
    }

    public static void makeCellsVisible(JTable table, int firstRow, int
lastRow, int firstColumn, int lastColumn, int rowBias, int columnBias)
    {
        Scrolling.scroll(table, getCellBounds(table, firstRow, lastRow,
firstColumn, lastColumn), rowBias, columnBias);
    }

    public static void centerRow(JTable table, int row)
    {
        Scrolling.centerVertically(table, getRowBounds(table, row), false);
    }

    public static void centerColumn(JTable table, int column)
    {
        Scrolling.centerHorizontally(table, getColumnBounds(table,
column), false);
    }

    public static void centerRows(JTable table, int first, int last)
    {
        Scrolling.centerVertically(table, getRowBounds(table, first,
last), false);
    }

    public static void centerColumns(JTable table, int first, int last)
    {
        Scrolling.centerHorizontally(table, getColumnBounds(table,
first, last), false);
    }

    public static void centerCell(JTable table, int row, int column)
    {
        Scrolling.center(table, getCellBounds(table, row, column), false);
    }

    public static void centerCells(JTable table, int firstRow, int
lastRow, int firstColumn, int lastColumn)
    {
        Scrolling.center(table, getCellBounds(table, firstRow, lastRow,
firstColumn, lastColumn), false);
    }

    public static boolean isRowVisible(JTable table, int row)
    {
        return Scrolling.isVerticallyVisible(table, getRowBounds(table,
row));
    }

    public static boolean isColumnVisible(JTable table, int column)
    {
        return Scrolling.isHorizontallyVisible(table,
getColumnBounds(table, column));
    }

    public static boolean isCellVisible(JTable table, int row, int column)
    {
        return Scrolling.isVisible(table, getCellBounds(table, row,
column));
    }

    public static boolean areColumnsVisible(JTable table, int first,
int last)
    {
        return Scrolling.isHorizontallyVisible(table,
getColumnBounds(table, first, last));
    }

    public static boolean areRowsVisible(JTable table, int first, int last)
    {
        return Scrolling.isVerticallyVisible(table, getRowBounds(table,
first, last));
    }

    public static boolean areCellsVisible(JTable table, int firstRow,
int lastRow, int firstColumn, int lastColumn)
    {
        checkCells(table, firstRow, lastRow, firstColumn, lastColumn);

        return Scrolling.isVisible(table, getCellBounds(table,
firstRow, lastRow, firstColumn, lastColumn));
    }

    private static void checkRow(JTable table, int row)
    {
        if (row < 0)
            throw new IndexOutOfBoundsException(row+" < 0");
        if (row >= table.getRowCount())
            throw new IndexOutOfBoundsException(row+" >=
"+table.getRowCount());
    }

    private static void checkColumn(JTable table, int column)
    {
        if (column < 0)
            throw new IndexOutOfBoundsException(column+" < 0");
        if (column >= table.getColumnCount())
            throw new IndexOutOfBoundsException(column+" >=
"+table.getColumnCount());
    }

    private static void checkCell(JTable table, int row, int column)
    {
        checkRow(table, row);
        checkColumn(table, column);
    }

    private static void checkRows(JTable table, int first, int last)
    {
        if (first < 0)
            throw new IndexOutOfBoundsException(first+" < 0");
        if (first > last)
            throw new IndexOutOfBoundsException(first+" > "+last);
        if (last >= table.getRowCount())
            throw new IndexOutOfBoundsException(last+" >=
"+table.getRowCount());
    }

    private static void checkColumns(JTable table, int first, int last)
    {
        if (first < 0)
            throw new IndexOutOfBoundsException(first+" < 0");
        if (first > last)
            throw new IndexOutOfBoundsException(first+" > "+last);
        if (last >= table.getColumnCount())
            throw new IndexOutOfBoundsException(last+" >=
"+table.getColumnCount());
    }

    private static void checkCells(JTable table, int firstRow, int
lastRow, int firstColumn, int lastColumn)
    {
        checkRows(table, firstRow, lastRow);
        checkColumns(table, firstColumn, lastColumn);
    }
}
Signature


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

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

Luc Van Bogaert - 17 Mar 2006 07:28 GMT
> I have not tried this but here goes..
>
> Making cells/rows/columns visible

Thanks for your help, but I'm afraid you misunderstood the question :
the objective is to completely hide or show some specific table
columns.

Signature

Luc Van Bogaert

Via ProNews/2 & eComStation
http://www.os2world.com/os2ecs

IchBin - 17 Mar 2006 15:09 GMT
>> I have not tried this but here goes..
>>
[quoted text clipped - 3 lines]
> the objective is to completely hide or show some specific table
> columns.

The cells/rows/columns visible means cells and or rows and or columns
visible.

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

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
steve - 24 Mar 2006 22:46 GMT
>> I have not tried this but here goes..
>>
[quoted text clipped - 3 lines]
> the objective is to completely hide or show some specific table
> columns.

it's not difficult, only 5  to 10 lines of code,  the only thing to remember
is do not do it by setting the  row/hight to zero.

Steve

Signature

NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth

Nigel Wade - 17 Mar 2006 12:00 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>
> Any help is much appreciated.

I think it's almost certain that you will need to create your own custom
TableModel possibly extending either AbstractTableModel or DefaultTableModel to
do this.

As a starting point have a look at the TableSorter class
http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/TableSo
rter.java

which, whilst it doesn't do what you want, might provide you with some insight
into how to go about creating a TableModel to mess with the view of the table
data.

Does Google give any hits if you ask it? There may be a solution on the Web
already.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Andrew McDonagh - 24 Mar 2006 23:31 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>
> Any help is much appreciated.

Its nothing to do with the TableModel - this holds the table's Data.

You need to look at the TableColumnModel - this is used to present the data.

specifically, TableColumnModel's removeColumn() & addColumn()


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.