I have a super basic JTable
, which I'm adding to a JFrame
, but for some reason frame.pack()
doesn't work. I'm setting the row height to the column width so that the cells of the table are squares, but this doesn't seem to work when the window is resized. So, how can I make it so that the cells of the table are always squares, even when resized, and the frame is properly packed? Here's the code for the window:
package me.an.ar.window;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class DayPlanner
{
private JFrame frame;
private void createAndShowGUI()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = frame.getContentPane();
Object[][] data = new Object[5][7];
String[] columnNames = { "Day1", "Day2", "Day3", "Day4", "Day5", "Day6", "Day7" };
JTable table = new JTable(data, columnNames);
for (int col = 0; col < table.getColumnCount(); col++)
{
for (int row = 0; row < table.getRowCount(); row++)
{
int colWidth = table.getColumnModel().getColumn(col).getWidth();
table.setRowHeight(row, colWidth);
}
}
container.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void show()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
public DayPlanner()
{
show();
}
public static void main(String[] args)
{
new DayPlanner();
}
}