JScrollPane doesn't appear in my JTextArea
This class is the layout and implementation for the first tab in my program, the CreatePanel. I am trying to implement a JScrollPane in my JTextArea. However, the scroll bar never appears, although the program compiles correctly. I've looked all over for solutions and have tried a couple of variations, but nothing is seeming to work.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CreatePanel extends JPanel
{
private Vector flightList;
private SelectPanel sPanel;
private JButton createButton;
private JLabel airName, flightNumber, departCity, departDay, departTime, arriveCity, arriveDay, arriveTime, price;
private JLabel notify, blank;
private JTextField l1, l2, l3, l4, l5, l6, l7, l8, l9;
private JTextArea flightInfo;
private JScrollPane scroll;
String air, num, dC, dD, dT, aC, aD, aT, prc;
//Constructor initializes components and organize them using certain layouts
public CreatePanel(Vector flightList, SelectPanel sPanel)
{
this.flightList = flightList;
this.sPanel = sPanel;
// organize components here
// creates all labels and textfields for Flight Creation tab
notify = new JLabel("");
blank = new JLabel("");
airName = new JLabel("Enter a name of Airlines");
flightNumber = new JLabel("Enter a flight number");
departCity = new JLabel("Enter a departure city");
departDay = new JLabel("Enter a departure day");
departTime = new JLabel("Enter a departure time");
arriveCity = new JLabel("Enter an arrival city");
arriveDay = new JLabel("Enter an arrival day");
arriveTime = new JLabel("Enter an arrival time");
price = new JLabel("Price");
l1 = new JTextField("");
l2 = new JTextField("");
l3 = new JTextField("");
l4 = new JTextField("");
l5 = new JTextField("");
l6 = new JTextField("");
l7 = new JTextField("");
l8 = new JTextField("");
l9 = new JTextField("");
createButton = new JButton("Create a flight");
createButton.addActionListener(new ButtonListener());
flightInfo = new JTextArea("No flight");
flightInfo.setEditable(false);
// the user-entered information panel
JPanel panelUser = new JPanel();
panelUser.setLayout(new GridLayout(10, 2));
panelUser.add(notify);
panelUser.add(blank);
panelUser.add(airName);
panelUser.add(l1);
panelUser.add(flightNumber);
panelUser.add(l2);
panelUser.add(departCity);
panelUser.add(l3);
panelUser.add(departDay);
panelUser.add(l4);
panelUser.开发者_JS百科add(departTime);
panelUser.add(l5);
panelUser.add(arriveCity);
panelUser.add(l6);
panelUser.add(arriveDay);
panelUser.add(l7);
panelUser.add(arriveTime);
panelUser.add(l8);
panelUser.add(price);
panelUser.add(l9);
// panel for the button
JPanel panelButton = new JPanel();
panelButton.add(createButton);
// the leftside panel of CreatePanel
JPanel panelLeft = new JPanel();
panelLeft.setLayout(new BorderLayout());
panelLeft.add(panelUser, BorderLayout.CENTER);
panelLeft.add(panelButton, BorderLayout.SOUTH);
// the rightside panel of CreatePanel
JPanel panelInfo = new JPanel();
scroll = new JScrollPane(flightInfo);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setVisible(true);
panelInfo.setLayout(new GridLayout(1, 1));
panelInfo.add(flightInfo);
// WHY WONT YOU WORK?
// layout for CreatePanel
setLayout(new GridLayout(1, 2));
add(panelLeft);
add(panelInfo);
}
}
What am I doing wrong when adding the JScrollPane??
You have been trying to add the wrong Panel.
Replace panelInfo.add(flightInfo);
with panelInfo.add(scroll)
and you should see your JScrollPane appear (you may need to set the scrollbar policies to "ALWAYS" to demonstrate this).
While I'm at it, may I suggest that you try to make a minimum failing example when you post a question? Apologies if I sound pedantic, but it really makes it easier for others to help you.
You use scrollbar as needed, so the bar will only be visible when you reach the limit of the textarea (overflow, more text than the size :D)
Edit: Additionally, you must add the scroll to the panel and not the flight info
精彩评论