Search code examples
javaswinglayoutjtextarea

using both JTabbedPane and JScrollPane


As you see the code, I would like to implement the 2nd tab with a text area, scrolling, and a button down there. This (JScrollPane scr = new JScrollPane(secondTab(panel2));) code was in the function, private static JTextArea secondTab(JPanel panel) before but I took it out of the function and put that back to MainFrame. Because the scroll and text area didn't show up. Now that I moved the code to mainframe the tex tarea and scroll are visible, but I'm struggling with making the button showing up in the 2nd tab. Do you guys have any idea?

public MainFrame(String username)
{

    JTabbedPane tab = new JTabbedPane();
    mainFrame.add(tab, BorderLayout.CENTER);

    JPanel panel1 = new JPanel();
    firstTab(panel1);
    tab.add("babababa", panel1);


    JPanel panel2 = new JPanel();
    JScrollPane scr = new JScrollPane(secondTab(panel2));
    JButton saveButton = new JButton("Save");
    panel2.add(saveButton);
    saveButton.setBounds(190, 280, 80, 40);

    tab.add("hahaha", panel2.add(scr));

    mainFrame.setBounds(200,200,500,400);
    mainFrame.setVisible(true);
}

private static JTextArea secondTab(JPanel panel) {
    panel.setLayout(null);

    final JTextArea nameTextArea=new JTextArea();
    nameTextArea.setBounds(10,10,440,270);
    nameTextArea.setLineWrap(true);

    return nameTextArea;

}

}


Solution

  • You should avoid use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Not only that, JScrollPanes do not work well when the viewport view (the component that you display inside of the JScrollPane) uses null layout. Not only that, if you set the bounds or even the preferred size of a JTextArea, it will not expand inside the JScrollPane and you won't see scrollbars.

    Solution:

    1. Avoid using null layouts like the plague.
    2. Learn and use the layout managers: The Swing Layout Manager Tutorials
    3. Never set a JTextArea's size or preferredSize. Instead consider setting its columns and rows.

    For example:

    import java.awt.BorderLayout;
    import javax.swing.*;
    
    public class MainFrame2 extends JPanel {
    
       private JTabbedPane tabbedPane = new JTabbedPane();
       private JTextArea textArea = new JTextArea(20, 40);
    
       public MainFrame2() {
          tabbedPane.add("Bahahahaha", new JPanel());
          tabbedPane.add("TextArea Info", createTextAreaPane());
    
          setLayout(new BorderLayout());
          add(tabbedPane, BorderLayout.CENTER);
       }
    
       private JComponent createTextAreaPane() {
          JPanel btnPanel = new JPanel();
          btnPanel.add(new JButton("Save"));
    
          JPanel textAreaPane = new JPanel(new BorderLayout());
          textAreaPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
          textAreaPane.add(btnPanel, BorderLayout.SOUTH);
          return textAreaPane;
       }
    
       private static void createAndShowGui() {
          MainFrame2 mainPanel = new MainFrame2();
    
          JFrame frame = new JFrame("Main Frame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }