简体   繁体   中英

Adding JScrollPane to a JTextArea in a JPanel

I am trying to add JScrollPane to my large JTextArea, but whenever I include the JScrollPane code, my whole JTextArea disappears.

public myGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1174, 656);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    contentPane.setVisible(true);

    JTextArea textArea_1 = new JTextArea();
    textArea_1.setBounds(203, 5, 869, 440);
    textArea_1.setEditable(true);
    textArea_1.setVisible(true);
    contentPane.add(textArea_1);

    JScrollPane scroll = new JScrollPane (textArea_1);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    contentPane.add(scroll);

}

Several problems with your code:

  • You're trying to add a component, here your JTextArea called textArea_1, to multiple containers, here the contentPane and the JScrollPane. You can't do this in Swing as a component can be added to only one component. Add it only to the JScrollPane and not to the contentPane, and then add the scroll pane to the GUI.
  • You're constraining the size of your JTextArea via setBounds which almost guarantees that the JScrollPane will not work since doing this prevents the JTextArea from expanding when it holds more text than is shown. Instead set the JScrollPane's rows and columns properties and not its bounds. This will be the number of rows and columns that it should display within the scrollpane
  • You're using null layouts but not specifying the size of the JScrollPane, and so it defaults to a size of [0, 0] -- and this is why your JTextArea disappears . Null layouts require complete specification of all component sizes and positions.
  • You're using null layouts to set up your GUI. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

For example:

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class MyGuiPanel extends JPanel {
    // some row and column values for our JTextArea
    private static final int TXT_AREA_ROWS = 25;
    private static final int TXT_AREA_COLS = 80;

    // create the JTextArea, passing in the rows and columns values
    private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS);

    public MyGuiPanel() {
        // create the JScrollPane, adding our JTextArea
        JScrollPane scroll = new JScrollPane(textArea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        // lets add some buttons to the bottom of the GUI just for fun
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
        buttonPanel.add(new JButton("Save"));
        buttonPanel.add(new JButton("Open"));
        buttonPanel.add(new JButton("Delete"));
        buttonPanel.add(new JButton("Exit"));

        // let's add a title to the top:
        JLabel title = new JLabel("This is my Applications's Title", SwingConstants.CENTER);
        title.setFont(title.getFont().deriveFont(Font.BOLD, 24)); // and make
                                                                  // the text
                                                                  // *BIG*

        // use a BorderLayout for our GUI
        setLayout(new BorderLayout(5, 5));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        add(scroll, BorderLayout.CENTER); // add the scrollpane to the center
        add(buttonPanel, BorderLayout.PAGE_END); // the button panel to the
                                                 // bottom
        add(title, BorderLayout.PAGE_START); // and the title JLabel to the top
    }

    private static void createAndShowGui() {
        // create our GUI JPanel
        MyGuiPanel mainPanel = new MyGuiPanel();

        // create a JFrame to add it to
        JFrame frame = new JFrame("My GUI");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel); // add the GUI to the JFrame
        frame.pack(); // tell the layout managers to do their work
        frame.setLocationByPlatform(true);
        frame.setVisible(true); // display the GUI
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Also, if your program extends JFrame, understand that you are painting yourself in a corner by doing this, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

You do not need setBounds for your JTextArea. Because you are using a null layout and the JScrollPane has no bounds, nothing shows up. Your JTextArea is also added to two places which would cause some problems. I would recommend any of swings layout managers . As an example using BorderLayout which is one of the easiest managers:

public mygui() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(700, 500);
    setLayout(new BorderLayout());

    JTextArea textArea_1 = new JTextArea();
    textArea_1.setEditable(true);

    JScrollPane scroll = new JScrollPane(textArea_1);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    add(scroll, BorderLayout.CENTER);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM