Label not updating in same instance of GUI - java

I have a label that isn't updating in the same instance of the GUI.
If I click the jButton that should update the value on a jLabel ("testLabel" from block of code), I have to run the java program again to see the change appear. How can I get it to appear on button click in the same instance?
I know about invokelater and I've been playing with it trying to get it to update in real time, but with no luck. I've been stuck on this for awhile now so any help is appreciated.
With the block of code listed below I still have to run a new instance of the GUI to get the value to update.
Related code:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MISControlPanel window = new MISControlPanel();
window.frame.setVisible(true);
// testLabel.setText(CN);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JButton searchComputerButton = new JButton("Search");
searchComputerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String line;
BufferedWriter bw = null;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(tempFile));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// String lineToRemove = "OU=Workstations";
String s = null;
Process p = null;
/*
* try { // p = Runtime.getRuntime().exec(
* "cmd /c start c:\\computerQuery.bat computerName"); } catch
* (IOException e1) { // TODO Auto-generated catch block
* e1.printStackTrace(); }
*/
try {
p = Runtime.getRuntime().exec("c:\\computerQuery.bat");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer sbuffer = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
// textArea.append(line);
String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
LdapName ldapName = new LdapName(dn);
String commonName = (String) ldapName.getRdn(
ldapName.size() - 1).getValue();
}
ComputerQuery.sendParam();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidNameException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally
{
try {
fw.close();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ComputerQuery.sendParam();
}
});
try (BufferedReader br = new BufferedReader(new FileReader(
"resultofbatch.txt"))) {
final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");
try {
while ((sCurrentLine = br.readLine()) != null) {
String[] tokens = PATTERN.split(","); // This will return
// you a array,
// containing the
// string array
// splitted by what
// you write inside
// it.
// should be in your case the split, since they are
// seperated by ","
// System.out.println(sCurrentLine);
CN = sCurrentLine.split("CN=", -1)[1].split(",", -1)[0];
System.out.println(CN);
testLabel.setText(CN);
}
Full Class Code
http://pastebin.com/havyqMxP
Computer Query Class (Small Class)
http://pastebin.com/Q89BCjya

As promised... here is a simple example of fetching an URL content using swing worker to decouple the task of getting the contents (the long running task) from the task that update the swing components. This will show an example of how you should approach this issue...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
/* FrameDemo.java requires no other files. */
public class MainWindow extends JFrame {
private static final Logger LOOGER = Logger.getLogger(MainWindow.class.getName());
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private JLabel statusLabel = new JLabel("Status");
private JButton actionButton = new JButton("Push me");
public MainWindow() {
super("FrameDemo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
statusLabel = new JLabel("Status");
actionButton = new JButton("Push me");
statusLabel.setPreferredSize(new Dimension(400, 50));
actionButton.setPreferredSize(new Dimension(100, 50));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(statusLabel, BorderLayout.NORTH);
getContentPane().add(actionButton, BorderLayout.CENTER);
actionButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// THIS METHOD IS INVOKED WITHIN THE EVENT DISPATCH THREAD!!.. SO IS CRUCIAL TO NOT PERFORM
// HERE LONG TIME RUNNING TASKS...
actionButton.setEnabled(false); // let's disable this button, in order to avoid invoking
// multiple times the same task and messing up the entire app...
UrlFechterSwingWorker urlFechterSwingWorker = new UrlFechterSwingWorker();
urlFechterSwingWorker.execute();
}
});
}
public void display() {
pack();
setVisible(true);
}
private class UrlFechterSwingWorker extends SwingWorker<String, String> {
#Override
public String doInBackground() { // this method is executed under a worker thread
BufferedReader in;
StringBuilder sb = new StringBuilder();
try {
URL url = new URL("http://www.stackoverflow.com");
in = new BufferedReader(new InputStreamReader(url.openStream()));
String line = in.readLine();
while (line != null) {
sb.append(line);
publish(line); // publish partial results....
line = in.readLine();
}
in.close();
} catch (Exception e) {
LOOGER.log(Level.SEVERE, "", e);
}
return sb.toString();
}
#Override
protected void process(List<String> readedLines) { // this method in the Event dispatch Thread
// do what you want to do with the readedLines....
statusLabel.setText("The doInBackground read... " + readedLines.size() + " lines");
}
#Override
public void done() { // this method in the Event dispatch Thread
// do what you want when the process finish
actionButton.setEnabled(true); // well.. at least i would like to enable the button again...
}
}
public static void main(String[] args) {
MainWindow mainWindow = new MainWindow();
mainWindow.display();
}
}
Here are more tips:
After you understand (more or less) how things are working in the above example.. you will have to implement a proper doInBackground method that perform all the LDAP stuff, for this you will have to make your mind to conceive a way of informing the progress to a end user... i mean, look my example, is very poor regarding progress advance.. all the thing that i am stating is that, we read "a number of lines" from a given url. You should think what is the best way to inform progress regarding your task.. there is no template for this except the understanding of the underlying domain model.
Have in mind that swing workers has two ways to inform about the progress.
One is using the publish and process method in conjunction. (Like the example above)
Other is to to modify internal properties (progress and state) inside the method doInBackground, and attach a PropertyChangeListener to the swing worker. This propertyChangeListener object has a method whose signature is public void propertyChange(PropertyChangeEvent evt) (a little more complex, on my point of view..)
Patience and good luck!

Related

How to fit JTextArea as my file require?

I want my JTextArea to show text as in the txt file. But it is showing the whole text in only row.
http://pastebin.com/Y8vWUvtg
package jBoxThreadTry;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class GuestFinal extends JFrame implements Runnable {
private JLabel test;
private JTextArea txtArea;
private String titleBar;
private static String fileName;
private String[] CSEterms = {"CSE11.txt", "CSE12.txt", "CSE21.txt",
"CSE22.txt", "CSE31.txt", "CSE32.txt", "CSE41.txt", "CSE42.txt"};
private boolean threadAliveFlag;
public GuestFinal(boolean threadAliveFlag) {
// TODO Auto-generated constructor stub
// super()
this.threadAliveFlag = threadAliveFlag;
}
#Override
public void run() {
// TODO Auto-generated method stub
try {
while (threadAliveFlag) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
/*test = new JLabel("yes");
add(test);
*/
setTitle(titleBar);
threadAliveFlag = false;
}
} catch (Exception e) {
// TODO: handle exception
}
}
public void setBool(boolean b) {
// TODO Auto-generated method stub
threadAliveFlag = b;
}
public void setTitleBar(String string) {
// TODO Auto-generated method stub
titleBar = "Syllabus for " + string;
}
public void setFileToShow(int selectedIndex) {
// TODO Auto-generated method stub
fileName = CSEterms[selectedIndex];
showFile(fileName);
}
private void showFile(String fName) {
// TODO Auto-generated method stub
try {
FileInputStream fstream = new FileInputStream("syllabusDir\\"
+ fName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String line = br.readLine();
StringBuilder strBuilder = new StringBuilder();
while (line != null) {
// Print the content on the console
System.out.println(line);
strBuilder.append(line);
line = br.readLine();
}
String everything = strBuilder.toString();
txtArea = new JTextArea(everything);
add(txtArea);
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
"I want my JTextArea to show text as in the txt file. But it is showing the whole text in only row"
while (line != null) {
// Print the content on the console
System.out.println(line);
strBuilder.append(line);
line = br.readLine();
}
A string is just a long sequence of character. So what you are doing is just appending to the same sequence of characters. The way to separate lines is to make use of the line separator \n character. So you want to append that after every line.
strBuilder.append(line);
strBuilder.append("\n");
Alternativelive, not much of a difference in this case, but JTextArea also has an append method.
UPDATE
The most resonable approach is to just use the JTextArea.read() method, which you can pass the BufferedReader to, and that will read the whole file the text area. No need to loop and append.
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
JTextArea area = new JTextArea(10, 50);
area.read(reader, null);
Simple as that
This block of code:
while (line != null) {
// Print the content on the console
System.out.println(line);
strBuilder.append(line);
line = br.readLine();
}
change strBuilder.append(line); to strBuilder.append(line+"\n");
Append will not add the newline as you are intending here.
Also, use txtArea.setLineWrap(true); to ensure lines will always be wrapped if you are looking for that functionality.
Just to add a convenient way to read files using streams and NIO
public String readFile(String fName) throws IOException {
List<String> contents = Files.readAllLines(FileSystems.getDefault().getPath("syllabusDir\\"
+ fName));
return contents
.stream()
.collect(Collectors.joining("\n"));
}
Also see java.nio.file.Files#readAllLines(java.nio.file.Path, java.nio.charset.Charset)

Code tells me I need to implement ActionListener when I already have?

So I am making this code to write to a file based on user clicks. The only problem I have, is that I get an error on "public class prog". The prog name is where I get the error: It says: The type prog must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent). When I do the quickfix of adding the uninherited methods, it adds the action listener method to the end of my code but with nothing in it. If I already have action listeners in the program, why does it tell me I need to implement them? And why when I add it at the end, does it work fine even though nothing is in it?
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.*;
public class prog extends JFrame implements ActionListener {
//create newLine
final String newLine = System.getProperty("line.separator");
//create buttons
JPanel row1 = new JPanel();
JButton oneLeft = new JButton("oneLeft");
JButton oneRight = new JButton("oneRight");
JPanel row2 = new JPanel();
JButton twoLeft = new JButton("twoLeft");
JButton twoRight = new JButton("twoRight");
JPanel row3 = new JPanel();
JButton threeLeft = new JButton("threeLeft");
JButton threeRight = new JButton("threeRight");
public prog() {
super("Prog");
setLookAndFeel();
setSize(400, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(3, 2);
setLayout(layout);
//create outStream for writing to file
try {
final File numClicks = new File("numClicks.properties");
final FileOutputStream outStream = new FileOutputStream(numClicks);
//add Listeners
oneLeft.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "oneLeft has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
oneRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "oneRight has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
twoLeft.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "twoLeft has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
twoRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "twoRight has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
threeLeft.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "threeLeft has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
threeRight.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
write(outStream, "threeRight has been clicked.");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
} catch (IOException ioe) {
System.out.println("The file could not be written.");
}
row1.add(oneLeft);
row1.add(oneRight);
row2.add(twoLeft);
row2.add(twoRight);
row3.add(threeLeft);
row3.add(threeRight);
add(row1);
add(row2);
add(row3);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
//ignore error
}
}
void write(FileOutputStream stream, String output) throws IOException {
output = output + newLine;
byte[] data = output.getBytes();
stream.write(data, 0, data.length);
}
public static void main(String[] args) {
prog progApp = new prog();
}
}
Your class shouldn't implement ActionListener. Instead of writing a top-level class that implements the interface, you're writing a bunch of little inline classes (called anonymous inner classes) that do this work for you when you say new ActionListener().
You implement ActionListener it, but you don't actually implement the required methods (i.e., actionPerformed()). Therefore your class is invalid to the compiler.
You need a method like:
#Override
public void actionPerformed(ActionEvent e) {
// ...
}
The way an interface works is that it defines what the classes that implements it have to... well... implement. That way any other process can treat it as an ActionListener and know that certain methods have been defined.
Just another way Java tries to make polymorphism your friend.
To address something from the comment below, it's actually not that uncommon to see a class implement an interface (like KeyListener) and define the method without even using it.
For example, KeyListener requires you to implement three different methods:
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
public void keyTyped(KeyEvent e);
Let's say I only really care about keyPressed. Then my class might look something like this:
public class MyKeyListener implements KeyListener {
#Override
public void keyPressed(KeyEvent e) {
// do stuff
}
#Override
public void keyReleased(KeyEvent e){}
#Override
public void keyTyped(KeyEvent e){}
}

java GUI trying to get multiple inputs from multiple text fields

How do you get multiple inputs from the multiple textfields you have created?
I want one text field to get the port number, another to get file location.
So once the user enter the int and the string, I can use these inputs for the program.
I am new to this, so when I tried to implement this, I would enter the port number, and suddenly the UI seems to "freeze" and I can't enter the file location.
constructor for frame
public TcpServerCompareCSV () {
setLayout(new FlowLayout());
// "this" Frame sets its layout to FlowLayout, which arranges the components
// from left-to-right, and flow to next row from top-to-bottom.
lblPort = new Label("Port"); // construct Label
add(lblPort); // "this" Frame adds Label
tfPort = new TextField("0", 10); // construct TextField
tfPort.setEditable(true); //edit text
add(tfPort); // "this" Frame adds tfCount
tfPort.addActionListener(this); // for event-handling
lblLocation = new Label("CSV File Location"); // construct Label
add(lblLocation); // "this" Frame adds Label
tfLocation = new TextField("text", 40); // construct TextField
tfLocation.setEditable(true); //edit text
add(tfLocation); // "this" Frame adds tfCount
tfLocation.addActionListener(this);
setTitle("compare"); // "this" Frame sets title
setSize(250, 100); // "this" Frame sets initial window size
setVisible(true); // "this" Frame shows
addWindowListener(this);
// "this" Frame fires WindowEvent its registered WindowEvent listener
// "this" Frame adds "this" object as a WindowEvent listener
}
action event
/** ActionEvent handler - Called back when user clicks the button. */
#Override
public void actionPerformed(ActionEvent evt) {
// Get the String entered into the TextField tfPort, convert to int
port = Integer.parseInt(tfPort.getText());
fileLocation = tfLocation.getText();
String csvName = fileLocation;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 57635.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection.....");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
System.out.println ("Waiting for input.....");
PrintWriter out = null;
try {
out = new PrintWriter(clientSocket.getOutputStream(),
true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputLine, outputLine;
try {
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);
if (inputLine.trim().equals("Bye.")) {
System.out.println("Exit program");
break;
}
Scanner input1 = new Scanner(new File(csvName));
Scanner input2 = new Scanner(new File(csvName));
Scanner input3 = new Scanner(new File(csvName));
Scanner input4 = new Scanner(new File(csvName));
String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1, input2)), getCSVLine( input3, input4) );
outputLine = compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));
out.println(outputLine);
input1.close();
input2.close();
input3.close();
input4.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
By adding the ActionListener to both text fields, if I remember correctly, they will fire the event as soon as you hit Return in one of them.
That will trigger your network code immediately.
You should only register the action for the button.
Moreover, as was already pointed out in a comment, you are executing network communications on the GUI thread, which is causing the freeze.
In the action implementation, you must spawn a separate thread to do the actual network communications to prevent the blocking. To find out how that works, look at the documentation for Runnable and the Executor framework.
Only call addActionListener(this); for your button, don't listen to actions on your textFields, because your actionPerformed method is being called when actions occur on your textfield (user hits enter). I'm guessing your doing this after the first field and that your action method is getting called by this and then waiting for the socket connection, as that is a blocking call, this would make your GUI unresponsive.

why won't my text area show up

I'm trying to implement a program where the client will send a fix message to the server side, and the UI I have for the server side will show up the FIX message on the text area.
However when I start the program, the textfields and all the labels will show, except the text area. And when I try send a message from the client side, everything works besides the text area.
code
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.awt.*; // using AWT containers and components
import java.awt.event.*; // using AWT events and listener interfaces
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TcpServerCompareCSV extends Frame implements ActionListener , WindowListener {
private Label lblPort; // declare component Label
private TextField tfPort; // declare component TextField
private int port; // port number
private Label lblLocation; // declare component Label
private TextField tfLocation; // declare component TextField
private String fileLocation; // csv file location
private Button btnCount; // declare component Button //________________________________________________________________________________\\
private Label lblCSVLine; // declare component Label from csv file line, server side
private TextField tfCSVLine; // declare component TextField from csv file line, server side
private String CSVLine; // port number
private Label lblFIXMsg; // declare component Label from csv file line, server side
private JTextArea tfFIXMsg; // declare component TextField from csv file line, server side
private String FIXMsg; // port number
private JScrollPane jScrollPane1;//________________________________________________________________________________\\
/** WindowEvent handlers */
// Called back upon clicking close-window button
#Override
public void windowClosing(WindowEvent e) {
System.exit(0); // terminate the program
}
//constructor for frame
public TcpServerCompareCSV () {
setLayout(new FlowLayout());
// "this" Frame sets its layout to FlowLayout, which arranges the components
// from left-to-right, and flow to next row from top-to-bottom.
lblPort = new Label("Port"); // construct Label
add(lblPort); // "this" Frame adds Label
tfPort = new TextField("0", 40); // construct TextField
tfPort.setEditable(true); //edit text
add(tfPort); // "this" Frame adds tfCount
// tfPort.addActionListener(this); // for event-handling
lblLocation = new Label("CSV File Location"); // construct Label
add(lblLocation); // "this" Frame adds Label
tfLocation = new TextField("text", 40); // construct TextField
tfLocation.setEditable(true); //edit text
add(tfLocation); // "this" Frame adds tfCount
//________________________________________________________________________________\\
setTitle("compare"); // "this" Frame sets title
setSize(800,200); // "this" Frame sets initial window size
setVisible(true); // "this" Frame shows
lblCSVLine = new Label("CSV Line"); // construct Label
add(lblCSVLine); // "this" Frame adds Label
tfCSVLine = new TextField("text", 40); // construct TextField
tfCSVLine.setEditable(false); //edit text
add(tfCSVLine); // "this" Frame adds tfCount
lblFIXMsg = new Label("FIX message from client"); // construct Label
add(lblFIXMsg); // "this" Frame adds Label
tfFIXMsg = new JTextArea(); // construct TextField
tfFIXMsg.setColumns(20);
tfFIXMsg.setLineWrap(true);
tfFIXMsg.setRows(50);
tfFIXMsg.setWrapStyleWord(true);
tfFIXMsg.setEditable(false); //edit text
add(tfFIXMsg); // "this" Frame adds tfCount
jScrollPane1 = new JScrollPane(tfFIXMsg);
add(jScrollPane1);
btnCount = new Button("Enter"); // construct Button
add(btnCount); // "this" Frame adds Button
btnCount.addActionListener(this); // for event-handling
addWindowListener(this);
// "this" Frame fires WindowEvent its registered WindowEvent listener
// "this" Frame adds "this" object as a WindowEvent listener
}
/** ActionEvent handler - Called back when user clicks the button. */
#Override
public void actionPerformed(ActionEvent evt) {
// Get the String entered into the TextField tfPort, convert to int
port = Integer.parseInt(tfPort.getText());
fileLocation = tfLocation.getText();
String csvName = fileLocation;
/*
Scanner console = new Scanner(System.in);
System.out.println("Type in CSV file location: ");
//String csvName = console.nextLine();
String csvName = "C:\\Users\\I593458\\Downloads\\orders.csv";
*/
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 57635.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection.....");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
System.out.println ("Waiting for input.....");
PrintWriter out = null;
try {
out = new PrintWriter(clientSocket.getOutputStream(),
true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputLine, outputLine;
try {
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);
tfFIXMsg.setText(inputLine);
if (inputLine.trim().equals("Bye.")) {
System.out.println("Exit program");
break;
}
Scanner input1 = new Scanner(new File(csvName));
Scanner input2 = new Scanner(new File(csvName));
Scanner input3 = new Scanner(new File(csvName));
Scanner input4 = new Scanner(new File(csvName));
String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1, input2)), getCSVLine( input3, input4) );
outputLine = compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));
out.println(outputLine);
tfCSVLine.setText(outputLine);
input1.close();
input2.close();
input3.close();
input4.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You add the textArea to a JScrollPane, and then never do anything with the pane.
jScrollPane1 = new JScrollPane(tfFIXMsg);
You need add(jScrollPane1);

Java : UI thread not resuming in two-threaded application

I'm currently building a Java application using two threads :
The first thread is about the UI of the application, and also the processing of command received via the bluetooth thread.
The bluetooth thread is bluetooth server waiting for a robot to connect and handling communication.
As of now, the UI thread is in wait() state until the bluetooth thread gets a new message to process.
The problem is, I can trace the notify/notifyAll call from the bluetooth thread, but my UI is not resuming it's activity.
I am now sure I misunderstood something about the proper way to manage synchronized threads, but I can't figure out what's wrong in my software.
Here is the code for the UI :
package mapper;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
public class MapperUI extends JFrame implements Runnable {
private ArrayList<String> messageArray;
public MapperUI(){
super();
build();
this.setVisible(true);
new Thread(this).start();
}
private void build(){
setTitle("SLAM Mapper");
setSize(600,500);
setLocationRelativeTo(null);
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(buildContentPane());
}
private JPanel buildContentPane(){
JPanel main = new JPanel();
main.setLayout(new BorderLayout());
//TODO Implements auto-generated map after bluetooth communication
MapPanel map = new MapPanel();
main.add(map,BorderLayout.CENTER);
//TODO This fields will be buildt with stored message
JTable positions = new JTable(15,2);
main.add(positions,BorderLayout.EAST);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
JButton bouton = new JButton("Start");
buttonPanel.add(bouton);
JButton bouton2 = new JButton("Send");
buttonPanel.add(bouton2);
main.add(buttonPanel,BorderLayout.SOUTH);
return main;
}
public synchronized void run(){
MapperCom bt = new MapperCom();
while(true){
try {
System.out.println("Mapper is Waiting......");
wait();
String message = bt.getMessage();
this.messageArray.add(message);
bt.setNextCommand(processMessage(message));
notifyAll();
System.out.println("Mapper Notify");
build();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String processMessage(String message){
String command = "";
//TODO Build a response
command = "fffff\n";
return command;
}
}
Here is the bluetoothService :
package mapper;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class MapperCom extends Thread {
public final UUID uuid = new UUID(
"27012f0c68af4fbf8dbe6bbaf7aa432a", false);
public final String name = "Server";
public final String url = "btspp://localhost:" + uuid
+ ";name=" + name
+ ";authenticate=false;encrypt=false;";
private LocalDevice local ;
private StreamConnectionNotifier server ;
private StreamConnection conn ;
private DataInputStream din ;
private DataOutputStream dout ;
private String command;
private String message;
public MapperCom(){
try {
this.command = "";
this.message = "";
System.out.println("Setting device to be discoverable...");
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
System.out.println("Start advertising service...");
server = (StreamConnectionNotifier)Connector.open(url);
System.out.println("Waiting for incoming connection...\n");
conn = server.acceptAndOpen();
System.out.println("Client Connected...");
din = new DataInputStream(conn.openInputStream());
dout = new DataOutputStream(conn.openOutputStream());
new Thread(this).start();
} catch (Exception e) {
System.out.println("Exception Occured: " + e.toString());
}
}
#Override
public synchronized void run(){
System.out.println("Bluetooth Thread Started");
while(true){
try {
String cmd = "";
char c;
System.out.println("Waiting for message");
while (((c = din.readChar()) > 0) && (c!='\n') ){
System.out.println("Char received :"+c);
cmd = cmd + c;
}
storeMessage(cmd);
System.out.println("Bt Notify......");
notifyAll();
System.out.println("Bt is Waiting for a command from mapper......");
wait();
sendResponse();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void sendResponse(){
try {
dout.writeChars(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized String getMessage(){
return this.message;
}
public synchronized void storeMessage (String data){
this.message = data;
System.out.println("Received " + data);
}
public synchronized int setNextCommand (String data){
int result = -1;
//TODO Implement proper protocol
this.command = data;
System.out.println("Sending " + data);
return result;
}
}
I think when you call notifyAll(), it calls this.notifyAll(). Which there are no other elements waiting in the same class. So what you can do is you share the same object over both classes and call obj.wait() and obj.notifyAll(). It will work.
Below is one sample program. Basically there needs some common lock between 2 threads if you want communication using wait & notify.
package ravi.tutorial.java.threads;
public class TestThreads {
/**
* #param args
*/
public static void main(String[] args) {
CommonLock commonLock = new CommonLock();
Thread1 thread1 = new Thread1(commonLock);
Thread2 thread2 = new Thread2(commonLock);
thread1.start();
thread2.start();
}
}
/*
* Common monitor lock between both threads, used for communication using wait
* notify.
*/
class CommonLock {
}
// Extending Thread instead of Runnable as its just a test
class Thread1 extends Thread {
private CommonLock commonLock;
public Thread1(CommonLock commonLock) {
this.commonLock = commonLock;
}
public void run() {
System.out.println("Started thread 1");
System.out.println("waiting thread 1");
try {
// TO wait on commonLock, first need to get lock on commonLock. SO
// put synchronized block of commonLock.
synchronized (commonLock) {
commonLock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("DONE waiting thread 1 as I got notification from THread 2");
}
}
class Thread2 extends Thread {
private CommonLock commonLock;
public Thread2(CommonLock commonLock) {
this.commonLock = commonLock;
}
public void run() {
System.out.println("Running thread 2");
try {
System.out.println("Sleeping thread 2");
// Just take gap of 2 sec before notifying.
Thread.sleep(2000);
// TO notify on commonLock, first need to get lock on commonLock. SO
// put synchronized block of commonLock.
synchronized (commonLock) {
System.out.println("Notifying thread 2");
commonLock.notifyAll();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Look at this: Thread tutorial
wait( ) tells the calling thread to give up the monitor and go to
sleep until some other thread enters the same monitor and calls
notify( ).
notify( ) wakes up the first thread that called wait( ) on
the same object.
notifyAll( ) wakes up all the threads that called wait( ) on the
same object. The highest priority thread will run first.
This is main misunderstanding of thread conception in Java.

Categories