Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,383 Java Programmers. There are 1,379 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

How can I set the width for a column in JTable?

 
Reply to this topicStart new topic

How can I set the width for a column in JTable?

fsloke
post 7 May, 2008 - 07:09 PM
Post #1


D.I.C Head

**
Joined: 19 Dec, 2007
Posts: 101



How can I set the width for a column in JTable?

My Tabel created is equal width. can i adjust it?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pbl
post 7 May, 2008 - 07:53 PM
Post #2


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

QUOTE(fsloke @ 7 May, 2008 - 07:09 PM) *

How can I set the width for a column in JTable?

My Table created is equal width. can i adjust it?

did it once... this is from code I wrote months ago... it worked ans still works

CODE

TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setMaxWidth(50);
tcm.getColumn(1).setMaxWidth(80);


I am sure there is setMinWidth() method and a setWidth() method

This post has been edited by pbl: 7 May, 2008 - 07:54 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

fsloke
post 8 May, 2008 - 09:43 AM
Post #3


D.I.C Head

**
Joined: 19 Dec, 2007
Posts: 101

Can I do the table as a Excel table?

When It double click the it will be find the highest character then fixed to the highest character width column....

Can?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 12:32 PM
Post #4


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

QUOTE(fsloke @ 8 May, 2008 - 09:43 AM) *

Can I do the table as a Excel table?

When It double click the it will be find the highest character then fixed to the highest character width column....

Can?


Sure but kind of useless... the user can ajust by himself the width of the columns by clicking in the header between two column

But if you still want the feature you'll have to add a ListSelectionListener to your table and to something like that in it

CODE


jtable.addListSelectionListener(new SelectionListener());

and as inner class

class SelectionListener implements ListSelectionListener {

   // use to detect double clik
   long lastCall;

   public void valueChanged(ListSelectionEvent e) {
    if(e.getValueIsAdjusting())        // mouse button not released yet
        return;
                // to determine double click
                long now = System.currentTimeMillis();
                if(now - lastCall < 100) {                            // double click must me in 1/10 second
                   lastCall = now;
                   return;
                }
                // ok it is a double click
                lastCall = 0;

    int col = getSelectedColumn();    
    if(col < 0)                // true during a clearSelection
        return;

// now your code to scan the row
// find the largest one
// set the size of the column

    clearSelection();
   }
        
}

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

iNaStY v3
post 8 May, 2008 - 02:05 PM
Post #5


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63

Hi fsloke -- I'm working on a GUI myself right now, and pbl will have to verify this as I'm not using a table, but with JFrame you can use

java

JFrame.setResizable(false);
boolean (resizable) = JFrame.isResizable();

/* so maybe in your code you could do something like */
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setMaxWidth(50);
tcm.getColumn(1).setMaxWidth(80);
tcm.setResizable(false);
boolean (resizable) = tcm.isResizable();


again, I'm not 100% sure that this is valid with a table, or if that snippet just fixes the size of the table, NOT the columns and widths (meaning you can't resize the table window, but you can still resize the columns and widths)
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 03:49AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month