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

Tip: Looking for answers? Try searching our database.

JTable AUTO_RESIZE_OFF with columns filling space

Thread view: 
Java and Swing - 14 Jun 2006 13:10 GMT
Hi,
  I have a JTable which I put into a JScrollPane.  I want the auto
resize mode to be JTable.AUTO_RESIZE_OFF...so that the scroll bars will
appear upon resizing columns.  However, I want the columns to initially
fill the width of the table.

I've tried...
[code]
int tableWidth = table.getPreferredSize().width;
int colWidth = tableWidth / table.getColumnCount();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        for (int i = 0; i < colModel.getColumnCount(); i++) {
            colModel.getColumn(i).setPreferredWidth(colWidth);
            colModel.getColumn(i).setWidth(colWidth);
            colModel.getColumn(i).setMinWidth(colWidth);
        }
[/code]

....problem is that the columns dont get the right size b/c of
AUTO_RESIZE_OFF.  If i dont set that autoresize mode, the columns are
the right size, but when user adjusts the size of a column they don't
get the scroll bars.

any suggestions?
Java and Swing - 14 Jun 2006 13:22 GMT
Well this is a bit hackish feeling, but does the job.

table = createMyTable();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scroller = new JScrollPane(table);

        Runnable r =  new Runnable() {
            public void run() {
                while (!table.isShowing()) {
                    try {
                        Thread.sleep(100);
                    }
                    catch (Exception e) {}
                }
                Runnable runner = new Runnable() {
                    public void run() {
                        TableColumnModel colModel = table.getColumnModel();
                        int tWidth = table.getParent().getSize().width;
                        int colWidth = tWidth / table.getColumnCount();
                        for (int i = 0; i < colModel.getColumnCount(); i++) {
                            colModel.getColumn(i).setPreferredWidth(colWidth);
                        }
                    }
                };
                SwingUtilities.invokeLater(runner);
            }
        };
        new Thread(r).start();

:)


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.