473,796 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

button action problem

oll3i
679 Contributor
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=(Strin g)namesList1.ge tSelectedItem as an error
and says to change action variable to final ?
May 11 '07 #1
3 1751
JosAH
11,448 Recognized Expert MVP
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 Contributor
now eclipse wants me to make them static ?
May 12 '07 #3
JosAH
11,448 Recognized Expert MVP
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
2295
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 to either a) generate numbers b) Prompts a user to locate a web page c) go to previous page in history list d) Loads next page in history list e) Promps the user for a URL and loads the web page in a new window f) and Re-Sizes the window. ...
15
2560
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 Column B Column C data 4 radio buttons more data .... ... ... .... ... ...
7
2780
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 pressed? thanks!!!
2
12491
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 the onClick command. It always commits the CGI action, and skip the URL redirect part. The example code is below <form action=mycgi.pl> <input type='submit' name=submit value='Submit' onClick="window.location.href='http://myserver.com'">...
4
2857
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 then submit the form... Could someone correct this lump of code for us pleaseeeeeeee ;o) <input name="Submit" type="button" class="EmployerPostJobForm" value=" Verify Job " id="mFormSubmit" onClick="if(confirm('You are about to verify this...
0
2289
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 below). I have set the button's "Triple State" value to Yes, and the default value to False. Here is the macro that I used for tglName (I assigned the macro to the button's On Click event): ...
7
16046
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 statement like as follows in the PageLoad: private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { //do something here } else {
8
23235
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 our application that will do the exact same thing as the Back button in the browser. Can anyone give me any suggestions on how to do this using VB .Net? I have Googled for "Create Back Button" and found stuff in JavaScript and HTML but nothing in...
4
2636
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 before the list is repopulated. For example, suppose the user selects 3 in the drop down list and 3 text fields are shown. If the user populates the 3 text fields with data and decides to change the drop down to a list of 4 or 5, how do I capture...
1
1818
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 comes to question page. I have about 20 questions. As soon the user login to the system, he should start answering. The system should remember the number of correct answers til the 20th question. I used session to hold the value of number of correct...
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.