import java.awt.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
public class jTable extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel, topPanel2, topPanel3, topPanel4;
private JTable table;
private JScrollPane scrollPane;
private String columnNames[];
private String dataValues[][];
// Constructor of main frame
public jTable()
{
// Set the frame characteristics
setTitle( "Advanced Table Application" );
setSize( 300, 200 );
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel2 = new JPanel();
topPanel3 = new JPanel();
topPanel4 = new JPanel();
topPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
topPanel2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
topPanel3.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
topPanel4.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
topPanel.setLayout( new BorderLayout() );
topPanel.add(topPanel2, BorderLayout.NORTH);
topPanel.add(topPanel3, BorderLayout.CENTER);
topPanel.add(topPanel4, BorderLayout.SOUTH);
getContentPane().add( topPanel );
// Create columns
CreateColumns();
CreateData();
// Create a new table instance
table = new JTable( dataValues, columnNames );
// Configure some of JTable's paramters
//table.setShowHorizontalLines( false );
//table.setRowSelectionAllowed( true );
//table.setColumnSelectionAllowed( true );
// Change the selection colour
table.setSelectionForeground( Color.white );
table.setSelectionBackground( Color.red );
// Add the table to a scrolling pane
scrollPane = table.createScrollPaneForTable( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
}
public void CreateColumns()
{
// Create column string labels
columnNames = new String[8];
for( int iCtr = 0; iCtr < 8; iCtr++ )
columnNames[iCtr] = "Col:" + iCtr;
}
public void CreateData()
{
// Create data for each element
dataValues = new String[100][8];
for( int iY = 0; iY < 100; iY++ )
{
for( int iX = 0; iX < 8; iX++ )
{
dataValues[iY][iX] = "" + iX + "," + iY;
}
}
}
// Main entry point for this example
public static void main( String args[] )
{
// Create an instance of the test application
jTable mainFrame = new jTable();
mainFrame.setVisible( true );
}
}
```java
Jeżeli table jest w topPanel to po zmianie rozmiarów okna rozmiary tabeli też ulegają zmianie.
Jak uzyskać taki sam efekt zmiany rozmiarów table jeżeli tabela będzie w innym panelu?
Używałem różnych rozkładów ale brak efektu
Proszę o pomoc
Odpowiedz:
Dlatego że domyślnym layoutem dla JPanela jest FlowLayout a nie BorderLayout i BorderLayout ustawiony dla panelu z tabelą daje pożądany efekt
np: "topPanel3 = new JPanel(new BorderLayout());"
czasami odpowiedz jest tak blisko
0