enter code hereI am having the following problem.
I am adding a two JPanels to one panel and add this finally to my TabbedPane. However, I want to make it scrollable, therefore I just make the Panel scrollable which just adds my scroller in the middle of the bar.
Here is my example:
public class Test extends JFrame {
private static final long serialVersionUID = -4682396888922360841L;
private JMenuBar menuBar;
private JMenu mAbout;
private JMenu mMain;
private JTabbedPane tabbedPane;
public SettingsTab settings = new SettingsTab();
private void addMenuBar() {
menuBar = new JMenuBar();
mMain = new JMenu("Main");
mAbout = new JMenu("About");
menuBar.add(mMain);
menuBar.add(mAbout);
setJMenuBar(menuBar);
}
public void createTabBar() {
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JComponent panel2 = new JPanel();
tabbedPane.addTab("Test1", null, panel2,
"Displays the results");
tabbedPane.addTab("Settings", settings.createLayout());
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private void makeLayout() {
setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) {
Test gui = new Test();
gui.start();
}
public class SettingsTab extends JPanel {
public JPanel createLayout() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(table1());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(new JScrollBar());
return panel;
}
public JPanel table1() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = {{"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table2() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
}
}
Any recommendations what I am doing wrong?
I appreciate your reply!
UPDATE
I tried it with a JScrollPane but it still does not work:
public class Test extends JFrame {
private static final long serialVersionUID = -4682396888922360841L;
private JMenuBar menuBar;
private JMenu mAbout;
private JMenu mMain;
private JTabbedPane tabbedPane;
public SettingsTab settings = new SettingsTab();
private void addMenuBar() {
menuBar = new JMenuBar();
mMain = new JMenu("Main");
mAbout = new JMenu("About");
menuBar.add(mMain);
menuBar.add(mAbout);
setJMenuBar(menuBar);
}
public void createTabBar() {
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JComponent panel2 = new JPanel();
tabbedPane.addTab("Test1", null, panel2,
"Displays the results");
tabbedPane.addTab("Settings", settings.createLayout());
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private void makeLayout() {
setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) {
Test gui = new Test();
gui.start();
}
public class SettingsTab extends JScrollPane {
public JPanel createLayout() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(table1());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table3());
panel.add(Box.createRigidArea(new Dimension(0,10)));
add(new JScrollPane());
return panel;
}
public JPanel table1() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table2() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table3() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
}
}