Results 1 to 3 of 3
  1. #1
    instop is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Question about buttons in SWT

    Hi.
    I have a problem. I've created SWT button, and now i'm trying to understand how to change variable x by pressing on it.
    My code is:
    Java Code:
    public class Main{
         public static void main(String[] args) {
        	int x;
    
           Display display = new Display();
           Shell shell = new Shell(display);
    
           Button button = new Button(shell, SWT.NONE);
           button.addSelectionListener(new SelectionAdapter() {
           	public void widgetSelected(SelectionEvent e) {
                   x = 1;
                   System.out.println(x);
           	}
           });
           button.setBounds(69, 42, 75, 25);
           button.setText("123");
           shell.open();
           while (!shell.isDisposed()) {
              if (!display.readAndDispatch()) display.sleep();
           }
        }
    }
    But Eclipse says that x must be made final and as i make it final it says x can't be changed. How do i make button change x? Help me please.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Question about buttons in SWT

    Move it outside of the method and make it static.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    instop is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Radio buttons to open list of more radio buttons
    By mcconnelln89 in forum New To Java
    Replies: 1
    Last Post: 11-16-2013, 02:31 AM
  2. Set Buttons on top of each other
    By Taylor91 in forum AWT / Swing
    Replies: 4
    Last Post: 11-21-2012, 04:38 AM
  3. GUI buttons question...
    By glenrowan in forum New To Java
    Replies: 3
    Last Post: 01-18-2011, 05:23 PM
  4. Help with Buttons
    By wld4ubabay in forum New To Java
    Replies: 20
    Last Post: 05-17-2010, 08:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •