Solved

JTable not occupying the entire Panel.

Posted on 1999-01-08
7
514 Views
Last Modified: 2013-11-23

HI all,

    I have a JTable with out scrollbars in a JPanel. I tried different layout managers, but the table is not occupying the entire Panel and is not resizing along with the Panel.

Here is the code i used. I am trying to place a couple of JTables with borders in separate Panels and am adding those Panels to another Panel.

String[] colFields = {"LABEL","VALUE"};
//Top Level Panel
JPanel resPanel = new JPanel(new GridLayout(1,2));

//One of the two panels.
JPanel selPanel = new JPanel(new BorderLayout());
selModel    = new MyTableModel(colFields, 0);
selTable    = new JTable(selModel);
selTable.setColumnSelectionAllowed(false);
selTable.setRowSelectionAllowed(false);       selTable.getTableHeader().setReorderingAllowed(false);
selPanel.add(selTable.getTableHeader(),"North");
selPanel.add(selTable,"Center");
selPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"SELECTED"));
resPanel.add(selPanel);

//Second Panel
JPanel conPanel = new JPanel(new BorderLayout());
conModel    = new MyTableModel(colFields, 0);
conTable    = new JTable(conModel);
conTable.setColumnSelectionAllowed(false);
conTable.setRowSelectionAllowed(false);       conTable.getTableHeader().setReorderingAllowed(false);
conPanel.add(conTable.getTableHeader(),"North");
conPanel.add(conTable,"Center");
conPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"CONNECTED"));
resPanel.add(conPanel);

I donot want scrollbars for any of the three Panels i used. I am going to palce resPanel in another Panel with other components, which will have a scrollbar. I am using Swing 1.0.3, JDK 1.1.7A, NT.

Any suggestion or pointers will be highly appreciated.

Thanks in advance
Satish.
0
Comment
Question by:satishb
  • 4
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 20 total points
ID: 1230717
/*
    the magic is:
    1. use JTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    2. put JTable into JViewport
   
    just compile this example and start it - the left table is a Good one, the righ - a Bad one
    (tested on Win95 jdk1.1.6 Swing 1.0.2, but it should work an all other systems.)

   best regards
     heyhey
*/
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
import com.sun.java.swing.tree.*;
import com.sun.java.swing.border.*;


public class test {

public static void main(String args[])
{
    String[] colFields = {"LABEL","VALUE"};
    //Top Level Panel
    TableModel selModel, conModel;
    JTable selTable, conTable;
    JViewport selView;

    JPanel resPanel = new JPanel(new GridLayout(1,2));

    //One of the two panels.
    JPanel selPanel = new JPanel(new BorderLayout());
    selModel    = new DefaultTableModel(colFields, 10);
    selTable    = new JTable(selModel);
    selTable.setColumnSelectionAllowed(false);
    selTable.setRowSelectionAllowed(false);
    selTable.getTableHeader().setReorderingAllowed(false);
    selView = new JViewport();
    selView.setView(selTable);
    selPanel.add(selTable.getTableHeader(),"North");
//    selPanel.add(selTable,"Center");
    selPanel.add(selView,"Center");
    selPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"with JViewport"));
    selTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    resPanel.add(selPanel);

    //Second Panel
    JPanel conPanel = new JPanel(new BorderLayout());
    conModel    = new DefaultTableModel(colFields, 10);
    conTable    = new JTable(conModel);
    conTable.setColumnSelectionAllowed(false);
    conTable.setRowSelectionAllowed(false);       conTable.getTableHeader().setReorderingAllowed(false);
    conPanel.add(conTable.getTableHeader(),"North");
    conPanel.add(conTable,"Center");
    conPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"without JViewport"));
    conTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    resPanel.add(conPanel);

    JFrame f = new JFrame("Test Frame");
    f.addWindowListener(
      new WindowAdapter () {
        public void windowClosing(WindowEvent e)
        {System.exit(0);}
      }
    );
    f.getContentPane().add(resPanel);
    f.setSize(300,300);
    f.show();
    System.out.println("new version");

}

}

0
 
LVL 1

Author Comment

by:satishb
ID: 1230718
Hi Mr. Hey Hey,

        I tried your suggestion. Its filling the Panel in width-wise but its not doing it  in height-wise. Even if i have only 5 rows in that table the panel is occupying twice that size and the table is not occupying that much space. This in turn is bringing scrollbars to the othey panel in which i am placing this panel and other components. I don't like to have scrollbars on my externalPanel. I am adding this resPanel and another Panel to my external Panel using BorderLayout.

     So i would like the resPanel to occupy space depending on the size of my external Panel. And i would like to have the table occupy the entire selPanel and conPanel.

Thanks for your suggestion.
Satish.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230719
evety JTable has FIXED row height.
as far as I understand you want to change the height of a single row depending on the Container height ?

probably this can be implemented ... but sometimes it will look very ugly (image a JTable row with height 150 pixels).

maybe it's better if you adjust your external container (JPanel) so that it makes its size equal to the size of the table.

can you post some sample code that reproduces your problem (some small example - just like mine) so that I can take a look at it and give you the best solution ?

best regards
  heyhey
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:satishb
ID: 1230720
Hi,

    I am giving the code i am using. Instead of the DefaultTableModel i have a MyTableModel class extended from AbstractTableModel. This example works fine as far as the table width is concerned. But some how its not working in my actual application. It is increasing the columnwidth well beyond what is required. And it is making the table height almost equal to table width. I even tried to set the minwidth of the column. May be i should try setting maxwidth, but it doen't look nice when i resize the external window.


    import java.awt.*;
    import java.awt.event.*;
    import com.sun.java.swing.*;
    import com.sun.java.swing.table.*;
    import com.sun.java.swing.tree.*;
    import com.sun.java.swing.border.*;


    public class test {

    public static void main(String args[])
    {
        String[] colFields = {"LABEL","VALUE"};
        //Top Level Panel
        TableModel selModel, conModel;
        JTable selTable, conTable;
        JViewport selView;

        JPanel monitorPanel = new JPanel(new BorderLayout());
        JPanel labelPanel = new JPanel(new GridLayout(3,3));

        JLabel lbl_dds1 = new JLabel("SYS : ");
        JLabel lbl_dds2 = new JLabel("TAP : ");
        JLabel lbl_dds3 = new JLabel("STATUS : ");
        JLabel lbl_dds4 = new JLabel("SEL DG : ");
        JLabel lbl_dds5 = new JLabel("CH : ");
        JLabel lbl_dds6 = new JLabel("TYPE  : ");
        JLabel lbl_dds7 = new JLabel("CON DG : ");
        JLabel lbl_dds8 = new JLabel("CH : ");
        JLabel lbl_dds9 = new JLabel("TIME : ");

        labelPanel.add(lbl_dds1);
        labelPanel.add(lbl_dds2);
        labelPanel.add(lbl_dds3);
        labelPanel.add(lbl_dds4);
        labelPanel.add(lbl_dds5);
        labelPanel.add(lbl_dds6);
        labelPanel.add(lbl_dds7);
        labelPanel.add(lbl_dds8);
        labelPanel.add(lbl_dds9);

        JPanel resPanel = new JPanel(new GridLayout(1,2));

        //One of the two panels.
        JPanel selPanel = new JPanel(new BorderLayout());
        //selModel    = new MyTableModel(colFields, 0);
        selModel    = new DefaultTableModel(colFields, 12);
        selTable    = new JTable(selModel);
        selTable.setColumnSelectionAllowed(false);
        selTable.setRowSelectionAllowed(false);
        selTable.getTableHeader().setReorderingAllowed(false);
        selView = new JViewport();
        selView.setView(selTable);
        selPanel.add(selTable.getTableHeader(),"North");

        selModel.setValueAt("DSOB Frame Errs",  0,0);
        selModel.setValueAt("MUX Out Sync Code",1,0);
        selModel.setValueAt("Unasgn MUX Code",  2,0);
        selModel.setValueAt("OCU Loop Code",    3,0);
        selModel.setValueAt("CSU Loop Code",    4,0);
        selModel.setValueAt("DSU Loop Code",    5,0);
        selModel.setValueAt("Undef Code",       6,0);
        selModel.setValueAt("Idle Code",        7,0);
        selModel.setValueAt("ASC Code",         8,0);
        selModel.setValueAt("Data Count",       9,0);
        selModel.setValueAt("Last Ctrl Code",   10,0);
        selModel.setValueAt("Last Data Sample", 11,0);

        selPanel.add(selView,"Center");
        selPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"with JViewport"));
        selTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        resPanel.add(selPanel);

        //Second Panel
        JPanel conPanel = new JPanel(new BorderLayout());
       //conModel    = new MyTableModel(colFields, 0);
        conModel    = new DefaultTableModel(colFields, 12);
        conTable    = new JTable(conModel);
        conTable.setColumnSelectionAllowed(false);
        conTable.setRowSelectionAllowed(false);
        conTable.getTableHeader().setReorderingAllowed(false);

        //int rc = conModel.createDataObject(12);
        conModel.setValueAt("DSOB Frame Errs",  0,0);
        conModel.setValueAt("MUX Out Sync Code",1,0);
        conModel.setValueAt("Unasgn MUX Code",  2,0);
        conModel.setValueAt("OCU Loop Code",    3,0);
        conModel.setValueAt("CSU Loop Code",    4,0);
        conModel.setValueAt("DSU Loop Code",    5,0);
        conModel.setValueAt("Undef Code",       6,0);
        conModel.setValueAt("Idle Code",        7,0);
        conModel.setValueAt("ASC Code",         8,0);
        conModel.setValueAt("Data Count",       9,0);
        conModel.setValueAt("Last Ctrl Code",   10,0);
        conModel.setValueAt("Last Data Sample", 11,0);

        conPanel.add(conTable.getTableHeader(),"North");
        conPanel.add(conTable,"Center");
        conPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"without JViewport"));
        conTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        resPanel.add(conPanel);

        monitorPanel.add(labelPanel,BorderLayout.NORTH);
        monitorPanel.add(resPanel,BorderLayout.CENTER);


        JFrame f = new JFrame("Test Frame");
        f.addWindowListener(
          new WindowAdapter () {
            public void windowClosing(WindowEvent e)
            {System.exit(0);}
          }
        );
        f.getContentPane().add(monitorPanel);
        f.setSize(300,300);
        f.show();
        System.out.println("new version");

    }

    }

Thanks in advance.
Satish.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230721
hi again

I've tested your example code.
It works fine for the column widths of the 'selTable'
It does not work for the row height of the same table.
So what behaviour  exactly do you need ? Do you need JTable to resize its 'row height' or you want the selPanel to have as 'preferred size' the size of the selTable ?
0
 
LVL 1

Author Comment

by:satishb
ID: 1230722

HI,

         The example i provided i working fine. But in my actual application its not working fine. First its taking too much column width then whats required. Twice or thrice more than  the longest label. Second The table height is too much. An empty space of 2 or 3 rows is fine but i am getting an empty space of more than 10 rows. I assume its bacause the table is trying to seta height equal to the width. This scenario is giving scrollbars to monitorPanel even though it has considerable viewing area in it.

I do not want to fix the Panel size to be the size of the table. In that case if my external panel(monitorPanel)  is resized i doubt whether both the internal panel(selPanel) and the table will increase or not. If i allow scrollbars to the table and disable them then its looking fine except that i want to remove those scrollbars.

So basically i want a look as it is in the example i sent you which is not coming when i add this code in my application. Any ideas why it is happening like that.

Thanks in advance.
Satish.
0
 
LVL 1

Author Comment

by:satishb
ID: 1230723

HI Mr. Hey Hey,

    I fixed my problem by setting the preferred sizes  of the internal panels selPanel and conPanel. Now everything is working fine. I got that idea from you suggestion only. I am accepting your answer and giving you the points.

Thanks for your help
Satish.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now