Results 1 to 4 of 4
  1. #1
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default How to pu a sort of Global Variable?

    I want to modify a variable inside an event listener of a class and that variable to be available to the others methods of the class 2 ( the method that called the event listener usually throgh a button press )

    Cause in an ATM programm when i press "See Remaining Credit" in the event the database is red and the remaining credit passed into a variable int, which I will put it into a text label like

    Label Text2RemainingCredit = new Label(Amount+" Euro", Label.CENTER);//amount is the int variable

    Thank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    That information will be part of your GUI's model if your gui is build along a MVC (model view controller) pattern.

  3. #3
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    which information? the amount? What has it to do with the GUI?

    I dont understand what an MVC is but the label is simply inside a card of a cardlayout

    the layout is part of a class which is called through the private static void CreateAndShowGUI()

    How can i have a common variable for both the event listener ( where it will be given the amount value from the database ) and the central ATM class where is the entire card Layout, so i can put the Amount inside the label and output it to the Remaining Credit Window

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    MVC means Model-View-Controller. When you create a GUI program, often you will use several classes with some of the classes creating and displaying the GUI - the View part of MVC, some of the classes dedicated to the program logic (including database access) - the Model part of MVC, and some classes dedicated towards interaction between the two and the user - the Control part. And this is what I meant about this being part of your gui program's model, that the data that you want to have as a common variable will be part of the logic component of the program, and in the classes dedicated to the program logic. You can pass the information between classes by using method or constructor parameters.

    Now if your program is extremely simple, often components are combined, especially the controller and the view. Otherwise, there's no such thing as "global variables" in Java, but instead you may wish to keep your value from the database in your model and perhaps pass references to the model where needed.

    If you need more specific help, then you're probably going to have to post some code and more explanation of your problem.

Similar Threads

  1. Global variable not accessible
    By bolduc4 in forum New To Java
    Replies: 3
    Last Post: 05-10-2010, 07:51 AM
  2. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  3. How to undefine a Global Variable in JAVA?
    By ajaykushwaha in forum New To Java
    Replies: 13
    Last Post: 11-19-2008, 10:53 PM
  4. write a selection sort without having numerous variable?
    By seandingobat in forum New To Java
    Replies: 6
    Last Post: 10-28-2008, 02:33 PM
  5. Replies: 1
    Last Post: 08-06-2007, 02:17 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
  •