I have a JTextArea in my GUI (my GUI class extends JFrame), which I have added using the following code:
public static JTextArea displayOutput = new JTextArea(text, rows, columns);
displayOutput.setBounds(10, 610, 700, 450);
panel.add(displayOutput);
displayOutput.setEditable(false);
I now want to add a scroll bar to the JTextArea, and have tried doing this by adding the lines:
JScrollPane displayOutputScroll = new JScrollPane(displayOutput);
displayOutputScroll.setVericalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
But, I get a syntax error on "setVerticalScrollBarPolicy", which says that an Identifier is expected after this token.
I have also tried adding the scrollbar with this line instead:
JScrollPane displayOutputScroll = new JScrollPane(displayOutput, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
But get a compile error that says: "The constructor JScrollPane(JTextArea, int) is undefined.
Can someone point out to me what I'm doing wrong here? How can I add a scroll bar to the JTextArea?
Here is the full code for my Gui class, as requested:
package openDIS;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Gui extends JFrame{
public static String text = "";
public static int rows = 20;
public static int columns = 5;
public static JTextArea receivingNetworkInfo = new JTextArea(text, rows, columns);
public static JTextArea sendingNetworkInfo = new JTextArea(text, rows, columns);
public static JTextArea displayOutput = new JTextArea(text, rows, columns);
public static JTextArea displayFilteredOutput = new JTextArea(text, rows, columns);
/*Add scroll bars to the display areas */
JScrollPane displayOutputScroll = new JScrollPane(displayOutput);
displayOutputScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
/*Create a JComboBox to display filter options, and JTextField to allow user to enter filter criteria */
public static String[] filterOptions = {"", "Site", "Application", "ID"};
public static JComboBox<String> filter1 = new JComboBox<String>(filterOptions); /*Adding the '<String>' parameters to JComboBox got rid of the warnings 29/04/2014 */
public static JTextField filter1Text = new JTextField();
public static JComboBox filter2 = new JComboBox(filterOptions);
public static JTextField filter2Text = new JTextField();
public static JComboBox filter3 = new JComboBox(filterOptions);
public static JTextField filter3Text = new JTextField();
public static boolean filterButtonClicked;
/*Create an ExecutorService and then call shutdown in my ActionListener for stop */
public static final ExecutorService pool = Executors.newSingleThreadExecutor();
public Gui(){
setTitle("DIS Filter");
setSize(1000, 1000);
setLocation (10, 10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
initGui();
}
private void initGui(){
//JFrame frame = new JFrame();
JPanel panel = new JPanel();
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("DIS Filter");
this.setSize(1400, 1100);
EspduSender.initSocket();
panel.setLayout(null);
/*Add JTextArea to display receivingNetwork info */
receivingNetworkInfo.setBounds(10, 10,700, 60);
panel.add(receivingNetworkInfo);
receivingNetworkInfo.setEditable(false);
receivingNetworkInfo.setText("Default receiving broadcast address: " + Networks.DEFAULT_BROADCAST_ADDRESS_STRING + "\n" +
"Default receiving multicast group: " + Networks.DEFAULT_MULTICAST_GROUP_STRING + "\n" +
"Default receiving port: " + Networks.receivePORT);
/*Add a JTextArea to display the output DIS information */
displayFilteredOutput.setBounds(10, 80, 700, 450);
panel.add(displayFilteredOutput);
displayFilteredOutput.setEditable(false);
/*Add JTextArea to display sendingNetwork info */
sendingNetworkInfo.setBounds(10, 540, 700, 60);
panel.add(sendingNetworkInfo);
sendingNetworkInfo.setEditable(false);
sendingNetworkInfo.setText("Default sending broadcast address: " + Networks.DEFAULT_BROADCAST_ADDRESS_STRING + "\n" +
"Default sending multicast group: " + Networks.DEFAULT_MULTICAST_GROUP_STRING + "\n" +
"Default sending PORT: " + Networks.sendPORT);
/*Add displayFilteredOutput JTextArea */
displayOutput.setBounds(10, 610, 700, 450);
panel.add(displayOutput);
displayOutput.setEditable(false);
add(panel);
//displayOutput.append("hello");
/*The code underneath here will never be reached unless I specify how long to perform this method call for- */
/*Since the receivePdu() method has no 'end' condition- it keeps looping continually until told to stop */
/*Try using a timer to specify how long it should be called for */
long start = System.currentTimeMillis();
long end = start + 60*1000; /* 60 seconds * 1000 ms/sec */
/*Move pool ExecutorService to top of class, make it public so that it can be used in EspduReceiver.java (06/05/2014 @ 11:00) */
/*Create an ExecutorService and then call shutdown in my ActionListener for stop */
// final ExecutorService pool = Executors.newSingleThreadExecutor();
/*Create 'Quit' button and add it to the panel */
JButton quitButton = new JButton("Quit");
panel.add(quitButton);
quitButton.setBounds(1080, 500, 80, 30); /*Set the location of the button in the window, and its size */
quitButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
pool.shutdown();
}
});
setLocationRelativeTo(null);
panel.repaint();
setDefaultCloseOperation(EXIT_ON_CLOSE);
/*Create 'StartCapture' button and add it to the panel */
JButton startCaptureButton = new JButton("Start");
panel.add(startCaptureButton);
startCaptureButton.setBounds(1080, 350, 80, 30);
startCaptureButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Filter.getFilterConditions();
EspduReceiver.startCapture();
EspduReceiver.stopCapture = false;
EspduSender.senderThread = new Thread(){
public void run(){
//FilteredPdu.displayFilteredPdu();
/*Need to add a call to stop Filter.retrieveFilteredPdu() before calling the Filter.filterPDUs method (14/05/2014 @ 15:00) */
/* EspduReceiver.stopCapture = true; /*Setting 'stopCapture' to true will cause the capture to stop when the Filter button is
pressed- use another boolean that will stop the call to Filter.retrieveFilteredPdu()*/
/* EspduReceiver.pauseCapture = true;
Filter.pauseCaptureForFilter(); /*Don't need a call to these two lines */
try {
EspduSender.sendPdu();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("UnknownHostException in call to EspduSender.sendPdu(); from startCaptureButton ActionListener");
e.printStackTrace();
}
/* Filter.filterPDUs(); /* Try commenting this line since it's now been added to the invokeLater() method in the main(), at end of
* this class. It is now being called from the Filter.pauseCaptureForFilter() method (14/05/2014 @ 15:40)
*/
}
// EspduSender.senderThread.start();
};
EspduSender.senderThread.start();
}
});
/*Create 'StopCapture' button and add it to the panel */
JButton stopCaptureButton = new JButton("Stop");
panel.add(stopCaptureButton);
stopCaptureButton.setBounds(1080, 400, 80, 30);
stopCaptureButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
EspduReceiver.stopCapture = true;
}
});
/*Add the filters to the window */
panel.add(filter1);
filter1.setBounds(720, 50, 100, 30);
panel.add(filter1Text);
filter1Text.setBounds(850, 50, 200, 30);
panel.add(filter2);
filter2.setBounds(720, 90, 100, 30);
panel.add(filter2Text);
filter2Text.setBounds(850, 90, 200, 30);
panel.add(filter3);
filter3.setBounds(720, 130, 100, 30);
panel.add(filter3Text);
filter3Text.setBounds(850, 130, 200, 30);
/*Add buttons that will call 'retrieve' functions from Filter.java, and display only the entities that match the filtered
* values in the GUI. */
/*Get entities with matching site */
JButton filterButton = new JButton("Filter");
panel.add (filterButton);
filterButton.setBounds(900, 400, 80, 30);
filterButton.addActionListener(new ActionListener(){
boolean filterButtonClicked = true;
@Override
public void actionPerformed(ActionEvent e){
FilteredPdu.thread = new Thread(){
public void run(){
//FilteredPdu.displayFilteredPdu();
/*Need to add a call to stop Filter.retrieveFilteredPdu() before calling the Filter.filterPDUs method (14/05/2014 @ 15:00) */
/* EspduReceiver.stopCapture = true; /*Setting 'stopCapture' to true will cause the capture to stop when the Filter button is
pressed- use another boolean that will stop the call to Filter.retrieveFilteredPdu()*/
EspduReceiver.pauseCapture = true;
Filter.pauseCaptureForFilter();
/* Filter.filterPDUs(); /* Try commenting this line since it's now been added to the invokeLater() method in the main(), at end of
* this class. It is now being called from the Filter.pauseCaptureForFilter() method (14/05/2014 @ 15:40)
*/
}
};
FilteredPdu.thread.start();
// EspduReceiver.receiveFilteredPdu();
/*Need to check if there are any PDUs stored in 'entity' arrays first- if there aren't, then add a call to receivePdu() at
* the start of this method; if there are, then get the value entered into the text box, and search through the array for
* a matching value. */
Filter.numMatchingAppsFound = 0; /*Reset this variable to 0 every time the button is clicked, so that the count doesn't carry over. */
Filter.numMatchingSitesFound = 0; /*Reset this variable to 0 every time the button is clicked, so that the count doesn't carry over. */
/*Reseting these values to 0 here doesn't seem to work for some reason. */
//displayOutput.setText(null); /*Clear the display, so that the user only sees the results from the most recent button press. */
/*Call 'displayFilteredPdu()' method here */
// FilteredPdu.displayFilteredPdu();
/*Get matching site values from Sites ArrayList */
try{
//EspduReceiver.receivePdu(); /*I don't necessarily want to call receivePdu() here */
if(EspduReceiver.entitySite.get(0) == null){
// EspduReceiver.receivePdu();
// Filter.retrieveFilteredSite(Integer.parseInt(filter1Text.getText()));
System.out.println("Please enter a Site value. ");
} else {
/*Need to add code to retrieve value of 'filter1Text */
Filter.retrieveFilteredSite(Integer.parseInt(filter1Text.getText()));
//Filter.numMatchingSitesFound++;
}
if((EspduReceiver.entityApplication.get(0) == null) || (filter2Text.getText() == null)){
// Filter.retrieveFilteredApplication(Integer.parseInt(filter2Text.getText()));
System.out.println("Please enter an Application value. ");
} else {
Filter.retrieveFilteredApplication(Integer.parseInt(filter2Text.getText()));
//Filter.numMatchingAppsFound++;
}
if((EspduReceiver.entity.get(0) == null) || filter3Text.getText() == null){
System.out.println("Please enter an ID value. ");
} else {
Filter.retrieveFilteredID(Integer.parseInt(filter3Text.getText()));
}
}catch(Exception ex){
System.out.println("No PDUs have yet been received. You must receive at least one PDU before you can search for a matching site entity. " + ex.toString() + "\n" +
"or, you have selected a filter, but not entered a filter value. ");
}
/*Get matching applications from Application ArrayList. This code doesn't work- try adding it to the existing try/catch block instead 30/04/2014 @ 15:10
try{
if(EspduReceiver.entityApplication.get(0) == null){
EspduReceiver.receivePdu();
Filter.retrieveFilteredApplication(Integer.parseInt(filter2Text.getText()));
} else {
Filter.retrieveFilteredApplication(Integer.parseInt(filter2Text.getText()));
}
}catch(Exception ex2){
System.out.println("No PDUs have yet been received. You must receive at least one PDU before you can search for a matching application entity. ");
} */
displayFilteredOutput.setText("Total number of entities with matching Site value: " + /* Filter.matchingSite.length */ Filter.numMatchingSitesFound + "\n" +
" Total number of entities with matching Application value: " + /* Filter.matchingApplication.length */ Filter.numMatchingAppsFound + "\n" +
" Total number of entities with matching ID value: " + /* Filter.matchingID.length */ Filter.numMatchingIDsFound );
/*Write a method that will only add the filtered PDUs to 'displayFilteredOutput' here, and call that method within these
* parenthesis. 01/05/2014 */
//displayFilteredOutput.setText("");
}
});
}
public static void main(String[] args){ /* I also have a main in EspduSender.java- probably need to get rid of that one (14/05/2014) */
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
Gui gui = new Gui();
gui.setVisible(true);
/* if(filterButtonClicked == true){
Filter.filterPDUs();
} */
}
});
}
}
you can not put statements like this out of any methods:
displayOutputScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Move this inside initGUI()
that will resolve your problem.
EDIT
instead adding displayOutput
to your panel
displayOutput.setBounds(10, 610, 700, 450);
panel.add(displayOutput);
add displayOutputScroll
to the panel.
displayOutputScroll.setBounds(10, 610, 700, 450);
panel.add(displayOutputScroll);