We help IT Professionals succeed at work.

resize JTable

allelopath
allelopath asked
on
In this example, when the user grabs the right edge of the frame and increases the size of the frame, the table size stays the same. How can it be coded so that the table size increases with the frame?

package tableStretch;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

/**
 * JPanel with JTable
 */
public class MyPanel extends JPanel {

	private JTable jTable;

	private DefaultTableModel defaultTableModel;

	private String[] columnNameArray = {"One", "Two", "Three"};

	private Object[][]  dataArray = { 
			{ "", "", ""},
			{ "", "", ""},
			{ "", "", ""},
			{ "", "", ""},
	};

	/**
	 * constructor
	 * 
	 */
	public MyPanel () {

		super();

		defaultTableModel = new DefaultTableModel(dataArray, columnNameArray);

		jTable = new JTable (defaultTableModel); 

		setLayout();

		setListeners();

	}

	/**
	 * set layout of components
	 */
	public void setLayout() {

		jTable.setFillsViewportHeight(true);

		JScrollPane jScrollPane = new JScrollPane(jTable);
		jScrollPane.getViewport().setPreferredSize(jTable.getPreferredSize());

		Border loweredbevel = BorderFactory.createLoweredBevelBorder();
		jScrollPane.setBorder(loweredbevel);

		jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

		this.add(jScrollPane, BorderLayout.CENTER);
	}

	/**
	 * set listeners
	 */
	public void setListeners() {

	}

	/**
	 * @return defaultTableModel
	 */
	public DefaultTableModel getDefaultTableModel() {
		return defaultTableModel;
	}


	/**
	 * 
	 * @param defaultTableModel
	 */
	public void setDefaultTableModel(DefaultTableModel defaultTableModel) {

		this.defaultTableModel = defaultTableModel;
		jTable.setModel(defaultTableModel);

	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e){
			e.printStackTrace();
		}

		final MyPanel jPanel = new MyPanel();
		final JFrame frame = new JFrame();
		frame.add(jPanel, BorderLayout.CENTER);
		frame.setTitle("My Panel");
		//		frame.setPreferredSize(new Dimension(500, 250));
		frame.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
			}
		});

		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setLocation(300, 200);
		frame.pack();
		frame.setVisible(true);

	}

}

Open in new window

Comment
Watch Question

HonorGodSoftware Engineer

Commented:
I think that you would have to have a listener that caught resize events for the frame, and that adjusted the JTable accordingly.
HonorGodSoftware Engineer

Commented:
Here's a start...
package tableStretch;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

/**
 * JPanel with JTable
 */
public class MyPanel extends JPanel implements ComponentListener {

  private JTable jTable;

  private DefaultTableModel defaultTableModel;

  private String[] columnNameArray = {"One", "Two", "Three"};

  private Object[][]  dataArray = { 
      { "", "", ""},
      { "", "", ""},
      { "", "", ""},
      { "", "", ""},
  };

  /**
   * constructor
   * 
   */
  public MyPanel () {

    super();

    defaultTableModel = new DefaultTableModel(dataArray, columnNameArray);

    jTable = new JTable ( defaultTableModel );

    setLayout();

    setListeners();

  }

  /**
   * set layout of components
   */
  public void setLayout() {

    jTable.setFillsViewportHeight(true);

    JScrollPane jScrollPane = new JScrollPane(jTable);
    jScrollPane.getViewport().setPreferredSize(jTable.getPreferredSize());

    Border loweredbevel = BorderFactory.createLoweredBevelBorder();
    jScrollPane.setBorder(loweredbevel);

    jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    this.add(jScrollPane, BorderLayout.CENTER);
  }

  /**
   * set listeners
   */
  public void setListeners() {
  }

  /**
   * implement ComponentListener methods
   */
   public void componentHidden(ComponentEvent e) {
     System.out.println( e.getComponent().getClass().getName() + " --- Hidden" );
   }
   
   public void componentMoved(ComponentEvent e) {
     System.out.println( e.getComponent().getClass().getName() + " --- Moved" );   
   }
   
   public void componentResized(ComponentEvent e) {
     Dimension size = e.getComponent().getSize();
     System.out.println( e.getComponent().getClass().getName() + " --- Resized to: width: " + size.width + " height: " + size.height );
   }
   
   public void componentShown(ComponentEvent e) {
     System.out.println( e.getComponent().getClass().getName() + " --- Shown" );   
   }
   
  /**
   * @return defaultTableModel
   */
  public DefaultTableModel getDefaultTableModel() {
    return defaultTableModel;
  }


  /**
   * 
   * @param defaultTableModel
   */
  public void setDefaultTableModel(DefaultTableModel defaultTableModel) {

    this.defaultTableModel = defaultTableModel;
    jTable.setModel(defaultTableModel);

  }

  /**
   * 
   * @param args
   */
  public static void main(String[] args) {

    try{
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e){
      e.printStackTrace();
    }

    final MyPanel jPanel = new MyPanel();
    final JFrame frame = new JFrame();
    frame.add(jPanel, BorderLayout.CENTER);
    jPanel.addComponentListener( jPanel );
    frame.setTitle("My Panel");
    //    frame.setPreferredSize(new Dimension(500, 250));
    frame.addWindowListener(new WindowAdapter(){
      @Override
      public void windowClosing(WindowEvent e) {
      }
    });

    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setLocation(300, 200);
    frame.pack();
    frame.setVisible(true);

  }

}

Open in new window

HonorGodSoftware Engineer

Commented:
btw, thanks for enumerating the source for all of the classes.  I too prefer to know from where things come.
Software Engineer
Commented:
further though made me remove line 136, i.e.,

    jPanel.addComponentListener( jPanel );

and add this line between 78 & 79:

    this.addComponentListener( this );

This is a really interesting question.  ;-)

I've made some progress, but I still have more work to do.

I'm to the point where I'm trying to figure out the best way to resize the ScrollPane container when the window is resized...

Author

Commented:
>>thanks for enumerating the source for all of the classes.  
I prefer as well. Eclipse does this for me.
HonorGodSoftware Engineer

Commented:
Thanks for the grade & points.

Good luck & have a great day.