Reputation: 173
First, hier is the Code:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
@SuppressWarnings("serial")
public class Spielklasse extends JPanel{
private int iEntries = 1;
public Spielklasse(String strTitle){
setLayout(new GridBagLayout());
//dummy to fill vertical space
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1000;
c.fill = GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
c.gridwidth = 2;
add(new JLabel(" "),c);
}
public void addEntry(JComponent component){
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 1;
c.gridy = iEntries;
c.weightx = 1;
c.insets = new Insets(10, 20, 10, 20);
c.fill = GridBagConstraints.HORIZONTAL;
add(component, c);
iEntries++;
}
public static JPanel addExampleTablePanel() {
String[] columnNames = { "first", "second", "third", "fourth", "fifth", "sixth" };
String[][] strArr = new String[100][columnNames.length];
for (int i = 0; i < strArr.length; i++) {
for (int j = 0; j < strArr[0].length; j++) {
strArr[i][j] = i + " xxxxx " + j;
}
}
JTable table = new JTable(strArr, columnNames) {
@Override
public Dimension getPreferredScrollableViewportSize() {
int headerHeight = getTableHeader().getPreferredSize().height;
int height = headerHeight + (10 * getRowHeight());
int width = getPreferredSize().width;
return new Dimension(width, height);
}
};
JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumn column = null;
for (int i = 0; i < columnNames.length; i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(50);
column.setMaxWidth(50);
column.setMinWidth(50);
}
JToolBar toolBar = new JToolBar();
JButton btn = new JButton("test123");
toolBar.add(btn);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 1;
c.anchor = GridBagConstraints.LINE_START;
panel.add(toolBar, c);
c.gridy = 1;
panel.add(scrollPane, c);
return panel;
}
public static void main(String[] args) {
Spielklasse mainPanel = new Spielklasse("test");
mainPanel.addEntry(addExampleTablePanel());
mainPanel.addEntry(addExampleTablePanel());
mainPanel.addEntry(addExampleTablePanel());
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(mainPanel));
frame.setVisible(true);
}
}
The class "Spieklasse" should create a JPanel with multiple entries of different types like tables, textboxes etc.
In this example here, just 3 JTable-containing panels should be added.
This JTable is inside a JSrollPane and should have fixed column widths. This JScrollPane is inside a JPanel, wich contains a Toolbar above the Table to perform some actions etc. This JPanel is added to the main panel of the type "Spielklasse". The main panel is inside another JScrolLPane.
Here are the problem: - The Table-Panel should have a fixed height wich i can set like i want. In the code example, the size is already fixed, but i dont know why and its the wrong size too :-) - At the table a horizontal scrollbar should appear, when the size of the frame is smaller than the size of the table (all columns together). When the frame is bigger, everying should be stretched horizontal (works already)
I hope my explanation is good enough and someone can help me :-)
edit: updated with improvement of camickr, vertical size problem solved.
Upvotes: 0
Views: 3468
Reputation: 324118
the size is already fixed, but i dont know why and its the wrong size too :-)
The size of the scroll pane is determined from the getPreferredScrollableViewportSize()
method of JTable.
When the table is created the following is hardcoded in the JTable:
setPreferredScrollableViewportSize(new Dimension(450, 400));
So if you want to control the default width/height of the scroll pane you need to override the getPreferredScrollableViewportSize()
method to return a reasonable value. The height should include the column header as well as the number of rows in the table you wish to display.
Maybe something like:
@Override
public Dimension getPreferredScrollableViewportSize()
{
int headerHeight = table.getTableHeader().getPreferredSize().height;
int height = headerHeight + (10 * getRowHeight());
int width = getPreferredSize().width;
return new Dimension(width, height);
}
Edit:
To use the ScrollablePanel you can change your code to:
//public class Table8 extends JPanel{
public class Table8 extends ScrollablePanel{
private int iEntries = 1;
public Table8(String strTitle){
setLayout(new GridBagLayout());
setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );
Another problem is the GridBagLayout. If there is not enough space to display the component at its preferred size, then the component snaps to is minimum size. This causes problems with the scroll pane so you will also need to add:
scrollPane.setMinimumSize( scrollPane.getPreferredSize() );
Or it may be easier to use another layout manager. Maybe a vertical BoxLayout.
Upvotes: 1