0

I'm creating a notepad program in Java and would like to have different tabs to for each document opened. I'm having trouble getting the tabs to be displayed. This is my test document so far that I have looked over here at first and I modified it for the text document. How to add a JScrollPane onto a JTabbedPane using a null layout?.

This is what I currently have:

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame {

    private static void CreateAndShowGui() {

        JFrame frame = new JFrame("Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextPane txt = new JTextPane();
        JPanel noWrapPanel = new JPanel(new BorderLayout());
        noWrapPanel.add(txt);

        JScrollPane scroll = new JScrollPane(noWrapPanel);

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());

        frame.add(topPanel);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(scroll, BorderLayout.CENTER);

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Welcome", panel);
        topPanel.add(tabbedPane);

        frame.add(scroll);

        frame.setSize(400, 200);
        frame.setVisible(true);

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                UIManager.put("swing.boldmetal", Boolean.FALSE);
                CreateAndShowGui();

            }

        });

    }

}

What am I doing wrong? :(

Community
  • 1
  • 1
Vince
  • 2,596
  • 11
  • 43
  • 76

1 Answers1

2

you are adding scroll pane to frame .

frame.add(scroll);

but you should add scroll pane to jpanel and add pane to scroll pane.you have done that part.

so remove this incorrect line.

frame.add(scroll);

this is complete code without using extends jframe .

this is complete code using extends jframe

note: you have extends your class by jframe class but you are creating a new frame .you don't need to create a frame variable .you can either remove extends part or directly use your class as a jframe without creating a new frame.

enter image description here

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60