3 Replies - 818 Views - Last Post: 09 June 2010 - 06:09 PM Rate Topic: -----

#1 slumdawg   User is offline

  • New D.I.C Head

Reputation: -7
  • View blog
  • Posts: 11
  • Joined: 30-May 10

Java Swing

Posted 09 June 2010 - 05:34 PM

My goal is
When you click on JButton that has caption of Button1 - read the contents of JTextField and change the caption of other JButton (Button2) to the string value from JTextfield.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ButtonPanel extends JPanel
   implements ActionListener
{
    private JButton JButton2;
    private JButton JButton1;
    private JTextField textArea;
    public ButtonPanel()
    {
        JButton1 = new JButton("JButton1");
        JButton2 = new JButton("JButton2");
        String a = "Come on everybody, clap your hands!"; //random text
        setLayout (new FlowLayout(FlowLayout.LEADING));
        add(JButton2);
        add(JButton1);
        JButton1.addActionListener(this);
        JButton2.addActionListener(this);
        textArea = new JTextField(a, 40);
   }

   public void actionPerformed(ActionEvent evt)
   {
       Object source = evt.getSource();
       if (source == JButton1)
       {
            //
       }
       else if (source == JButton2)
       {
            //
       }
   }
}
class ButtonFrame extends JFrame
{
    public ButtonFrame()
    {
      setTitle("ButtonTest");
      setSize(500, 400);
      addWindowListener(new WindowAdapter()
         {  public void windowClosing(WindowEvent e)
            {  System.exit(0);
            }
         } );

      Container contentPane = getContentPane();
      contentPane.add(new ButtonPanel());
   }
}
public class HelloWorldFrame
{  public static void main(String[] args)
   {
       JFrame frame = new ButtonFrame();
       frame.show();
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Swing

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Java Swing

Posted 09 June 2010 - 05:40 PM

We are not a code writing service. We need to see some effort before we can really help you. Also, please describe any errors or problems with your good faith efforts.

Hint- take a look at the getText() and setText() methods for JTextField and JButton.
Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Swing

Posted 09 June 2010 - 05:48 PM

and don't forget to add() your textArea to the JPanel as you did correctly with your JButton
Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Java Swing

Posted 09 June 2010 - 06:09 PM

You may want to look at this thread as the person was doing what you are trying to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1