Results 1 to 20 of 21
Thread: Listener/JTextField to Int
- 05-05-2010, 06:01 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
- 05-05-2010, 06:09 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-05-2010, 06:12 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You've got execution code floating around inside your class outside of a block (ie method).
- 05-05-2010, 07:22 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Solved problem
Last edited by michail; 05-08-2010 at 07:53 PM.
For a world of science and reason
- 05-06-2010, 08:56 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
listenportext isn't declared anywhere in that code.
- 05-06-2010, 10:43 AM #6
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
It is at the beginning!
private JTextField listenporttext;
listenportext = new JTextField("listen",6);
listenportext.addActionListener(handler);
I still can't get it to work. I have a print statement just after the Object source = event.getSource();. It recognises the action but then it does nothing.For a world of science and reason
- 05-06-2010, 10:46 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
If you're going to post code then please make sure it is at least meaningful.
Those three lines, if stuck in a class, wouldn't compile.
You need to show us where you are declaring the variable, where it is initialised (less important, but you never know) and the part of the code you have a problem. With all the brackets and relevant method declarations in the right place.
- 05-06-2010, 10:46 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-06-2010, 10:55 AM #9
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Ok, I will post the whole code.
Maybe I can understand it thenFor a world of science and reason
- 05-06-2010, 11:00 AM #10
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
This is it
The problem is, when I press listen or connect it doesn.t triger anything, so that means I can.t even test it to see if the rest of it works
Java Code:import java.io.EOFException; import java.io.IOException; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; public [B]class[/B] Server extends JFrame { private JButton send; private JButton close; private JButton save; private JButton clear; private JButton connect; private JButton listen; private JTextArea displayarea; private JTextField writearea; private JTextField listenporttext; private JTextField connectport; private JTextField connectIp; public final static int CONNECTED = 1; public final static int WAITING = 2; public final static int CONNECTING = 3; public final static int DISCONNECTED = 4; public static int connectionStatus; private Socket connection; private ServerSocket server; private ObjectOutputStream output; private ObjectInputStream input; private ConnectionHandler connections; private JTextField listenportext; [B]public[/B] Server() { super("Server"); JPanel container = new JPanel(new BorderLayout(3,3));//add to the container the components below //maybe put some colour JPanel topContainer = new JPanel(new GridLayout(2,1,2,2)); JPanel topbuttonBar = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5)); JPanel inputPanel = new JPanel(new BorderLayout(5,5)); inputPanel.setBackground(Color.RED); JPanel connections = new JPanel(new FlowLayout(FlowLayout.CENTER,3,3));//connections panel //JMenu Sth = new JMenu("sth"); /*Input outpout area */ listenportext = new JTextField("22222",6); connectport = new JTextField("22222",6); connectIp = new JTextField("localhost",6); writearea = new JTextField(); writearea.setEditable(true); JTextArea displayarea = new JTextArea(30,2); displayarea.setLineWrap(true); displayarea.setEditable(false); /*Buttons */ send = new JButton("send"); add(send); close = new JButton("close"); add(close); save = new JButton("save"); add(save); clear = new JButton("clear"); add(clear); connect = new JButton("connect"); add(connect); listen = new JButton("listen"); add(listen); container.setBorder(BorderFactory.createLineBorder(Color.green));//compound border? container.add(topContainer, BorderLayout.NORTH);//add topContainer on top topContainer.add(connections);//add the panel with the connections buttons topContainer.add(topbuttonBar);//add the panel with thr rest of the buttons topbuttonBar.add(close); topbuttonBar.add(save); topbuttonBar.add(clear); topbuttonBar.add(Box.createHorizontalStrut(50)); topbuttonBar.add(listen); topbuttonBar.add(listenportext); topbuttonBar.add(connect); topbuttonBar.add(connectIp); topbuttonBar.add(connectport); container.add(inputPanel, BorderLayout.SOUTH); container.add(new JScrollPane(displayarea)); inputPanel.add(writearea, BorderLayout.CENTER); inputPanel.add(send, BorderLayout.EAST); /*Action Handling */ ActionListener handler = new ActionsHandler(); send.addActionListener(handler); close.addActionListener(handler); save.addActionListener(handler); clear.addActionListener(handler); connect.addActionListener(handler); listen.addActionListener(handler); writearea.addActionListener(handler); listenportext.addActionListener(handler); connectport.addActionListener(handler); connectIp.addActionListener(handler); setContentPane(container); pack(); } private [B]class[/B] [B]ActionsHandler[/B] implements ActionListener { public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == listen) { String temp = listenportext.getText(); int port = Integer.parseInt(temp); connections = new ConnectionHandler(port); connections.start(); } else if (source == connect) { int port; String connectemp = connectport.getText(); port = Integer.parseInt(connectemp); connections = new ConnectionHandler(connectIp.getText(), port); connections.start(); } else if (source == close) { System.exit(0); } else if (source == send || source == writearea) { if (connection !=null && connectionStatus == CONNECTED) { connections.sendData(writearea.getText()); } } } } //type object private [B]class[/B] [B]ConnectionHandler[/B] extends Thread { private int port; private String remoteip; private ObjectOutputStream out = null; private ObjectInputStream in = null; private ServerSocket serversocket; private Socket socket = null; //First constructor if it.s a server ConnectionHandler(int port) { Server.this.connectionStatus = WAITING; this.port = port; System.out.println("Waiting on port "+port); //this.start(); } //Second constructor if it.s a client ConnectionHandler(String remoteip, int port) { Server.this.connectionStatus = CONNECTING; this.port = port; this.remoteip = remoteip; System.out.println("Connecting to "+remoteip+" "+port); } //thread has been started public void [B]run[/B]() { try { if (Server.this.connectionStatus == WAITING) { //server waiting for a connection System.out.println( "Setting up server socket on port " + port ); serversocket = new ServerSocket(port); System.out.println( "Waiting for connections..." ); connection = serversocket.accept(); } //client is connecting else if (Server.this.connectionStatus == CONNECTING) { socket = new Socket(InetAddress.getByName(remoteip), port); System.out.println("Accepted a connection from: "+InetAddress.getByName(remoteip)); System.out.println("Accepted a connection from: "+socket.getInetAddress()); //while loop to handle the messages } connectionEstablished(); while(connectionStatus == CONNECTED) { String dispatch = (String)in.readObject(); sendData(dispatch); } }catch (Exception e) { System.out.println( e ); } finally{} } private void [B]connectionEstablished[/B]() throws IOException {[B]//problem here //after connecting get the streams[/B] try{ out = new ObjectOutputStream(socket.getOutputStream());//auto flush out.flush(); in = new ObjectInputStream(socket.getInputStream()); }catch(NullPointerException s){s.printStackTrace();} connectionStatus = CONNECTED; } private void [B]sendData[/B](String message) { try { out.writeObject("Client>"); out.flush(); displayarea.append(message); } catch (IOException ioException) { System.out.print("Error writing"); } } }//end of connection handler public static void [B]main[/B](String[] args) { Server chat = new Server(); chat.setVisible(true); } }Last edited by michail; 05-08-2010 at 07:56 PM. Reason: One step further
For a world of science and reason
- 05-06-2010, 11:36 AM #11
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Java Code:/*Buttons */ /*JButton send = new JButton("send"); add(send); JButton close = new JButton("close"); add(close); JButton save = new JButton("save"); add(save); JButton clear = new JButton("clear"); add(clear); JButton connect = new JButton("connect"); add(connect); JButton listen = new JButton("listen"); add(listen);*/ /* you have already declared the buttons as instance level variables at top so the listeners were not getting attached to instance level vars, rather to the vars declared inside the constructor */ send = new JButton("send"); add(send); close = new JButton("close"); add(close); save = new JButton("save"); add(save); clear = new JButton("clear"); add(clear); connect = new JButton("connect"); add(connect); listen = new JButton("listen"); add(listen); /*buttons*/Swastik
- 05-06-2010, 11:45 AM #12
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Thank you very very much. I missed that detail. Have you got any suggestions on the other methods?
Thanks againFor a world of science and reason
- 05-06-2010, 12:10 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
OK, so what exactly is the problem?
What is the error you are getting, stack trace and all?
- 05-06-2010, 12:25 PM #14
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
At the moment I press connect and listen, it shows the input. when I get to the run(), it doesn.t recognise the variables in the code.(serversocket). I don't know even if it distinguishes between the constants WAITING and CONNECTING in the run() method
Have I grasped the general idea? I mean am i going in the right direction with the methods ?
ThanksFor a world of science and reason
- 05-06-2010, 02:03 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
What do you mean by "doesn't recognise the variables".
What exception are you getting?
- 05-06-2010, 05:29 PM #16
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Ok I got there in the end. I will post further
The problem was here---> connectionStatus == WAITING
it should have been Server.this.connectionStatus == WAITING
ThanksFor a world of science and reason
- 05-06-2010, 05:39 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Eh?
connectionStatus is a static in Server, so a direct reference, or Server.connectionStatus shouldn't make a difference.
Hang on.
Do you have multiple connections coming in here? All using the single static connectionStatus?
- 05-06-2010, 09:10 PM #18
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Hi again. I updated the code above.
I got the server running! When I try to connect I get---->>.Connecting to localhost 3333
java.net.ConnectException: Connection refused
What does that mean? Do I have to put an exception somewhere?
:confused:For a world of science and reason
- 05-08-2010, 07:36 PM #19
Member
- Join Date
- Dec 2009
- Posts
- 31
- Rep Power
- 0
Hi, I fixed the connection problem. It was a blocked port!!
When I compile and connect to the server the server comes up with this error
java.lang.NullPointerException
at Server$ConnectionHandler.connectionEstablished(Ser ver.java:244)
at Server$ConnectionHandler.run(Server.java:222)
Any suggestions on why the OutputStream is behaving like this
It should just get the streams, right?For a world of science and reason
- 05-08-2010, 07:53 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Tab Listener
By teckno101 in forum AWT / SwingReplies: 2Last Post: 09-29-2009, 09:40 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
[SOLVED] action listener and Jtextfield
By tOpach in forum AWT / SwingReplies: 4Last Post: 12-16-2008, 01:02 PM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-20-2008, 11:07 PM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-10-2008, 02:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks