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

Tip: Looking for answers? Try searching our database.

Resize jtable in jscrollpane

Thread view: 
Marie - 18 Apr 2005 19:15 GMT
Hi,

I have a frame containing a JTable and a panel with buttons. When the
window gets resized, I would like that the jtable takes the extra space and
that the button panel stays the same. How can I do that? The jtable panel
is already the "Center" and both panels stay the same...

Thanks,
Marie

Here's the code:

package test;

import javax.swing.*;
import java.awt.AWTEvent;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Testframe extends JFrame {
   private JPanel mainPanel = new JPanel();
   private JPanel buttonPanel = new JPanel();
   private JPanel tablePanel = new JPanel();

   //Button panel
   private JButton applyTableBt = new JButton("Generate Table");
   private JButton addBt = new JButton("Add");
   private JButton editBt = new JButton("Edit");
   private JButton deleteBt = new JButton("Delete");
   private JButton matchBt = new JButton("Find");
   private JButton saveBt = new JButton("Save to file");
   private JButton loadBt = new JButton("Load from file");

   /**
    * Constructor.
    * @param a_parent Frame
    */
   protected Testframe() {
       enableEvents(AWTEvent.WINDOW_EVENT_MASK);
       try {
           jbInit();
           this.setResizable(true);
           this.setVisible(true);
           pack();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   /**
    * Initialization of the dialog.
    * @throws Exception
    */
   private void jbInit() throws Exception {
       this.setTitle("Pattern management - CAT");
       Object[][] data = { {"11", "12", "13", "14", "15", "16", "17", "18"}
,
                         {"21", "22", "23", "24", "25", "26", "27", "28"}
       };
       Object[] names = {"col1", "col2", "col3", "col4", "col5", "col6",
                        "col7", "col8"};
       JTable table = new JTable(data, names);
       table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       //table.setColu

       TableColumnModel colModel = table.getColumnModel();
       for (int i = 0; i < table.getColumnCount(); i++) {
           TableColumn column = colModel.getColumn(i);
           column.setPreferredWidth(511);
       }
       JScrollPane scrollPane = new JScrollPane(table);
       tablePanel.add(scrollPane);

       //Layout = vertical box with vertical gap
       buttonPanel.setLayout(new GridLayout(7, 1, 0, 5));
       buttonPanel.add(applyTableBt);
       buttonPanel.add(addBt);
       buttonPanel.add(editBt);
       buttonPanel.add(deleteBt);
       buttonPanel.add(matchBt);
       buttonPanel.add(saveBt);
       buttonPanel.add(loadBt);

       //mainPanel.setLayout(new BorderLayout());
       mainPanel.add(tablePanel, BorderLayout.CENTER);
       mainPanel.add(buttonPanel, BorderLayout.EAST);
       this.setContentPane(mainPanel);
   }

   /**
    * Overrides this class to call the good method when the dialog is
closed.
    * @param a_windowE WindowEvent
    */
   protected void processWindowEvent(WindowEvent a_windowE) {
       //If it is a request to close the window (X)
       if (a_windowE.getID() == WindowEvent.WINDOW_CLOSING) {
           cancel();
       }
       super.processWindowEvent(a_windowE);
   }

   /**
    * Closes the dialog.
    */
   private void cancel() {
       dispose();
   }

   private static void createAndDisplayFrame() {
       //Make sure we have nice window decorations.
       JFrame.setDefaultLookAndFeelDecorated(true);
       new Testframe();
   }

   public static void main(String[] args) {
       createAndDisplayFrame();
   }
}
Arnaud Berger - 19 Apr 2005 07:30 GMT
Hi,

Have a look at the SwingSet application in the demo directory of the JDK.
It shows many ways of having a JTable resized.

Regards,

Arnaud

> Hi,
>
[quoted text clipped - 121 lines]
> --
> Message posted via http://www.javakb.com
Marie - 19 Apr 2005 13:24 GMT
I found how to do it (using someonelse's help).  The center element needs
to be the table, but it needs to be the center element of the frame itself,
not of one of it's panels...  The way to do this is to replace the
following three lines:
mainPanel.add(tablePanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.EAST);
this.setContentPane(mainPanel);
by these:
mainPanel.add(buttonPanel, BorderLayout.EAST);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(mainPanel, BorderLayout.EAST);
Nigel Wade - 19 Apr 2005 15:15 GMT
> Hi,
>
[quoted text clipped - 118 lines]
>     }
> }

You need to set the layout of mainPanel and tablePanel to BorderLayout.

E.g. add this code at the start of jbInit():

 mainPanel.setLayout(new java.awt.BorderLayout());
 tablePanel.setLayout(new java.awt.BorderLayout());

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



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.