Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Auto size JFrame

Posted on 2011-10-13
7
Medium Priority
?
578 Views
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.

Thanks!
0
Comment
Question by:dshrenik
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36965514
In what way 'auto'?
0
 

Author Comment

by:dshrenik
ID: 36965520
Depending on the size of whats inside the frame - the table and the buttons.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1200 total points
ID: 36965527
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.setRowHeight(80);
    table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add(BorderLayout.CENTER, pane);
   // frame.setSize(500, 400);
      frame.pack();
    frame.setVisible(true);
  }
}

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);
    lbl.setIcon(icon);
    return lbl;
  }
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 36965701
I suspect you could be running foul of not using a layout manager. Can you post runnable code that demonstrates the current situation?
0
 

Author Comment

by:dshrenik
ID: 36965736
@CEHJ:
Unfortunately, I am not allowed to post code online. But, thanks for the suggestions. I will try to learn more about layouts.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 400 total points
ID: 36966826
>> 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.

But

yourFrame.getContentPane().setLayout( <the layout manager of your choice> );   // cf. http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html

and

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

is the bottom line.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 37055275
Thanx for axxepting
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

572 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