Auto size JFrame

Posted on 2011-10-13
Last Modified: 2012-06-27
I have s JTable and a few buttons in a JFrame. Please let me know how I can set its dimensions to auto.

Question by:dshrenik
    LVL 86

    Expert Comment

    In what way 'auto'?

    Author Comment

    Depending on the size of whats inside the frame - the table and the buttons.
    LVL 47

    Accepted Solution

    frame.pack() works for me in this case:

    import java.awt.BorderLayout;
    import java.awt.Component;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    public class Main {
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());
        MyTableModel model = new MyTableModel();
        JTable table = new JTable(model);
        table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
        JScrollPane pane = new JScrollPane(table);
        frame.getContentPane().add(BorderLayout.CENTER, pane);
       // frame.setSize(500, 400);
    class MyTableModel extends AbstractTableModel {
      public Object getValueAt(int row, int column) {
        return "" + (row * column);
      public int getColumnCount() {
        return 4;
      public int getRowCount() {
        return 5;
    class ImageRenderer extends DefaultTableCellRenderer {
      JLabel lbl = new JLabel();
      ImageIcon icon = new ImageIcon("HearthFlag.gif");
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
          boolean hasFocus, int row, int column) {
        lbl.setText((String) value);
        return lbl;

    Open in new window

    LVL 86

    Assisted Solution

    I suspect you could be running foul of not using a layout manager. Can you post runnable code that demonstrates the current situation?

    Author Comment

    Unfortunately, I am not allowed to post code online. But, thanks for the suggestions. I will try to learn more about layouts.
    LVL 37

    Assisted Solution

    >> I am not allowed to post code online.
    I can't imagine you're not allowed to post a very small app illustrating your problem.


    yourFrame.getContentPane().setLayout( <the layout manager of your choice> );   // cf.


    yourFrame.getContentPane().add( <your components, like your JTable> );

    is the bottom line.
    LVL 37

    Expert Comment

    Thanx for axxepting

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Title # Comments Views Activity
    Using object mapper in spring web application 8 38
    userCompare  challenge 3 51
    IT Company 5 52
    return in catch statement 1 32
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now