button action problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #1

    button action problem

    Code:
    package zad41;
    
    
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.net.*;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class SimpleClient {
        public static void main(String[] args) throws IOException {
        	PhoneDirectory phonedirectory = new PhoneDirectory("numbers.txt");
        	String action=null;
        	JButton    search    = new JButton("Wyszukaj");
        	JButton    add    = new JButton("Dodaj");
        	JButton    replace    = new JButton("Zastap");
        	JButton    bye    = new JButton("Bye");
        	JTextField add_name     = new JTextField(20);
        	JTextField replace_with     = new JTextField(20);
        	JTextField output     = new JTextField(20);
        	JComboBox namesList1 = new JComboBox(phonedirectory.getNames());
        	JComboBox namesList2 = new JComboBox(phonedirectory.getNames());
        	Socket kkSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            
            
            
            JPanel pane = new JPanel();
            JLabel label1 = new JLabel("Wyszukaj");
            JLabel label2 = new JLabel("Dodaj");
            JLabel label3 = new JLabel("Zastap");
            pane.setBorder(BorderFactory.createTitledBorder("Client"));
            pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
            pane.add(label1); 
            pane.add(namesList1);
            pane.add(search);
            pane.add(label2);
            pane.add(add_name);
            pane.add(add);
            pane.add(label3);
            pane.add(replace_with);
            pane.add(namesList2);
            pane.add(replace);
            pane.add(output);
            output.setEditable(false);
            
              JFrame frame = new JFrame("Client");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //          /final Table table = new Table();
              frame.setContentPane(pane);
              frame.pack();
    //          /f.setLocationRelativeTo(null);
              frame.setVisible(true);
              
              
            try {
                kkSocket = new Socket("localhost", 4444);
                out = new PrintWriter(kkSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: localhost.");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: localhost.");
                System.exit(1);
            }
    
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String lastname;
    
                 
              search.addActionListener( new ActionListener() {
    	                public void actionPerformed(ActionEvent e) {
    	                	action="get";
    	                	lastname=(String)namesList1.getSelectedItem();
    	                }
    	              });
            
            
            
            
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye."))
                    break;
    		    
                ////fromUser = stdIn.readLine();
    	    if (action != null) {
                    System.out.println("Client: " + action);
                    out.println(action);
    	    }
            }
            
    
         
            
            
            
            
            
            out.close();
            in.close();
            stdIn.close();
            kkSocket.close();
        }
    }


    why eclipse highlights
    action="get";
    lastname=(Strin g)namesList1.ge tSelectedItem as an error
    and says to change action variable to final ?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      now eclipse wants me to make them static ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by oll3i
        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

        Comment

        Working...