15,892,161 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by nameJulian (Top 14 by date)
nameJulian
13-Jun-13 3:20am
View
" The PrintStream class enables you to write formatted data to an underlying OutputStream. For instance, writing int, long and other primitive data formatted as text, rather than as their byte values. "
from: http://tutorials.jenkov.com/java-io/printstream.html.
nameJulian
30-May-13 6:54am
View
and the continuity:
public static void main(String[] args) {
//ChatApplication program=new ChatApplication();
//program.createInterface();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
try{
String serverName="localhost";
new ChatApplication(userName, serverName);
window.setTitle(userName);
}catch(IOException ex){
JOptionPane.showMessageDialog(window,"Clientul nu se poate conecta."+ex.getMessage().toString());
}
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sendButton){
sendFlag=true;
try{
String messageToServer=new String("");
messageToServer=messageField.getText();
output.println(messageToServer);
messageField.setText("");
}catch(NullPointerException ex){
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
}
/*
* clasa se ocupa cu crearea unui thread pentru a putea citi de la server
*/
class ServerMessageThread extends Thread{
public void run(){
String serverResponse=new String();
try {
while(true){
serverResponse=input.readLine();
messagesTextArea.append(serverResponse+"\n");
}
} catch (IOException e) {
messagesTextArea.setForeground(Color.RED);
messagesTextArea.setText("Eroare citire de la server: "+e.getMessage());
}
}
}
/*
* clasa care se ocupa cu crearea firelor de executie a clientilor
*/
}
nameJulian
30-May-13 6:47am
View
this is the client class:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class ChatApplication implements ActionListener{
static JFrame window;
static Socket clientSocket;
Socket serverClientSocket;
BufferedReader input;
PrintWriter output;
static JTextArea messagesTextArea;
static JTextArea usersTextArea;
static JTextField messageField;
JButton sendButton;
JButton connectButton;
JButton startServerButton;
String userName=new String();
static boolean sendFlag=false;
public ChatApplication(){}
public ChatApplication(String userName,String serverName) throws UnknownHostException, IOException{
this.userName=userName;
clientSocket=new Socket(serverName,9999);
input=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
output=new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()),true);
output.println(userName);
createInterface();
new ServerMessageThread().start();
}
public void createInterface(){
window=new JFrame();
window.setSize(600,600);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BoxLayout(window.getContentPane(),BoxLayout.Y_AXIS));
sendButton=new JButton("Send");
connectButton=new JButton("Connect");
startServerButton=new JButton("Start");
messageField=new JTextField(10);
messagesTextArea=new JTextArea(40,40);
messagesTextArea.setEditable(false);
usersTextArea=new JTextArea(40,10);
usersTextArea.setEditable(false);
JScrollPane scrollPane=new JScrollPane(messagesTextArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel initialPanel=new JPanel();
initialPanel.setLayout(new BoxLayout(initialPanel,BoxLayout.X_AXIS));
initialPanel.add(connectButton);
initialPanel.add(startServerButton);
JPanel upPanel=new JPanel();
upPanel.setLayout(new BoxLayout(upPanel,BoxLayout.X_AXIS));
upPanel.setBorder(new TitledBorder(""));
upPanel.add(scrollPane);
upPanel.add(usersTextArea);
JPanel bottomPanel=new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel,BoxLayout.X_AXIS));
bottomPanel.setBorder(new TitledBorder(""));
bottomPanel.add(messageField);
bottomPanel.add(sendButton);
window.add(initialPanel);
window.add(upPanel);
window.add(bottomPanel);
sendButton.addActionListener(this);
// connectButton.addActionListener(new ActionListener(){
//
// public void actionPerformed(ActionEvent e){
//
// String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
// try{
// String serverName="localhost";
// new ChatApplication(userName, serverName);
// window.setTitle(userName);
//
// }catch(IOException ex){
// JOptionPane.showMessageDialog(window,"Clientul nu se poate conecta."+ex.getMessage().toString());
// }
//
// }
//
// });
startServerButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Thread thread=new Thread(){
public void run(){
ChatServer server=new ChatServer();
try {
server.process();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
};
thread.start();
}
});
}
public static void main(String[] args) {
//ChatApplication
nameJulian
30-May-13 3:28am
View
I had given up up the default constructor and called the parameterized constructor directrly intro the main method( in the constroctor calling the createInterface method), like this:
public static void main(String...args){
try{
String userName=JOptionPane.showInputDialog(window,"Name: "," ",JOptionPane.PLAIN_MESSAGE);
try{
String serverName="localhost";
new ChatApplication(userName, serverName); //call to appropriate constructor
window.setTitle(userName);
}catch(IOException e){
System.err.println(e.toString());
}
}//main
But i'm still geting the same exception.
nameJulian
29-May-13 13:22pm
View
The exception occurs in the actionPerformed method of sendButton, on the line: output.println(messageToServer);
nameJulian
17-May-13 6:43am
View
I didn't assume...I just gave a solution in case it's a problem like mine. And I know there can be multiple reasons.
nameJulian
16-May-13 5:42am
View
I believe that your problem might be the GridLayout for your centerButtons panel. GridLayout tells the program, that the buttons can ocupy all the space they can aquire. Try using this:
JPanel centerButtons=new JPanel();
centerButtons.setLayout9(new BoxLayout(centerButtons,BoxLayout.Y_AXIS);
This is much like GridLayout(2,1). It display's the buttons vertical in the Panel.
nameJulian
13-May-13 8:27am
View
Well in my application, i'm creating the big thread when i click a button, and then the big thread creates a number of threads. ( and the number of how many threads need to be created is taken from an ArrayList.size() ).
nameJulian
8-May-13 7:35am
View
Indeed...very good remark Filipe Marques. Thank you.
nameJulian
6-May-13 5:46am
View
I have solved my problem. I stopped using timers and moved to using threads. The problem has been solved, but the idea that you gave me is good as well. Thank you.
nameJulian
3-May-13 9:46am
View
Does it matter if i use java.util.Timer or javax.swing.Timer?
I know that when the current timer stops, the second one must be scheduled from the first one ( from the firsts one run() method ). I know that using too many timers is bad, but i strongly need this. Thank you.
nameJulian
30-Apr-13 10:23am
View
I have thought of what you said about the timer to be taken completly into the loop. I have done so. The problem is the next thing: if i have in the array lets say, the number 4 and 8. At the moment, the code does something like this: it start counting, by the time it reaches 4, the program does something ( System.out.println("reached here") - in my code ), after that it continues counting until it reaches 8. Here again the program does something.
________________________________________________________________________________________
What i need is that after it reached 4, the timer stops, it takes the value 8 and starts counting again from 0 until it reaches 8.
________________________________________________________________________________________
Or better said, after the first timer reaches 4, it stops counting and the second timer starts counting until it reaches 8, then it stops.
Hope you understand.
nameJulian
30-Apr-13 6:55am
View
What i basically want is...for different time values, to send a character to COM port with RXTX library. Thank you for the opinion.
nameJulian
25-Apr-13 14:58pm
View
You're welcome.
Show More