473,382 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Using variable from aother method

24
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..

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Main{
  6.     public static void main(String[] args){
  7.         EventQueue.invokeLater(new Runnable(){
  8.            public void run(){
  9.                Menu frame=new Menu();
  10.                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.                frame.setVisible(true);
  12.            } 
  13.         });
  14.     }
  15. }
  16.  
  17. class Menu extends JFrame{
  18.     public Menu(){
  19.         setTitle("Let's Paint (pilihan mode)");
  20.         setSize(300,200);
  21.  
  22.         JButton titik=new JButton("Titik");
  23.         JButton garis=new JButton("Garis");
  24.         JButton lingkaran=new JButton("Lingkaran");
  25.         JButton ellipse=new JButton("Ellipse");
  26.  
  27.         buttonPanel=new JPanel();
  28.  
  29.         buttonPanel.add(titik); //node
  30.         buttonPanel.add(garis); //line
  31.         buttonPanel.add(lingkaran); //circle
  32.         buttonPanel.add(ellipse); //ellipse
  33.  
  34.         add(buttonPanel);
  35.  
  36.  
  37.  
  38.         Pilih actTitik=new Pilih(1);
  39.         Pilih actGaris=new Pilih(2);
  40.         Pilih actLingkaran=new Pilih(3);
  41.         Pilih actEllipse=new Pilih(4);
  42.  
  43.         titik.addActionListener(actTitik);
  44.         garis.addActionListener(actGaris);
  45.         lingkaran.addActionListener(actLingkaran);
  46.         ellipse.addActionListener(actEllipse);
  47.     }
  48.  
  49.     private class Pilih implements ActionListener{
  50.         public Pilih(int c){
  51.             int idx=c;   
  52.         }
  53.         public int setIndex(){
  54.             return idx;
  55.         }
  56.        public void actionPerformed(ActionEvent event){
  57.             //here's the problem
  58.            Titik.run();
  59.        }
  60.         private int idx;
  61.     }
  62.  
  63.     private JPanel buttonPanel;
  64. }        
  65.  
user will be given four choices in button, whether to create node,line,circle 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...
Sep 23 '08 #1
1 2026
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.

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..

Expand|Select|Wrap|Line Numbers
  1.  
  2.     private class Pilih implements ActionListener{
  3.         public Pilih(int c){
  4.                                    // int is redundant, it creates a local variable,
  5.             int idx=c;         // Class variable defined below is not changed
  6.         }
  7.        public void actionPerformed(ActionEvent event){
  8.             //here's the problem
  9.            Titik.run();        // no run() method defined in class
  10.        }
  11.         private int idx;     // variable never changed
  12.     }
  13.  
user will be given four choices in button, whether to create node,line,circle 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, getActionCommand() gives you where it is coming from. Hope this helps.
Btw, Welcome to the world of Java programming.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*; 
  2. import java.awt.event.*; 
  3. import javax.swing.*; 
  4.  
  5. public class PaintMenu extends JFrame {
  6.  
  7.     protected    Pilih    pilih[];
  8.  
  9.     public static void main (String[] args) {
  10.         PaintMenu paintMenu = new PaintMenu();
  11.         paintMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
  12.         paintMenu.pack();
  13.         paintMenu.setVisible(true);
  14.     }
  15.  
  16.     public PaintMenu () {
  17.         super ("Let's Paint (pilihan mode)");
  18.         // setSize(300,200); Let the Layout manager do this
  19.         addButtonsToContainer (getContentPane());
  20.     }
  21.  
  22.     private void addButtonsToContainer (Container cp) {
  23.         JPanel bp = new JPanel();
  24.         int n = buttNames.length;
  25.         pilih = new Pilih[n];
  26.         cp.setLayout (new GridLayout(1,n));
  27.         for (int i = 0 ; i < buttNames.length ; i++) {
  28.             JButton b = new JButton(buttNames[i]);
  29.             pilih[i] = new Pilih (i+1);
  30.             b.addActionListener (pilih[i]);
  31.             cp.add (b);
  32.     }}
  33.  
  34.     protected final static String[] buttNames = {
  35.             "Titik", "Garis", "Lingkaran", "Ellipse"};
  36.  
  37.     private class Pilih implements ActionListener { 
  38.         private    int    idx;
  39.  
  40.         public Pilih (int c) {
  41.             idx = c; 
  42.         }
  43.  
  44.         public void actionPerformed(ActionEvent event) {
  45.             pilihAction(event.getActionCommand());
  46.         }
  47.  
  48.         public void pilihAction (String cmd) {
  49.             JOptionPane.showMessageDialog (PaintMenu.this,
  50.             "Running Pilih, internal variable idx = "+ idx +
  51.             "\nresponding to the command " + cmd,
  52.             "Using inner class variable",
  53.                                                    JOptionPane.INFORMATION_MESSAGE);
  54.             // Do whatever else ...
  55. }}}
  56.  
Sep 23 '08 #2

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

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
0
by: Julien | last post by:
Hello! I created a VB.Net program that retrieves particular computers names from the Active Directory and that then logs off all those machines every night (via a Scheduled Task) using the...
5
by: Eddie | last post by:
I have an Access database that tracks project information. The database is very simple, except for 1 small aspect. Some background: 4 Tables - Project information, Employees, activity and pay...
1
by: SleepSheep | last post by:
two struct in c# public class A { int i1; byte i2; }
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please....
6
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) {...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
6
by: =?Utf-8?B?Unlhbg==?= | last post by:
I am trying to pass a value from a texbox in Form1 to a textbox in Form2 using properties in VS2005 but it doesn't work; please help (project is attached). Code for Game Class: using System;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.