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

Tip: Looking for answers? Try searching our database.

JTable in JScrollPane in JInternalFrame: sizing problem

Thread view: 
Piet - 05 Feb 2005 13:19 GMT
Hello,
I have a program that creates JInternalFrames which are located inside
one half of a JSplitPane. The JInternalFrames contain a JTable. I
would like that each JInternalFrame is sized in a way that it just
fits around the included JTable. Attempts with setPreferredSize() (as
shown below) did not give the desired result: the width is as
expected, but the height is always chosen too small. Am I missing a
mechanism to adjust component sizes? Below is a sample program.
TIA
Piet

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

public class ScrolledFrame extends JFrame {
   String appName;
   JDesktopPane deskPane = new JDesktopPane();
   ScrolledFrame(String appName){
       super("Scrolled Internal Frames");
       JSplitPane splitPane = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JPanel(), deskPane);
       splitPane.setDividerLocation(100);
       splitPane.setDividerSize(5);
       this.getContentPane().add(splitPane);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       addWindow(7);
   }
   public void addWindow(int number){
       GridWindow iGW = new GridWindow(number);
       iGW.setLocation(5,5);
       iGW.setSize(iGW.getPreferredSize());
       iGW.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
       this.deskPane.add(iGW);
       iGW.setVisible(true);
   }
   public static void main(String[] args){
       ScrolledFrame iSF = new ScrolledFrame("Scrolled Frame Test");
       iSF.pack();
       iSF.show();
   }
}
class GridWindow extends JInternalFrame{
   JTable IJTable;
   GridWindow(int number){
       super("Internal window "+number,true,true);
       DefaultTableColumnModel columnModel = new
DefaultTableColumnModel();
       for (int i = 1;i<number+1;i++){
           TableColumn tc = new TableColumn(i,i % 2 ==0 ? 150 : 75);
            tc.setHeaderValue("Column"+i+1);
           columnModel.addColumn(tc);
       }
       IJTable = new JTable(new MyTableModel(number),columnModel);
       IJTable.setRowHeight(18);
       IJTable.setAutoResizeMode(5);
       JScrollPane tablePane = new JScrollPane(IJTable);
       tablePane.getViewport().setPreferredSize(IJTable.getPreferredSize());
       getContentPane().add(tablePane);
       setPreferredSize(tablePane.getPreferredSize());
   }
}
class MyTableModel extends AbstractTableModel{
   int number;
   MyTableModel(int number){
       this.number = number;
   }
   public int getRowCount(){
       return this.number;
   }
   public int getColumnCount(){
       return this.number;
   }
   public String getColumnName(int colNum){
       return (String)("Column"+colNum);
   }
   public Object getValueAt(int rowNum,int colNum){
       return "Value-"+rowNum+"-"+colNum;
   }
   public void setValueAt(Object value,int rowNum,int colNum){
   }
}
Frank Liu - 05 Feb 2005 16:21 GMT
Not sure if this is the case, but JTable only show the actual table cell
data no header at all. So you gotta take the JTableHeader into account.
Which you can get by JScrollPane's getHeaderView or some function like that.

Frank

> Hello,
> I have a program that creates JInternalFrames which are located inside
[quoted text clipped - 81 lines]
>    }
> }


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.