Home > Net >  My code is not working. How should I add a JScrollPane on a JTextArea that is in a JTabbedPane?
My code is not working. How should I add a JScrollPane on a JTextArea that is in a JTabbedPane?

Time:12-04

this is the code I have so far

public class WindowHelpGui extends JFrame{
    JScrollPane scroll;
    //constructor
    public WindowHelpGui(){
         //add window title
        super("Help");
        //set window layout
        setLayout(null);

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setBounds(10, 10, 750, 550);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        panel1.add(new JTextArea("\nDRAFT TEXT 101 
\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnidfnviofdnvindfinv
ivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnviofnvifdnv
fndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nh" 
       "e\nr\n\ns\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
nHello")); 
       panel2.add(new JTextArea("panel2 working"));
       panel3.add(new JTextArea("panel3 working"));
       JScrollPane scroll = new JScrollPane(panel1, JScrollPane.
HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDE
       add(scroll, panel1);

       tabbedPane.add("How to play", panel1);
       tabbedPane.add("Seeds", panel2);
       tabbedPane.add("Tools", panel3);
    
       //scroll = new Jscro
       this.add(tabbedPane);

        //set size of window
        setSize(800,600);
        //set visibility
        setVisible(true);
        //set resizable 
        setResizable(false);
        //set default close
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

when I run this I dont see any window popping up. Can someone help me please?

I have tried also tried using add(scroll, tabbedPane) but it just adds a new tab on my window.

CodePudding user response:

Call pack() before making your JFrame visible and use setPreferredSize() of the Jframe instead of setSize().

Add a setMinimumSize() as well to the JFrame.

Also there can only be one root component of a JFrame. Use a JPanel with a layout to add more components.

Don't use a null layout. It will require you to explicitly set position and size of each child component by using setBounds() method of each child.

Example code:

JFrame frame = new JFrame("Simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
JLabel textLabel = new JLabel("I'm a label in the window",SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));      
frame.getContentPane().add(textLabel, BorderLayout.CENTER);       
//Display the window.       
frame.setLocationRelativeTo(null);       
frame.pack();       
frame.setVisible(true);

Read more about JFrame and how it works from here.

CodePudding user response:

The JTextArea must be added directly to the JScrollPane and you can add the scrollpane directly to the tabbedPane:

    JTextArea textArea = new JTextArea("\nDRAFT TEXT 101\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnidfnviofdnvindfinvivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnviofnvifdnvfndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nh"  
            "e\nr\n\ns\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nn\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nnHello");
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    tabbedPane.add("How to play", scrollPane);

CodePudding user response:

You set the JTextArea into the ScrollPane constructor. Here is a runnable example (read comments in code):

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class WindowHelpGui extends JFrame {

    private static final long serialVersionUID = 34534L;
    
    private JTextArea textArea_1;
    private JTextArea textArea_2;
    private JTextArea textArea_3;
    
    private JTabbedPane tabbedPane;
    

    //constructor
    public WindowHelpGui() {
        createForm();
    }

    private void createForm() {
        //set window layout
//        setLayout(null);  // If possible, don't ever use a Null layout.

        //set resizable 
//        setResizable(false);  // It's nice to be able to resize from time to time.

        // add window title
        setTitle("Help");
        
        // set size of window
        setPreferredSize(new Dimension(800, 600));

        //set default close
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // OPTIONAL - Set form on top of everything.
        setAlwaysOnTop(true);

        tabbedPane = new JTabbedPane();

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        textArea_1 = new JTextArea();
        textArea_1.setText("\nDRAFT TEXT 101\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnid"
                  "fnviofdnvindfinvivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnvio"
                  "fnvifdnvfndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nhe\nr\n\n"
                  "s\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
                  "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nHello");
        textArea_1.setCaretPosition(0);  // Set the caret to beginning of document.
        
        textArea_2 = new JTextArea();
        textArea_2.setText("Seeds Area Working");
        
        textArea_3 = new JTextArea();
        textArea_3.setText("Tools Area Working");

        JScrollPane scroll_1 = new JScrollPane();
        scroll_1.setViewportView(textArea_1);
        JScrollPane scroll_2 = new JScrollPane();
        scroll_2.setViewportView(textArea_2);
        JScrollPane scroll_3 = new JScrollPane();
        scroll_3.setViewportView(textArea_3);
        
        tabbedPane.add("How to play", scroll_1);
        tabbedPane.add("Seeds", scroll_2);
        tabbedPane.add("Tools", scroll_3);

        add(tabbedPane);
        
        pack(); // Now you can resize or maximize the form and everything will resize with it.
        
        setVisible(true);
        // Set Screen Location (after the pack())
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        new WindowHelpGui();
    }
    
}
  • Related