|
This is a repost of code for project work[^], which has already received some suggestions. If you want further help then please edit this question and put your code between <pre> tags for readability. Also, please explain what problems you are having with your code.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
|
First lets give some context, I've just created a client and server but they don't work without using console So I've decided to create a GUI on top they'll communicate though the Stack, but that's all for a later date, right now I get a error on the Even listener which says that the JTextField is empty upon .getText() even though it's not. I've looked though it, but since I'm quite new to the whole Swing field, it's a challenge for me to fix the problem. It really is a simple GUI.
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener{
Stack<String> incoming = null;
Stack<String> outgoing = null;
JTextField input;
JTextArea output;
Button button;
public GUI(){
setTitle("Satire v1.0.1");
setSize(320,360);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
output = new JTextArea();
output.setBounds(0, 0, 300, 300);
output.setEditable(false);
input = new JTextField();
input.setBounds(0, 300, 260, 20);
button = new Button("SEND");
button.setBounds(250, 300, 50, 20);
button.addActionListener(this);
getContentPane().add(panel);
panel.add(button);
panel.add(input);
panel.add(output);
}
public void actionPerformed(ActionEvent e) {
GUI gui = new GUI();
try{
String out = gui.input.getText();
gui.input.selectAll();
outgoing.add(out);
}catch(NullPointerException e3){}
gui.output.append(outgoing.pop());
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run(){
GUI cGUI = new GUI();
cGUI.setVisible(true);
}
});
}
}
|
|
|
|
|
You are new to object oriented programming as well, arent you?
Instead of this:
public void actionPerformed(ActionEvent e) {
GUI gui = new GUI();
try{
String out = gui.input.getText();
gui.input.selectAll();
outgoing.add(out);
}catch(NullPointerException e3){}
gui.output.append(outgoing.pop());
}
Use this:
public void actionPerformed(ActionEvent e) {
try{
String out = input.getText();
input.selectAll();
outgoing.add(out);
}catch(NullPointerException e3){}
output.append(outgoing.pop());
}
|
|
|
|
|
I have made the edit, yes I'm new, but learning fast, a lot faster than expected.
though the edit has been made, there was no change to the error. What I don't understand is that the field isn't empty D:
|
|
|
|
|
There you go. This time I compiled/tested it, contained another big mistake. Why have you used those stacks? You should check the error messages as well in the console output!!! You program threw exceptions!
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener{
JTextField input;
JTextArea output;
Button button;
public GUI(){
setTitle("Satire v1.0.1");
setSize(320,360);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
output = new JTextArea();
output.setBounds(0, 0, 300, 300);
output.setEditable(false);
input = new JTextField();
input.setBounds(0, 300, 260, 20);
button = new Button("SEND");
button.setBounds(250, 300, 50, 20);
button.addActionListener(this);
getContentPane().add(panel);
panel.add(button);
panel.add(input);
panel.add(output);
}
public void actionPerformed(ActionEvent e) {
try{
String out = input.getText();
output.append(input.getText());
input.selectAll();
input.requestFocusInWindow();
}catch(NullPointerException e3){}
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run(){
GUI cGUI = new GUI();
cGUI.setVisible(true);
}
});
}
}
|
|
|
|
|
the purpose of the stack was for when I implements it into my chat client I'll have something to send it by, without changing the code around too much. I personally don't see how it caused an error.
|
|
|
|
|
It caused an error because in java every object is used by reference. You had there a null reference to a stack object, actually you haven't created your stack object and when you tried to use it that was a NullPointerException!
|
|
|
|
|
Meaning i should of put Stack<string> incoming = new Stack<string>();
|
|
|
|
|
Yep. Common mistake when starting java. In java only the primitive types are self contained, everything else is just a null or uninitialized reference without initialization.
|
|
|
|
|
Hopefully you could help me with this one, I've found the problem (Thanks to the breakpoints) My main created a thread1 and then thread1 created thread 2, both of which contain a while(true) (Then just sit and wait for input and output) but when thread1 creates thread2 it doesn't go past the line which creates thread2 which Isn't what I thought would happen... I thought Thread1 would create thread2 then proceeds to the loop for user input.
|
|
|
|
|
You are right that should happen.
|
|
|
|
|
I see no error but to call a implements Runnable I use the line,
(new Thread(new accept(br))).start(); if that helps
|
|
|
|
|
Not to be rude but maybe you should learn the basics first, correct threading is more or less an advanced topic. Actually the java tutorials put together by sun is exceptionally good: http://docs.oracle.com/javase/tutorial/[^]
There is a paragraph on that page with "Trails Covering the Basics" header, you should read through that carefully, you will find topics about threading in the "Essential Java Classes" part, but read it from the beginning to the end without skipping. After this basic tutorial you won't ask questions like this.
|
|
|
|
|
Any special features and tricks to Eclipse Debugger, I've hit an error while fusing it to the client. the server gets the connection but all input is just added to the JTextArea without being sent to the server even though it's told to send. I'd prefer to fix this one so I don't have to spam these forums.
|
|
|
|
|
Start using a good java IDE. Eclipse is one of the best free IDEs out there, choosing that you can only win. Download it and learn its basic features (how to set up a project and how to debug in it).
Advice regarding your program: Hook the input of your textbox and perform the "send text" action when the user presses enter in the textfield.
When sending the text it might be a better idea to clear the textfield instead of selecting its contents.
|
|
|
|
|
First of all you didn't set text in the JTextField and also you've not even enabled the user to write into the JTextField.
Because, setEditable(false); means the user cannot edit the text in the text field and also there was no text in the Textfield.
And also you do not need to create object for the class GUI in actionPerformed() because this method was written in the same class only. This means that you are performing action on the object 'g' which you've created.
You can directly access them, there is no need of creating the object.
|
|
|
|
|
Hi,
I need help to set up the SSL in Mutual Authentication.
Can any body help in this.
Regards,
Balu
Singapore
|
|
|
|
|
balarayala wrote: I need help You could start here[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Thank you for your reply..
I have IIS Server and Linux Client. I am having some trouble in setup.
If you have any idea on mutual setup. can you help me.
|
|
|
|
|
balarayala wrote: If you have any idea on mutual setup. Sorry no, did you check the links I gave you, the third one down contains lots of good details.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I guess you downvoted me because I did not fix your 'problem'. Given the amount of information you have provided I don't think many people could answer your question.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Richard MacCutchan wrote: guess you downvoted me because I did not fix your 'problem'
Countered with a 5 because from what I saw with google, it would point him in a good direction
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Thanks. It's amazing how many questioners use downvotes rather than actually doing some work for themselves.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
You provided good information to me.
Richard dont mistaken me, by mistake click that voting because i am new to this blog
|
|
|
|