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: Convert Application to JApplet

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Convert Application to JApplet

    I made an application and it works perfectly when I compile it in the command window with javac. Here is the code for that:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.AbstractButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class RegF extends JFrame {
    protected static JTextField textfield1, textfield2, textfield3, textfield4, textfield5;
    protected static JButton ok, cancel;
    protected static JLabel label1, label2, label3, label4, label5;
     
    public static void main(String args[]) {
     
    JFrame frame = new JFrame("Registration Form");
     
    GridBagLayout lay = new GridBagLayout();
    JPanel pane = new JPanel(lay);
    GridBagConstraints c = new GridBagConstraints();
     
    textfield1 = new JTextField(20);
    textfield2 = new JTextField(20);
    textfield3 = new JTextField(30);
    textfield4 = new JTextField(20);
    textfield5 = new JTextField(20);
     
     
    ok = new JButton("Ok");
    ok.setMnemonic(KeyEvent.VK_O);
     
    cancel = new JButton("Cancel");
    cancel.setMnemonic(KeyEvent.VK_C);
     
    ok.setToolTipText("Click if you you are ok with the your entries");
    cancel.setToolTipText("Click if would like to cancel");
     
    label1 = new JLabel("First Name");
    label2 = new JLabel("Last Name");
    label3 = new JLabel("Address");
    label4 = new JLabel("City");
    label5 = new JLabel("State");
     
     
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(textfield1, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 1;
    pane.add(textfield2, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 2;
    pane.add(textfield3, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 3;
    pane.add(textfield4, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 4;
    pane.add(textfield5, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(label1, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(label2, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 2;
    pane.add(label3, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 3;
    pane.add(label4, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 4;
    pane.add(label5, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 6;
    pane.add(ok, c);
     
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 2;
    c.gridy = 6;
    pane.add(cancel, c);
     
    frame.setDefaultCloseOperation(JFrame.EX…
    frame.getContentPane().add(pane);
    frame.pack();
    frame.setVisible(true);
    }
    }

    So that one compiles fine and the ok and cancel buttons aren't supposed to do anything. If anyone has any ideas for those that would be cool but I don't even need that so don't really worry about it. The main question here is how do I convert that to work in a browser window because I tried and I searched Google more than I like to say and I can't figure out how to get it to work.
    Thank you to anyone that can help I am just learning this stuff and it is driving me nuts!
    Last edited by helloworld922; May 11th, 2010 at 09:42 PM. Reason: Please use [code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert Application to JApplet

    I'd recommend going through Sun Java Lesson: Applets tutorials. They explain in great detail how to write applets.

    A short tutorial to get you started:

    The init() method basically replaces the constructor (put your initialization code here)

    Most applets over-ride the paint method, but I think in your case you shouldn't since you don't need to do any custom painting. Instead, simply get back the content pane of the applet and add your components as necessary to the content pane.

    Also, if you want your buttons to work, you need to add an ActionListener. This can either be it's own separate class that implements the ActionListener interface, or your Applet class can implement the ActionListener class.

    // sample ActionPerformed method which prints out the action command.
    // unfortunately, it prints out to the println stream, which you can't see directly from the web browser (you'll need to open the console, in Firefox go to [i]Tools/Java console[/i])
    actionPerformed(ActionEvent e)
    {
         System.out.println("The button pressed was: " + e.getActionCommand());
    }

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Convert Application to JApplet

    So then what would be classified as the constructor in the code of the program that I posted then? In other words where would I put the init()?

Similar Threads

  1. Replies: 0
    Last Post: April 17th, 2010, 08:29 AM
  2. Cannot update Jlabel in JApplet
    By rin in forum Java Applets
    Replies: 2
    Last Post: April 17th, 2010, 08:21 AM
  3. [SOLVED] [help] the application file (.jad) for application does not appear to be the ....
    By ordinarypeople in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2010, 03:50 AM
  4. JApplet containing JButton and a JLabel
    By JuneM in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 08:32 AM
  5. Replies: 0
    Last Post: December 3rd, 2009, 04:43 PM

Tags for this Thread