Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Removal of unwanted space

Posted on 2002-04-23
24
276 Views
Last Modified: 2012-08-14
Can anyone cast light on how I get rid of an area below a JTable in a JScrollPane that the table does not fill? How can I make the JFrame have a tight fit around the JTable?
0
Comment
Question by:afterburner
  • 10
  • 9
  • 2
  • +2
24 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6963749
frame.pack() ?
0
 

Author Comment

by:afterburner
ID: 6963975
heyhey

done that
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6963990
post some code that reproduces the problem.
(small compilable example please.)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 6964429
How is your frame layed out? May be a layout manager issue.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6964697
I believe too that is a problem of layout. A hack solution is to set the background color of thet area (infact of all the table) to match the background of your JFrame if any +/- setting off the borders of JScrollPane.
0
 

Author Comment

by:afterburner
ID: 6965117
EEs:

This problem was mentioned in a previous thread, but I thought it would disappear once the scrollbars, headers, scrollpane issues had been sorted, but it didnt. So ozymandias posted some code which was almost identical to mine, even used the same Borderlayout manager. His code worked ok, wihtout any blank space, although it was not based on the same table model. The difference between his code and mine was that mine was partially generated inside Forte IDE, whilst his was apparently not done in an IDE. When I tried his, I planted it straight into user code, in other words my IDE was not handling the generation of the jtable and scrollpane objects. But I cant see why the IDE would affect an issue like this, especially as it used the same layout man. Nor can I understand why he was seeing space below the table in the code I sent him, if he was not running it in an IDE.

Here is my code, which I sent to ozymandias before, and which he got the same space problem under the table:

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.JScrollPane;



public class EZCServ extends javax.swing.JFrame {


    private  static Hashtable masterHash;
    public static Hashtable serverHash;
    public static int numThreads;



    /** Creates new form EZServ */
    public EZCServ() {

        initComponents ();

        pack ();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {
        txtField = new javax.swing.JTextField();
        onlinetable = new javax.swing.JTable();
        scroller = new JScrollPane(new JTable(new myTabMod()));
        scroller.setVerticalScrollBarPolicy(22);
        getContentPane().setLayout(new java.awt.BorderLayout(25, 5));
        setTitle("Test");
        setBackground(new java.awt.Color (204, 255, 204));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                setorgSize(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        }
        );

        txtField.setBorder(new javax.swing.border.MatteBorder(1, 1, 1,
1, java.awt.Color.cyan));
        txtField.setEditable(false);
        txtField.setForeground(java.awt.Color.lightGray);
        txtField.setText("Hello World");
        txtField.setBackground(new java.awt.Color (51, 0, 255));

        getContentPane().add(txtField, java.awt.BorderLayout.SOUTH);


        onlinetable.setPreferredSize(new java.awt.Dimension(400, 200));

onlinetable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN
);

        getContentPane().add(onlinetable, java.awt.BorderLayout.CENTER);


        scroller.setBorder(new javax.swing.border.BevelBorder(0,
java.awt.Color.gray, java.awt.Color.yellow));
        scroller.setViewportBorder(new
javax.swing.border.EtchedBorder(java.awt.Color.white,
java.awt.Color.yellow));

        getContentPane().add(scroller, java.awt.BorderLayout.CENTER);

    }

  private void setorgSize(java.awt.event.WindowEvent evt) {
      //tried to put all kinds of calls here to remedy things, with no
luck.
  }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit (0);
    }

    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
        new EZCServ ().show ();
    }


    // Variables declaration - do not modify
    private static javax.swing.JTextField txtField;
    private javax.swing.JTable onlinetable;
    private javax.swing.JScrollPane scroller;
    // End of variables declaration
//inner class


public class myTabMod extends
tractTableModel{
   
  private String[] columnNames = {"ForeName",
                        "Last Name",
                        "Sport",
                        "# of Years",
                        "Vegetarian"};  
                       
  public Object [][] data = {
    {"Mary", "Campione",
     "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
     "Chasing toddlers", new Integer(2), new Boolean(false)},
    {"Mark", "Andrews",
     "Speed reading", new Integer(20), new Boolean(true)},
     {"Alison", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
     {"Alison", "Huml",


     "Rowing", new Integer(3), new Boolean(true)},
     {"Alison", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
     {"Beta", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
     {"Ceta", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
     {"Theta", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
     {"Zeta", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
     {"Omega", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
    {"Angela", "Lih",
     "Teaching high school", new Integer(4), new Boolean(false)} };


public myTabMod(){

  }
  public int getRowCount(){return data.length;}
  public int getColumnCount(){return columnNames.length;}
  public Object getValueAt(int row, int column){return
data[row][column];}
  public Class getColumnClass(int column){return
data[0][column].getClass();}
  public String getColumnName(int column){return columnNames[column];}
       }



    //end inner class





}

P.S.
[[You may recognise that the init() code and comments are protected blocks
from Sun's Forte IDE]]


Regards
aftrbrnr.
0
 
LVL 92

Expert Comment

by:objects
ID: 6965131
Why does it add the table to the centre, and then add the scroll pane to the centre??


       getContentPane().add(onlinetable, java.awt.BorderLayout.CENTER);

...
       getContentPane().add(scroller, java.awt.BorderLayout.CENTER);
0
 
LVL 92

Expert Comment

by:objects
ID: 6965183
Try setting the preferred size of your scroll panes viewport:

scroll.setViewport().setPreferredSize(table.getPreferredSize());
0
 
LVL 92

Expert Comment

by:objects
ID: 6965212
Also make sure you get the preferred size from the correct table. The above code actually creates two tables.
0
 

Author Comment

by:afterburner
ID: 6965232
objects:

well, if I add the scollpane I think it is, to the west for example, it loses the scrollbars!

I will try your other suggestion in a jif.

aftrbrn.
0
 
LVL 92

Expert Comment

by:objects
ID: 6965247
I believe you can only add your scroll pane to the centre.
0
 
LVL 92

Expert Comment

by:objects
ID: 6965260
I should rephrase that, the scrollpane behaves best in the center of a borderlayout.
0
 

Author Comment

by:afterburner
ID: 6965279
objects:

tried this :
scroll.setViewport().setPreferredSize(table.getPreferredSize());

 and the compiler falls over with a

 "no method found matching setViewport()"

 error.
0
 
LVL 92

Expert Comment

by:objects
ID: 6965293
Sorry typo, should be:

scroll.getViewport().setPreferredSize(table.getPreferredSize());
0
 
LVL 92

Expert Comment

by:objects
ID: 6965302
where table is the table contained in the scroll pane.
So you could also use:

scroll.getViewport().setPreferredSize(scroll.getViewport().getView());
0
 

Author Comment

by:afterburner
ID: 6965511
objects:

OK I dindt see it either. But getViewport() has no effect (although it runs ok) and

scroll.getViewport().setPreferredSize(scroll.getViewport().getView());

produces a "no method found matching setPreferredSize()"

maybe setPreferredSize() aint got the right args??


0
 

Author Comment

by:afterburner
ID: 6965528
It seems to me its not the size of the table that needs adjusting, its the size of the initial JFrame.

But when I try to set that size, I get what I asked for, but the scrollbars are not showing, and nor is a small textfield that sits at the bottom of the screen. These only appear when I drag a resize, or even just put the cursor over the frame border and click. I have tried event code which calls alls sorts of things, but it dont work.

I just know I am being dense here.
0
 
LVL 5

Accepted Solution

by:
nebeker earned 160 total points
ID: 6965929
First of all, you need to quit creating two JTables.  Change your code from:

  onlinetable = new javax.swing.JTable();
  scroller = new JScrollPane(new JTable(new myTabMod()));

to:

   onlinetable = new javax.swing.JTable(new myTabMod());
   scroller = new JScrollPane(onlinetable);


Second, you shouldn't call "setPreferredSize" on a JTable because if your height isn't an exact multiple of the row height, your last row will be cut-off (only partially displayed).


Third, add this sometime before you call pack():

 scroller.setPreferredSize(
     onlinetable.getPreferredSize() );


I made these modifications to your program, and it comes up fine, without any blank space below the table, and the scroll bars and the text field are being displayed properly.

0
 
LVL 5

Expert Comment

by:nebeker
ID: 6965941
Also, I don't see much difference between adding the scroll pane to the CENTER vs. the WEST -- the scrollbars appear normally, and the table works as expected.

The only real difference is that there's a small blank area on the right side of the table, since a WEST oriented component will not expand to fill in the entire horizontal space, even if there are no CENTER or EAST components...

This must be a peculiarity of the BorderLayout manager.
0
 

Author Comment

by:afterburner
ID: 6966469
nebeker

That is totally correct, not to mention cool.

Once I had wondered why my table wouldn't display, I cut it, then pasted it back, and somehow the IDE liked that, and at the next Run decided to display it.

Many thanks indeed;

aftrb.
0
 

Author Comment

by:afterburner
ID: 6966477
I appreciate your help and contributions very much.

aftrbnr.
0
 
LVL 92

Expert Comment

by:objects
ID: 6967013
Whats the difference with that solution to what I already said???
0
 
LVL 92

Expert Comment

by:objects
ID: 6967023
In fact setting the preferred size of the scroller is not completely correct as it doesn't take some of the trimmings into account resulting in the window being a little small.
Setting the preferred size of the viewport as I suggested, makes the window exactly the right size.
0
 

Author Comment

by:afterburner
ID: 6967259
I'm not really qualified to answer your question (obviously), and the machinations which these super-IDEs get up to leaves me in the dark sometimes (again obvious from all this).

But I do know that although your code worked, it only did so after I moved either the scroller or the table to another compass point. If they both stayed CENTER then I could only see the scollpane. Why I thought of cutting the scrollpane then repasting it immediately into the window at centre again eludes me now, but it worked. The IDE is great but if it is going to play the role it does, then I would have thought it could be trusted to stack the components entrusted to it in the right order. But hey, whadda I know.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

792 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