Using variable from aother method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ragonz
    New Member
    • Jul 2008
    • 24

    Using variable from aother method

    Hi, i'm kind of newbie in java programming. Now, i'm working on my task, n i hav a problem in using variable from other method. Here goes the code..

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Main{
        public static void main(String[] args){
            EventQueue.invokeLater(new Runnable(){
               public void run(){
                   Menu frame=new Menu();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setVisible(true);
               } 
            });
        }
    }
    
    class Menu extends JFrame{
        public Menu(){
            setTitle("Let's Paint (pilihan mode)");
            setSize(300,200);
            
            JButton titik=new JButton("Titik");
            JButton garis=new JButton("Garis");
            JButton lingkaran=new JButton("Lingkaran");
            JButton ellipse=new JButton("Ellipse");
            
            buttonPanel=new JPanel();
            
            buttonPanel.add(titik); //node
            buttonPanel.add(garis); //line
            buttonPanel.add(lingkaran); //circle
            buttonPanel.add(ellipse); //ellipse
            
            add(buttonPanel);
            
    
    
            Pilih actTitik=new Pilih(1);
            Pilih actGaris=new Pilih(2);
            Pilih actLingkaran=new Pilih(3);
            Pilih actEllipse=new Pilih(4);
            
            titik.addActionListener(actTitik);
            garis.addActionListener(actGaris);
            lingkaran.addActionListener(actLingkaran);
            ellipse.addActionListener(actEllipse);
        }
       
        private class Pilih implements ActionListener{
            public Pilih(int c){
                int idx=c;   
            }
            public int setIndex(){
                return idx;
            }
           public void actionPerformed(ActionEvent event){
                //here's the problem
               Titik.run();
           }
            private int idx;
        }
        
        private JPanel buttonPanel;
    }
    user will be given four choices in button, whether to create node,line,circl e or ellipse. what i want is that when the user click the node button, it goes to Titik.java and so does the other. but i don't know what to do. i wonder how to use (idx varable) from the constructor so that i can use switch condition in the actionPerformed method.

    thx in advance...
  • ManidipSengupta
    New Member
    • Sep 2008
    • 4

    #2
    Hi ragonz, I am no expert, but will try to answer your question, along with a few suggestions. The suggestions are my honest opinion only.

    Originally posted by ragonz
    Hi, i'm kind of newbie in java programming. Now, i'm working on my task, n i hav a problem in using variable from other method. Here goes the code..

    Code:
        
        private class Pilih implements ActionListener{
            public Pilih(int c){
                                       [B]// int is redundant, it creates a local variable,[/B]
                int idx=c;         [B]// Class variable defined below is not changed[/B]
            }
           public void actionPerformed(ActionEvent event){
                //here's the problem
               Titik.run();        [B]// no run() method defined in class[/B]
           }
            private int idx;     [B]// variable never changed[/B]
        }
    user will be given four choices in button, whether to create node,line,circl e or ellipse. what i want is that when the user click the node button, it goes to Titik.java and so does the other. but i don't know what to do. i wonder how to use (idx varable) from the constructor so that i can use switch condition in the actionPerformed method.

    thx in advance...
    Suggestions:
    1. Do not use function/method names like run() , it has special meaning in threads.
    2. Try to design the class in an object oriented fashion - do not declare arbitrary inner classes if not necessary
    3. Do not use standard awt class names like Menu, or methods like paint(), it will create a lot of confusion.

    From your problem description, I created the following class - feel free to modify, discard, or continue from here. The inner class, you see, is not really required here - you could implement the ActionListener interface in the top level, getActionComman d() gives you where it is coming from. Hope this helps.
    Btw, Welcome to the world of Java programming.

    Code:
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
      
    public class PaintMenu extends JFrame {
    
    	protected	Pilih	pilih[];
    
    	public static void main (String[] args) {
    		PaintMenu paintMenu = new PaintMenu();
    		paintMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		paintMenu.pack();
    		paintMenu.setVisible(true);
    	}
    
    	public PaintMenu () {
    		super ("Let's Paint (pilihan mode)");
    		// setSize(300,200); Let the Layout manager do this
    		addButtonsToContainer (getContentPane());
    	}
    
    	private void addButtonsToContainer (Container cp) {
    		JPanel bp = new JPanel();
    		int n = buttNames.length;
    		pilih = new Pilih[n];
    		cp.setLayout (new GridLayout(1,n));
    		for (int i = 0 ; i < buttNames.length ; i++) {
    			JButton b = new JButton(buttNames[i]);
    			pilih[i] = new Pilih (i+1);
    			b.addActionListener (pilih[i]);
    			cp.add (b);
    	}}
    
    	protected final static String[] buttNames = {
    			"Titik", "Garis", "Lingkaran", "Ellipse"};
      
    	private class Pilih implements ActionListener { 
    		private	int	idx;
    
    		public Pilih (int c) {
    			idx = c; 
    		}
    
    		public void actionPerformed(ActionEvent event) {
    			pilihAction(event.getActionCommand());
    		}
    
    		public void pilihAction (String cmd) {
    			JOptionPane.showMessageDialog (PaintMenu.this,
    			"Running Pilih, internal variable idx = "+ idx +
    			"\nresponding to the command " + cmd,
    			"Using inner class variable",
                                                       JOptionPane.INFORMATION_MESSAGE);
    			// Do whatever else ...
    }}}

    Comment

    Working...