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 / March 2005

Tip: Looking for answers? Try searching our database.

JTable column width resizing woe

Thread view: 
hiwa - 23 Mar 2005 12:58 GMT
JTable column width resizing woe

If I put JTables in JScrollPanes, column width resizing does work
as expected. But if JTables are simply put in simpler containers
like JPanels, resizing behavior is quite abnormal. What could be
the cause of the problem and its workaround?

Here's a small test program:

import java.awt.*;
import java.beans.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

public class TableColumnWidthResize{
 JFrame frame;
 JTable t1, t2;
 TableColumnModel tcm1, tcm2;
 Enumeration columns1, columns2;

 String[] t1h = {"Road", "Length", "City"};
 String[][] t1c
  = {{"NW2", "800", "Dora"},
    {"Iban", "600", "Weiho"},
    {"ENE2", "500","Porso"}};
 String[] t2h = {"Flower", "Curse", "Priest"};
 String[][] t2c
   = {{"Fang", "7.00", "EggChar"},
     {"Shaoma", "5.00", "ScaCra"},
     {"Jiao", "4.00", "PorCabb"}};

 public TableColumnWidthResize(){
   frame = new JFrame();

   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Container con = frame.getContentPane();
   con.setLayout(new GridLayout(2, 1));

   t1 = new JTable(t1c, t1h); //target table
   t2 = new JTable(t2c, t2h); //source table
   t1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   t2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   /* you can compare two */
   useScrollPane(t1, t2, con);//works fine(container size weird)
//    nouseScrollPane(t1, t2, con); //resize behavior weird

   frame.pack();
   frame.setVisible(true);

   tcm1 = t1.getColumnModel();
   tcm2 = t2.getColumnModel();
   columns2 = tcm2.getColumns(); // all columns of t2

   TableColumnListener tcl = new TableColumnListener();
   while (columns2.hasMoreElements()){
     TableColumn tc = (TableColumn)(columns2.nextElement());
     tc.addPropertyChangeListener(tcl);
   }
 }

 class TableColumnListener implements PropertyChangeListener{
   // achieve 'twin-resizing' of two tables
   public void propertyChange(PropertyChangeEvent pce){
     TableColumn tc = (TableColumn)(pce.getSource());
     int i = tc.getModelIndex();
     tcm1.getColumn(i).setPreferredWidth(tc.getWidth());
   }
 }

 void useScrollPane(JTable ta, JTable tb, Container c){
   JScrollPane jsc1 = new JScrollPane(ta);
   JScrollPane jsc2 = new JScrollPane(tb);
   c.add(jsc1); c.add(jsc2);
 }

 void nouseScrollPane(JTable ta, JTable tb, Container c){
   JPanel p1 = new JPanel(); JPanel p2 = new JPanel();
   p1.setLayout(new BorderLayout());
   p2.setLayout(new BorderLayout());
   p1.add(ta.getTableHeader(), BorderLayout.PAGE_START);
   p2.add(tb.getTableHeader(), BorderLayout.PAGE_START);
   p1.add(ta, BorderLayout.CENTER);
   p2.add(tb, BorderLayout.CENTER);    
   c.add(p1); c.add(p2);
 }

 public static void main(String[] args){
   new TableColumnWidthResize();
 }
}
Christian Kaufhold - 28 Mar 2005 22:33 GMT
> If I put JTables in JScrollPanes, column width resizing does work
> as expected. But if JTables are simply put in simpler containers
> like JPanels, resizing behavior is quite abnormal. What could be
> the cause of the problem and its workaround?

* Using a Container whose LayoutManager does not give the JTable its preferred size.
* Use a Container or LayoutManager who does that, or change JTable's layout not
 to resize the columns at all in AUTO_RESIZE_OFF_MODE.

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.