Reputation: 3
I have JTable
then i add it to a JPanel
. Then I add this JPanel
to a JFrame
called frame. This frame
show that table properly but when i maximize the window then the JPanel
still remains small size. I want to show the JPanel
as all over the frame when i maximize the frame.
Here is my code:
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
@SuppressWarnings("serial")
public class TestClass extends JPanel
{
public TestClass()
{
Vector columnNames = new Vector();
Vector data = new Vector();
int columns =3;
// Get column names
columnNames.addElement("Id");
columnNames.addElement("Name");
columnNames.addElement("Age");
// Get row data
Vector row = new Vector(columns);
row.addElement("1");
row.addElement("Moshi");
row.addElement("22");
data.addElement( row );
// Create table with database data
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane( table );
add(scrollPane);
}
public static void main(String[] args)
{
TestClass testClass = new TestClass(); //**JPanel**
JFrame frame = new JFrame();
frame.setSize(500, 600);
frame.getContentPane().add(testClass); //**add jpanel to frame**
frame.setVisible(true);
}
}
Upvotes: 0
Views: 716
Reputation: 159754
Use a layout manager which sizes components according to available size rather than JPanel's
default FlowLayout
which only uses the components preferred size, e.g.
setLayout(new GridLayout());
Upvotes: 1