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: How do I access this value?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How do I access this value?

    I need to write a program which has a JFrame with a number of JButtons on it. Each of the JButtons has an ActionListener. When any JButton is clicked, I need to somehow be able to use the text of the button that was clicked.

    I wrote a short and simple sample to illustrate my issue, although perhaps I'm approaching the problem with a poor solution in general. Perhaps there's a better way to set it up than my array of JButtons? Or something?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
     
    class SampleProblem implements ActionListener {
        public static void main(String[] args) {
     
            JFrame frame = new JFrame("Sample Program");
            frame.setLayout(new GridLayout(5,5));
     
            JButton[] buttonArray = new JButton[25];
            for (int i = 0; i < 25; i++) {
                buttonArray[i] = new JButton("foo");
                buttonArray[i].addActionListener(new SampleProblem());
                frame.add(buttonArray[i]);
            }
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
     
        }
     
        public void actionPerformed(ActionEvent e) {
     
            //THIS IS THE ISSUE:
            //System.out.print(e.getSource().getText());
        }
    }

    I tried "System.out.print(e.getSource().getText());" but that doesn't work. I seem to have misunderstood what "getSource" does...

    Any assistance is greatly appreciated.
    Last edited by austin.rose94; December 27th, 2011 at 11:44 PM.


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    313
    My Mood
    Bored
    Thanks
    46
    Thanked 86 Times in 72 Posts
    Blog Entries
    4

    Default Re: How do I access this value?

    You nearly have it.

    e.getSource() returns an object. Cast it into a JButton like so:

            JButton source = (JButton) e.getSource();
            System.out.println(source.getText());

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: How do I access this value?

    Also, you do not to create a.new instance of your class every time, you can reuse a class, including this. Ev
    button.addActionListener(this);

Similar Threads

  1. Access Method
    By CompScienceStudent1 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 21st, 2011, 08:56 AM
  2. UDDI access
    By kuzhali in forum Java Networking
    Replies: 1
    Last Post: April 26th, 2011, 05:29 AM
  3. Database access..
    By _lithium_ in forum JDBC & Databases
    Replies: 7
    Last Post: March 2nd, 2011, 09:32 PM
  4. access restriction??? wtf!?!
    By wolfgar in forum Java Theory & Questions
    Replies: 6
    Last Post: November 23rd, 2009, 06:27 AM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 03:11 AM