Hello I am trying to make use of a scroll bar depending on the number of tabs that are comprised in a JTabbedPane.
I have set the following code:
JTabbedPane jtp = new JTabbedPane();
jtp.setTabPlacement(JTabbedPane.LEFT);
JScrollPane sp = new JScrollPane(jtp);
My program works, however the tabs extensions doesn't match my expectations.
I have:
Tab1
Tab2
Tab3
After adding enough tabs so that their summed size get larger than my panel, they grow into a new line and the horizontal scrollbar appears:
Tab31 Tab1
Tab32 Tab2
Tab33 Tab3
...
Tab30
<--------horizontal ScrollBar??--------->
What I want is that tabs keep growing from top to bottom and the Vertical scroll bar appears:
Tab1
Tab2
Tab3
...
Tab30
Tab31
How can I do that?
HEre you will find an adapted piece of my program:
package eventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TabbedPane extends JFrame {
private JTabbedPane jtp;
public static void main(String [] args) {
System.out.println("-----\nTest TabbedPane\n------\n");
TabbedPane tp = new TabbedPane();
tp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tp.setSize(300, 600);
tp.setVisible(true);
}
public TabbedPane() { //constructor for the tabbed frame you will be creating
//This will create the title you see in the upper left of the window
setTitle("Tabbed Pane");
//Here we are creating the object
jtp = new JTabbedPane();
jtp.setTabPlacement(JTabbedPane.LEFT);
JScrollPane sp = new JScrollPane(jtp);
//sp.add(jtp);
//This creates the template on the windowed application that we will be using
getContentPane().add(sp);
JPanel jp1 = new JPanel();//This will create the first tab
JPanel jp2 = new JPanel();//This will create the second tab
//This creates a non-editable label, sets what the label will read
//and adds the label to the first tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
//This adds the first and second tab to our tabbed pane object and names it
jtp.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("Tab: " + jtp.getSelectedIndex());
}
});
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
//This creates a new button called "Press" and adds it to the second tab
JButton test = new JButton("Press");
jp2.add(test);
//This is an Action Listener which reacts to clicking on
//the test button called "Press"
ButtonHandler phandler = new ButtonHandler();
test.addActionListener(phandler);
}
//This is the internal class that defines what the above Action Listener
//will do when the test button is pressed.
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null, "I've been pressed", "What happened?", JOptionPane. INFORMATION_MESSAGE);
for(int i=0; i<30; i++) {
jtp.addTab("Filter " + Integer.toString(i), new JPanel());
}
}
}
}