By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

button action problem

oll3i
100+
P: 679
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
Share this Question
Share on Google+
3 Replies


Expert 10K+
P: 11,448
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
100+
P: 679
now eclipse wants me to make them static ?
May 12 '07 #3

Expert 10K+
P: 11,448
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

Post your reply

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