473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

button action problem

oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1. package zad41;
  2.  
  3.  
  4.  
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. import javax.swing.BorderFactory;
  11. import javax.swing.BoxLayout;
  12. import javax.swing.JButton;
  13. import javax.swing.JComboBox;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JTextArea;
  18. import javax.swing.JTextField;
  19.  
  20. public class SimpleClient {
  21.     public static void main(String[] args) throws IOException {
  22.         PhoneDirectory phonedirectory = new PhoneDirectory("numbers.txt");
  23.         String action=null;
  24.         JButton    search    = new JButton("Wyszukaj");
  25.         JButton    add    = new JButton("Dodaj");
  26.         JButton    replace    = new JButton("Zastap");
  27.         JButton    bye    = new JButton("Bye");
  28.         JTextField add_name     = new JTextField(20);
  29.         JTextField replace_with     = new JTextField(20);
  30.         JTextField output     = new JTextField(20);
  31.         JComboBox namesList1 = new JComboBox(phonedirectory.getNames());
  32.         JComboBox namesList2 = new JComboBox(phonedirectory.getNames());
  33.         Socket kkSocket = null;
  34.         PrintWriter out = null;
  35.         BufferedReader in = null;
  36.  
  37.  
  38.  
  39.  
  40.         JPanel pane = new JPanel();
  41.         JLabel label1 = new JLabel("Wyszukaj");
  42.         JLabel label2 = new JLabel("Dodaj");
  43.         JLabel label3 = new JLabel("Zastap");
  44.         pane.setBorder(BorderFactory.createTitledBorder("Client"));
  45.         pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
  46.         pane.add(label1); 
  47.         pane.add(namesList1);
  48.         pane.add(search);
  49.         pane.add(label2);
  50.         pane.add(add_name);
  51.         pane.add(add);
  52.         pane.add(label3);
  53.         pane.add(replace_with);
  54.         pane.add(namesList2);
  55.         pane.add(replace);
  56.         pane.add(output);
  57.         output.setEditable(false);
  58.  
  59.           JFrame frame = new JFrame("Client");
  60.           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61. //          /final Table table = new Table();
  62.           frame.setContentPane(pane);
  63.           frame.pack();
  64. //          /f.setLocationRelativeTo(null);
  65.           frame.setVisible(true);
  66.  
  67.  
  68.         try {
  69.             kkSocket = new Socket("localhost", 4444);
  70.             out = new PrintWriter(kkSocket.getOutputStream(), true);
  71.             in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
  72.         } catch (UnknownHostException e) {
  73.             System.err.println("Don't know about host: localhost.");
  74.             System.exit(1);
  75.         } catch (IOException e) {
  76.             System.err.println("Couldn't get I/O for the connection to: localhost.");
  77.             System.exit(1);
  78.         }
  79.  
  80.         BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  81.         String fromServer;
  82.         String lastname;
  83.  
  84.  
  85.           search.addActionListener( new ActionListener() {
  86.                     public void actionPerformed(ActionEvent e) {
  87.                         action="get";
  88.                         lastname=(String)namesList1.getSelectedItem();
  89.                     }
  90.                   });
  91.  
  92.  
  93.  
  94.  
  95.         while ((fromServer = in.readLine()) != null) {
  96.             System.out.println("Server: " + fromServer);
  97.             if (fromServer.equals("Bye."))
  98.                 break;
  99.  
  100.             ////fromUser = stdIn.readLine();
  101.         if (action != null) {
  102.                 System.out.println("Client: " + action);
  103.                 out.println(action);
  104.         }
  105.         }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.         out.close();
  115.         in.close();
  116.         stdIn.close();
  117.         kkSocket.close();
  118.     }
  119. }
  120.  
  121.  
  122.  


why eclipse highlights
action="get";
lastname=(String)namesList1.getSelectedItem as an error
and says to change action variable to final ?
May 11 '07 #1
3 1728
JosAH
11,448 Expert 8TB
The 'action' variable is a local variable. You instantiate your listener as a local
(anonymous) class. Those classes can only refer to local variables if and only
if they're final local variables.

This doesn't help you out because you want to change the value of that variable.
Make the 'action' variable a member variable of the outer class insead so the
anonymous local class can 'see' it and alter it.

kind regards,

Jos
May 11 '07 #2
oll3i
679 512MB
now eclipse wants me to make them static ?
May 12 '07 #3
JosAH
11,448 Expert 8TB
now eclipse wants me to make them static ?
That's not the way to do it; instead, in the body of the actionPerformed() method
call a method in the enclosing class and let that method do the work such as
changing member variables etc.

kind regards,

Jos
May 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: TrvlOrm | last post by:
Can any one please help me...I am new to JavaScript and I have been struggling with this code for days now and can't figure it out. I would like to get the Buttons to correspond with the action...
15
by: JR | last post by:
Hi. I hope someone out there who is more versed with JavaScript than I can help me with the following annoying problem. Here's the problem. I have a form with the following layout: Column A...
7
by: M | last post by:
i have a form which i would like to input different "action" url depending on the button that was clicked. is there a way that javascript can prefill a defined action based on the button...
2
by: Hongyu | last post by:
I am trying to implement a simple JavaScript of redirecting my window to a new URL upon clicking a submit button. This is an easy task except when I have to put an input type='submit' in front of...
4
by: @sh | last post by:
Can anyone help out here please, we have a button that when pressed will alert the user, should they cancel no action is taken, however should they confirm, the script will disable the button and...
0
by: Aravind | last post by:
Hi folks. I have a form, frmHistory, which has 3 toggle buttons (1 of which is tglName, which I will be using to demonstrate my problem). The buttons are used to sort the form (explanations...
7
by: Amadelle | last post by:
Hi all and thanks in advance, I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if...
8
by: Coleen | last post by:
Yes, I know why would I want to create a back button when there is one on the browser? Because that's what the users want! they want a "Previous" button that they can click from any web page in...
4
by: Greg Scharlemann | last post by:
I'm trying to setup a dyamic dropdown list that displays a number of text fields based on the selected number in the dropdown. The problem I am running into is capturing the data already entered...
1
by: confusedofphp | last post by:
Hi all, Im very new to php. Currently im creating an online exam system which uses php and mysql as the database. I have registration and login page which works pretty fine. I had problem when it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.