简体   繁体   中英

How do I make JScrollPane appear with JTextArea?

I am trying to make a UI to view recipes from a cookbook stored on the computer. Part of this tab is a JScrollPanel storing a JTextArea that displays the available recipes. All called functions work as intended (eg allRecipes() returns a string of the available recipes properly); however, the scroll pane itself does not appear. It is added to the frame, as I can see by a small grey block where the pane would be, but it is not filled as it should be. The code is as follows:

//First panel, buttons to limit displayed recipes
    JPanel pane1 = new JPanel();
    JButton all = new JButton("All");
    JButton makeable = new JButton("Makeable");
    JTextField search = new JTextField("", 10);
    JButton searchButton = new JButton("Search Ingredient");

    //Second panel, display of recipes
    JPanel pane2 = new JPanel();
    JTextArea recipes = new JTextArea(allRecipes());
    JLabel list = new JLabel("List of Recipes:");
    JScrollPane scroll = new JScrollPane(recipes);

    //Third panel, options to add recipe and view specific recipe
    JPanel pane3 = new JPanel();
    JButton add = new JButton("Add Recipe");
    JTextField view = new JTextField("", 10);
    JButton viewButton = new JButton("View Recipe");

    //Central method
    public Recipes() {

        //basic UI stuff
        super("Recipes");
        setSize(475,350);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        FlowLayout flo = new FlowLayout();
        setLayout(flo);

        //add pane 1
        pane1.add(all);
        pane1.add(makeable);
        pane1.add(search);
        pane1.add(searchButton);
        pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        add(pane1);

        //add pane 2
        pane2.add(list);
        scroll.setPreferredSize(new Dimension(10,15));
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        pane2.add(scroll, BorderLayout.CENTER);
        pane2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        add(pane2);

        //add pane 3
        pane3.add(add);
        pane3.add(view);
        pane3.add(viewButton);
        add(pane3);

        //start up the UI
        setVisible(true);
    }
JTextArea recipes = new JTextArea(allRecipes());

We don't know what allRecipes() does, but I would guess it sets the text of the text area.

Instead you should define your text area with the rows/columns you wish. Something like:

JTextArea recipes = new JTextArea(5, 30);

then in the constructor you would add the text:

recipes.setText( allRecipes() );

You should NOT be trying to set the preferred size of the scroll pane. The preferred size will automatically be determined from the preferred size of the text area which is calculated based on the rows/columns provided in the constructor.

//scroll.setPreferredSize(new Dimension(10,15));

Also, the preferred size of a component is specified in pixels, to the above makes no sense.

pane2.add(scroll, BorderLayout.CENTER); 

The default layout manager for a JPanel is the FlowLayout. So you can't just use a BorderLayout constraint when adding the component.

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