JButton

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijaykumarsharma
    New Member
    • Nov 2007
    • 12

    #1

    JButton

    Hi all

    I am having a problem with JButton.

    JButton b=new JButton("save") ;

    how can i get the name of the JButton b.

    Please give the anser for me
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vijaykumarsharm a
    Hi all

    I am having a problem with JButton.

    JButton b=new JButton("save") ;

    how can i get the name of the JButton b.

    Please give the anser for me
    The name is b.
    What can I say?

    Comment

    • vijaykumarsharma
      New Member
      • Nov 2007
      • 12

      #3
      Originally posted by r035198x
      The name is b.
      What can I say?
      I want the name of the button as "save".

      please give some idea.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by vijaykumarsharm a
        I want the name of the button as "save".

        please give some idea.
        Here "save" is usually called the text of the label. You get it using the getText() method. Read all about it in the specs for the button class.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          The ActionEvent can tell the source of the event which is object b (your button)
          after you have pressed it.

          kind regards,

          Jos

          Comment

          • vivhbti
            New Member
            • Nov 2007
            • 1

            #6
            Hi Vijay


            In Swing there is a facility of getActionComman d & setActionComman d.


            While defining button just add below line.
            JButton b=new JButton("save") ;
            b.setActionComm and("save");

            and when you are handling action performed...
            public void actionPerformed (ActionEvent e) {
            String nameOfButton = e.getActionComm and();

            ............
            ...........
            ............

            }


            Originally posted by vijaykumarsharm a
            Hi all

            I am having a problem with JButton.

            JButton b=new JButton("save") ;

            how can i get the name of the JButton b.

            Please give the anser for me

            Comment

            • heat84
              New Member
              • Nov 2007
              • 118

              #7
              Originally posted by vivhbti
              Hi Vijay


              In Swing there is a facility of getActionComman d & setActionComman d.


              While defining button just add below line.
              JButton b=new JButton("save") ;
              b.setActionComm and("save");

              and when you are handling action performed...
              public void actionPerformed (ActionEvent e) {
              String nameOfButton = e.getActionComm and();

              ............
              ...........
              ............

              }
              I dont think this code will return the name of the button as requested.

              Comment

              Working...