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 2 of 2

Thread: Creating an Applet program using JTextFields, JLabel, Jbutton.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Creating an Applet program using JTextFields, JLabel, Jbutton.

    Ok, here is another newbie in java. I want to create an Applet program. When the button “Show” is clicked, the information typed in the two JTextFields (if any), will be displayed on the JLabel. On the other hand, when the button “Clear” is clicked, the information typed in the two JTextField (if any), will be cleared.

    the complicated code(for newbie me) is right here:

    JLabel lblName = new JLabel("Name:");

    JLabel lblAge = new JLabel("Age:");

    textField = new JTextField();
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setColumns(10);

    JButton btnShow = new JButton("Show");
    btnShow.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("Max");
    textField_1.setText("25");
    }
    });

    JButton btnClear = new JButton("Clear");
    btnClear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("");
    textField_1.setText("");
    }


    Here my whole code using Eclipse:


    package applet.oum;

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.border.TitledBorder;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Toolkit;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.ImageIcon;

    public class Applet extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    Applet frame = new Applet();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the frame.
    */
    public Applet() {
    setIconImage(Toolkit.getDefaultToolkit().getImage( Applet.class.getResource("/buttons/img/KK.png")));
    setTitle("Applet for Me");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mntmExit.setIcon(new ImageIcon(Applet.class.getResource("/buttons/img/Snap4-16.png")));
    mntmExit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    dispose();
    }
    });
    mnFile.add(mntmExit);

    JMenu mnAbout = new JMenu("About");
    menuBar.add(mnAbout);

    JMenuItem mntmAboutThis = new JMenuItem("About This");
    mntmAboutThis.setIcon(new ImageIcon(Applet.class.getResource("/buttons/img/Snap6-16.png")));
    mnAbout.add(mntmAboutThis);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JPanel panelApplet = new JPanel();
    panelApplet.setBorder(new TitledBorder(null, "Applet", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
    gl_contentPane.createParallelGroup(Alignment.LEADI NG)
    .addComponent(panelApplet, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
    );
    gl_contentPane.setVerticalGroup(
    gl_contentPane.createParallelGroup(Alignment.LEADI NG)
    .addComponent(panelApplet, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
    );

    JLabel lblName = new JLabel("Name:");

    JLabel lblAge = new JLabel("Age:");

    textField = new JTextField();
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setColumns(10);

    JButton btnShow = new JButton("Show");
    btnShow.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("Max");
    textField_1.setText("25");
    }
    });

    JButton btnClear = new JButton("Clear");
    btnClear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("");
    textField_1.setText("");
    }
    });
    GroupLayout gl_panelApplet = new GroupLayout(panelApplet);
    gl_panelApplet.setHorizontalGroup(
    gl_panelApplet.createParallelGroup(Alignment.TRAIL ING)
    .addGroup(Alignment.LEADING, gl_panelApplet.createSequentialGroup()
    .addGap(74)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.TRAILING)
    .addComponent(lblName)
    .addComponent(lblAge))
    .addGap(18)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.LEADING, false)
    .addComponent(textField_1)
    .addComponent(textField, GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
    .addGap(69))
    .addGroup(gl_panelApplet.createSequentialGroup()
    .addContainerGap(112, Short.MAX_VALUE)
    .addComponent(btnShow)
    .addGap(62)
    .addComponent(btnClear)
    .addGap(122))
    );
    gl_panelApplet.setVerticalGroup(
    gl_panelApplet.createParallelGroup(Alignment.LEADI NG)
    .addGroup(gl_panelApplet.createSequentialGroup()
    .addGap(19)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
    .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(lblName))
    .addGap(18)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
    .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(lblAge))
    .addPreferredGap(ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
    .addComponent(btnClear)
    .addComponent(btnShow))
    .addGap(38))
    );
    panelApplet.setLayout(gl_panelApplet);
    contentPane.setLayout(gl_contentPane);
    }
    }

    Snap1.jpg

    any help would b appreciate.. TQ..

    There is no error about this code, it just that i want it to be like, when the button “Show” is clicked, the information typed in the two JTextFields (if any), will be displayed on the JLabel. On the other hand, when the button “Clear” is clicked, the information typed in the two JTextField (if any), will be cleared. The problem is, what ever names i enter will output name: Max, and age: 25. I want it to display different names and ages.
    Last edited by Maxly; March 10th, 2012 at 08:30 AM. Reason: Unclear question.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Creating an Applet program using JTextFields, JLabel, Jbutton.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Do you have any specific problems with the posted code?
    Copy and paste here the full text of the error messages.

Similar Threads

  1. problem of creating matrix with JButton or Panel
    By ms_ceng in forum AWT / Java Swing
    Replies: 3
    Last Post: December 16th, 2011, 04:53 PM
  2. Replies: 2
    Last Post: October 18th, 2011, 08:11 PM
  3. jbutton on applet
    By coder.freak in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 02:08 PM
  4. Creating a threaded applet
    By mjpam in forum Java Applets
    Replies: 22
    Last Post: August 11th, 2010, 07:37 PM
  5. JApplet containing JButton and a JLabel
    By JuneM in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 09:32 AM