Close

Results 1 to 6 of 6
  1. #1
    DF VIP Member OrangeJuicey's Avatar
    Join Date
    Aug 2006
    Location
    London
    Posts
    1,283
    Thanks
    1
    Thanked:        4
    Karma Level
    291

    Question Quick Java Question

    Okay first off, I've created a set of swing buttons. There are a lot of them and with names which refer to their position on the screen e.g a1-a9, b1-b9

    for (int i=1;i<9;i++)
    {
    String name;
    name = "a" + Integer.toString(i);
    name.addActionListener(this);

    }

    so obviously i want to add ActionListener to the buttons, but i have a problem. How do i get it to use the String within name as the button name, for example here it'd be a1-a9. Do i use a map or reflection?

    Secondly is there a way of creating an array of buttons. Obviously an array can only take primary types, my ultimate goal is to create a tiled grid for creating a simple connect 4 game. Instead of just creating each button individually creating them as an array using a for loop to increment button names.

    My first go at creating a java game!

  2. #2
    DF VIP Member
    BFG's Avatar
    Join Date
    Apr 2002
    Location
    Newcastle
    Posts
    3,685
    Thanks
    513
    Thanked:        138
    Karma Level
    497

    Default Re: Quick Java Question

    import java.awt.*;
    import java.awt.event.*;

    public class AL extends Frame implements WindowListener,ActionListener {
    TextField text = new TextField(20);
    Button b;
    private int numClicks = 0;

    public static void main(String[] args) {
    AL myWindow = new AL("My first window");
    myWindow.setSize(350,100);
    myWindow.setVisible(true);
    }

    public AL(String title) {

    super(title);
    setLayout(new FlowLayout());
    addWindowListener(this);
    b = new Button("Click me");
    add(b);
    add(text);
    b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
    numClicks++;
    text.setText("Button Clicked " + numClicks + " times");
    }

    public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
    }

    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}

    }


    http://java.sun.com/docs/books/tutor...nlistener.html

    decent page here to read, with examples.
    Last edited by BFG; 22nd November 2007 at 05:06 PM.


    Time Flies when you're having fun! A decade of DF - April 2002-2012.

  3. #3
    DF VIP Member OrangeJuicey's Avatar
    Join Date
    Aug 2006
    Location
    London
    Posts
    1,283
    Thanks
    1
    Thanked:        4
    Karma Level
    291

    Default Re: Quick Java Question

    Okay I think you've miss understood me, I can implement the action listener no problem. What i want is to get a variable name which is stored in a string out of the string.

    e.g

    JButton b1 = new JButton();

    String nameofButton = "b1";

    then i want to do

    nameofButton.addActionListener(this);

    but it won't let me since this is of type string not Jbutton b1.

  4. #4
    DF VIP Member Mobius's Avatar
    Join Date
    Jun 2001
    Location
    The Internet
    Posts
    170
    Thanks
    3
    Thanked:        9
    Karma Level
    287

    Default Re: Quick Java Question

    bung them in an array then itterate over the array instead
    Last edited by Mobius; 22nd November 2007 at 05:48 PM.

  5. #5
    DF VIP Member OrangeJuicey's Avatar
    Join Date
    Aug 2006
    Location
    London
    Posts
    1,283
    Thanks
    1
    Thanked:        4
    Karma Level
    291

    Default Re: Quick Java Question

    Ok, but you can't place JButtons in an array, it only accepts primitive types like Strings, Int, bool, floats, etc.. if you could provide an example of how to create an array of buttons that'd be great. I've got 2 books here and i've searched the net but making a tiled game board of buttons is proving harder than I first expected. If i figure it out i'll let people know as well incase your interested.

  6. #6
    DF VIP Member
    canardo's Avatar
    Join Date
    Jul 2001
    Location
    uk
    Posts
    840
    Thanks
    0
    Thanked:        0
    Karma Level
    323

    Default Re: Quick Java Question

    of course you can make an array of JButtons

    Code:
     
    
    JButtons[] myJButtons = new JButtons[20];
    
    for(int i=0; i < myJButtons.length; i++)
    {
          myJButtons[i] = new JButton("a"+i);
    }
    ps ""+i will achieve the same as Integer.toString(i);
    Last edited by canardo; 22nd November 2007 at 06:28 PM.

Similar Threads

  1. quick release keys
    By ghostman78 in forum Radio Decoding
    Replies: 2
    Last Post: 6th October 2002, 04:18 AM
  2. redirect question
    By Psychoschiz in forum Web Hosting & Domain Names
    Replies: 2
    Last Post: 26th September 2002, 04:09 PM
  3. Quick Saturn question
    By doughboy in forum Old Skool Gaming & Retro
    Replies: 1
    Last Post: 16th September 2002, 02:19 AM
  4. Quick questionon old consoles
    By bozza in forum Old Skool Gaming & Retro
    Replies: 3
    Last Post: 4th September 2002, 02:25 PM
  5. Where can I get microfilters cheap and quick???
    By furryboo in forum Internet Connections & VPNs
    Replies: 4
    Last Post: 28th August 2002, 02:38 PM

Social Networking Bookmarks

Posting Permissions

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