Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Components that interact with eachother

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Components that interact with eachother

    i've been programming a level editor for the past few days, and have come across something that doesn't seem right to me. i have a JFrame on which i instantiate two JPanels. One as a statusbar, which displays the selected image, and another which holds tiles. Each tile has a corresponding number. i have an ActionListener which sees which tile the user clicks on and then proceeds to set the statusbars' image. right now i do this by calling
            MapEditor.statusBar.setImage(cTile,iCol);
    MapEditor being the JFrame and statusBar being the statusbar that is instantiated in the MapEditor class.
    this seems like really bad code to me yet i can not find a better way to do this.
    any tips would help thanks.
    Last edited by Flamespewer; February 25th, 2010 at 06:33 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Components that interact with eachother

    I'm guessing MapEditor is a static reference to the class name?

    A simple way to avoid any problems with a second MapEditor object being created is to move your statusBar variable from static to instanced. Then, pass the reference to the statusBar panel to your ActionListener's constructor and have your ActionListener remember that reference. Then simply make the change on that object and it will be updated in the correct statusBar (so long as you didn't create a new statusBar without updating your ActionListener)

    public class MyActionListener implements ActionListener
    {
         private JPanel statusBar;
         MyActionListener(JPanel statusBar)
         {
              this.statusBar = statusBar;
         }
     
         public void actionPerformed(ActionEvent e)
         {
              // your code for figuring out what to perform
              this.statusBar.setImage(cTile,iCol);
         }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Flamespewer (February 25th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Components that interact with eachother

    thank you that solved my problem perfectly

Similar Threads

  1. Re-Initialization of FrameView app components??
    By DarkJoy in forum Java Networking
    Replies: 2
    Last Post: November 13th, 2009, 12:19 AM
  2. How can i make the components resizable?
    By ces_31 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 1st, 2009, 10:46 AM
  3. Replies: 0
    Last Post: September 2nd, 2009, 08:50 AM
  4. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM
  5. Change JFrame components problem
    By bruno88 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 30th, 2009, 01:25 PM