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.

How to force JScrollpane to resize?

Thread view: 
Rick - 11 Feb 2005 16:11 GMT
I have a JScrollpane that contains a JTable. The code below
demonstrates my resizing problem: when the button is pressed the
TableModel is changed and the scrollpane is repainted but its size does
not change. After pressing the button, if I resize the owner JFrame
then the scrollpane is redrawn the correct size.

I've overriden the getPreferredSize method of the JScrollpane to track
the size of the JTable. How can I force the scrollpane to be resized
when I change the TableModel?

Thanks for any advice.

Rick

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class FrmTasksList extends javax.swing.JFrame implements
ActionListener {

    private JButton                    btnManageTask = null;
    private JPanel                     myContentPane = null;
    private JScrollPane                scrTasks = null;
    private JTable                     tblTasks = null;
    private DefaultTableModel        taskTableModel;
    public static int                 DEFAULT_COLUMN_WIDTH  = 20;

public FrmTasksList(String title) {
    super(title);

    setContentPane(getmyContentPane());
}

public void actionPerformed(ActionEvent ae){

    if (ae.getSource() == getBtnManageTask()) {
        tblTasks.setModel(getTaskTableModel(7,7));
        setupColumns(gettblTasks())    ;
        scrTasks.revalidate();
    }
}

/**
* Return the myContentPane property value.
* @return javax.swing.JPanel
*/

private javax.swing.JPanel getmyContentPane() {
    if (myContentPane == null) {

        myContentPane = new javax.swing.JPanel();
        myContentPane.setName("myContentPane");
        myContentPane.setLayout(new java.awt.GridBagLayout());

        java.awt.GridBagConstraints gbcScrTasks = new
java.awt.GridBagConstraints();
        gbcScrTasks.gridx = 0; gbcScrTasks.gridy = 1;
        gbcScrTasks.fill = java.awt.GridBagConstraints.NONE;
        gbcScrTasks.anchor = GridBagConstraints.WEST;
        gbcScrTasks.weightx = 1.0;
        gbcScrTasks.weighty = 0.0;
        gbcScrTasks.insets = new java.awt.Insets(0, 10, 10, 10);
        myContentPane.add(getScrTasks(), gbcScrTasks);

        java.awt.GridBagConstraints gbcButton = new
java.awt.GridBagConstraints();
        gbcButton.gridx = 0; gbcButton.gridy = 5;
        gbcButton.fill = java.awt.GridBagConstraints.NONE;
        gbcButton.insets = new java.awt.Insets(0, 10, 10, 10);
        myContentPane.add(getBtnManageTask(), gbcButton);

    }
    return myContentPane;
}
/**
* Return the scrTasks property value.
* @return javax.swing.JScrollPane
*/

private javax.swing.JScrollPane getScrTasks() {
    if (scrTasks == null) {
        scrTasks = new javax.swing.JScrollPane(){
            public Dimension getPreferredSize(){
                return gettblTasks().getPreferredSize() ;

            }
        };
        scrTasks.setViewportView(gettblTasks());
        scrTasks.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrTasks.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    }
    return scrTasks;
}
/**
* Return the tblTasks property value.
* @return javax.swing.JTable
*/

private JTable gettblTasks() {
    if (tblTasks == null) {

            tblTasks = new JTable(getTaskTableModel(4,4));
            tblTasks.getTableHeader().setEnabled(false);
            tblTasks.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            tblTasks.setRowHeight(20);
            tblTasks.setAutoCreateColumnsFromModel(true);
            setupColumns(tblTasks);

    }
    return tblTasks;
}
public void setupColumns(JTable table) {

    table.tableChanged(null);

    Enumeration enum =
table.getTableHeader().getColumnModel().getColumns();
    while (enum.hasMoreElements()) {
        TableColumn column = (TableColumn)enum.nextElement();
            //set column widths
        int width = DEFAULT_COLUMN_WIDTH;
        column.setMinWidth(width);
        column.setMaxWidth(width);
        column.setPreferredWidth(width);
        column.setResizable(false);

    }

}

/**
* main entrypoint - starts the part when it is run as an application
* @param args java.lang.String[]
*/
public static void main(java.lang.String[] args) {

        FrmTasksList aFrmTasksList;
        aFrmTasksList = new FrmTasksList("Test resizing JScrollpane");

        aFrmTasksList.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        aFrmTasksList.setSize(700, 700);

    //    aFrmTasksList.pack();
        aFrmTasksList.setVisible(true);

}

    public JButton getBtnManageTask() {
        if (btnManageTask == null) {
            btnManageTask = new JButton();
            btnManageTask.setText("Enlarge Table");
            btnManageTask.addActionListener(this);
        }

        return btnManageTask;
    }

    protected DefaultTableModel getTaskTableModel(int numberOfRows, int
numberOfColumns) {

        taskTableModel = new DefaultTableModel(numberOfRows,numberOfColumns);
   
        return taskTableModel;
       
    }
}
Roland - 11 Feb 2005 18:57 GMT
> I have a JScrollpane that contains a JTable. The code below
> demonstrates my resizing problem: when the button is pressed the
[quoted text clipped - 9 lines]
>
> Rick

[snips]

The following seems to do the trick:
   //scrTasks.revalidate();
   getmyContentPane().revalidate();

However, the "problem"'s origin is more the use of one component
(JScrollPane with JTable) on one cell of the GridBagLayout (or rather
its GBConstraints). Why don't you use
   gbcScrTasks.fill = java.awt.GridBagConstraints.BOTH;
   gbcScrTasks.weighty = 1.0;
This causes the scrollpane to stretch in both directions. No need to
revalidate either.
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Rick - 11 Feb 2005 19:28 GMT
Thank you for your suggestion.

>> Why don't you use     gbcScrTasks.fill =
java.awt.GridBagConstraints.BOTH;

The sample code can illustrate the difficulty I had when I used a fill
of BOTH.
Change the constraint from NONE to BOTH and you see that the scrollpane
can be
much larger than the table thus creating an awkward gap between the
left edge of the table and the right hand border of the scrollpane.

Is there a way to control the 'fill' so that the scrollpane will not
extend beyond the size
of the table?

thanks again for your tip.

Rick
Rick - 11 Feb 2005 19:29 GMT
Thank you for your suggestion.

>> Why don't you use     gbcScrTasks.fill =
java.awt.GridBagConstraints.BOTH;

The sample code can illustrate the difficulty I had when I used a fill
of BOTH.
Change the constraint from NONE to BOTH and you see that the scrollpane
can be
much larger than the table thus creating an awkward gap between the
left edge of the table and the right hand border of the scrollpane.

Is there a way to control the 'fill' so that the scrollpane will not
extend beyond the size
of the table?

thanks again for your tip.

Rick


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.