Search code examples
javaswingjscrollpanejtabbedpaneborder-layout

How to stop JTextArea from being placed on top of JTabbedPane?


I am trying to design a layout with a JTabbedPane at the top of the frame and then a jLogArea below the tabbed pane.

I am using this code:

setLayout(new BorderLayout());

tabbedPane.setSize(WIDTH, HEIGHT);
add(tabbedPane, BorderLayout.PAGE_START);
tabbedPane.add("Tab 0", null);

scrollableTextArea = new JScrollPane(jTextArea);

jTextArea.setEditable(false);
jTextArea.setLineWrap(true);
scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollableTextArea, BorderLayout.PAGE_END);

However, the result of this is that the text area is placed behind the tabbed pane:

enter image description here

Does anyone know what I am doing wrong and how I can fix it? Thanks.

EDIT: Just to be clear, I am looking for the text area to be below the JTabbedPane, not in the tab iself.

Using BorderLayout.NORTH and BorderLayout.SOUTH does not help either. I added a label into the tab's contents just to see if that would make a difference but the text area still goes behind, this is how it looks:

enter image description here

Further code (the class extends JFrame):

public MainGUI() {
    init();
    pack();
    super.setTitle("test");
}

public void init() {
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));
    setMaximumSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));
    setMinimumSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));

    tabbedPane = new JTabbedPane();
    textArea = new JTextArea(WIDTH, TEXT_AREA_HEIGHT);
    scrollableTextArea = new JScrollPane(textArea);

    JLabel testLabel = new JLabel("Test!");
    tabbedPane.add("Tab 0", testLabel);

    tabbedPane.setBorder(null);
    tabbedPane.setSize(WIDTH, HEIGHT);
    add(tabbedPane, BorderLayout.NORTH);

    textArea.setEditable(false);
    textArea.setLineWrap(true);
    scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    add(scrollableTextArea, BorderLayout.SOUTH);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setResizable(false);
    setVisible(true);
}

Solution

  • UPDATE
    I think you are looking for something like this:
    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Container;
    
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.JTabbedPane;
    
    public class TabSample extends JFrame{
      public void createAndShowGUI() {
        JPanel panel = new JPanel();
        JTextArea ta = new JTextArea(100,50);
        JScrollPane jsp = new JScrollPane(ta);
        JTabbedPane tabbedPane = new JTabbedPane();
        panel.setLayout(new BorderLayout());
        tabbedPane.addTab("Tab one", panel);
        JSplitPane vPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tabbedPane, jsp);
        getContentPane().add(vPane);
        setSize(400,500);
        vPane.setDividerLocation(getHeight()/2);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
      }
    
      public static void main(String args[]) 
      {
          javax.swing.SwingUtilities.invokeLater(new Runnable()
          {
              @Override
              public void run()
              {
                  TabSample ts = new TabSample();
                  ts.createAndShowGUI();
              }
          });
      }
    }