When "ENTER" button in value class clicked the show class wil be run. but nothing happend

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


/*<applet code ="value" width=200 height=200>
</applet>*/

public class value extends Applet {

     private JLabel lab1;
    private JButton btn1;

      public void init () {

        // Construct the TextFields
     this.lab1 = new JLabel("Press the button");
     this.btn1 = new JButton("ENTER");

     // add the button to the layout
     this.add(lab1);
     this.add(btn1);

     transfer tran=new transfer();
     btn1.addActionListener(tran);

      }

      private class transfer implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
                {
                    show sh = new show();
                    sh.setVisible(true);
                }

        }


}




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




public class show extends Applet {

     private JLabel lab2;
     private JButton btn2;
     private JTextField txt2;
     String s1,s2;

      public void init () {

     // Construct the TextFields
     this.lab2 = new JLabel("Enter a number");
     this.btn2 = new JButton("DOUBLE");
     this.txt2 = new JTextField(20);
     this.txt2.setEditable(true);

     // add the button to the layout
     this.add(lab2);
     this.add(txt2);
      this.add(btn2);

      summation sa = new summation(txt2);
     btn2.addActionListener(sa);
     this.txt2.addActionListener(sa);

     }
      private int checkInput(String inString) {

        StringBuffer tStringBuf;
        int flag=0; 
        tStringBuf=new StringBuffer(inString);
        //check each input character to see if it is a number
        for(int index=0; index<tStringBuf.length(); index++){
            char ch=tStringBuf.charAt(index);
            if( (ch<'0' || ch>'9') && (ch !='.')){
                flag=1;
                break;
            }
        }
        if(flag==1) return 0;
        //not a number input
        else return 1;  //input is right

    }

    class summation implements ActionListener {
         private JTextField txt3;

         public summation(JTextField txt3)
         {
            this.txt3=txt3;

         }

         public void actionPerformed(ActionEvent ae) {
            if(ae.getSource()==btn2)
        {
            s1=txt3.getText();
            int i = Integer.parseInt(s1);
            int result=i*2;
            s2 = new Integer(result).toString();
            txt3.setText(s2);

        }
         }

    }
}

Recommended Answers

All 6 Replies

Calling applet's is not possible in java Applet. You need to place the interface in the current applet and repaint the current interface....

Applets normally run in a browser and require their support to execute. If you want to create an instance of an applet and have it execute, you will have to provide all the support that a browser does. You probably don't want to do that.
You should redesign your program, say by rewriting the Show class to extend JPanel which you can the add to your applet using some layout manager or to a JFrame.

through the applet program can i call a JFrame class? is it possible

Yes, an applet should be able to create an instance of a JFrame class.

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


public class input extends JApplet {
    private JLabel lab1;
    private JButton btn1;


    public void init() {
        // TODO start asynchronous download of heavy resources
        lab1 = new JLabel();
        btn1 = new JButton();

        lab1.setText("Press the Button");
        btn1.setText("ENTER");

        this.add(lab1);
        this.add(btn1);

        //transfer tran=new transfer();
        btn1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(lab1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(btn1)))
                .addContainerGap(202, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(lab1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(btn1)
                .addContainerGap(141, Short.MAX_VALUE))
        );


    }


    private void btn1ActionPerformed(ActionEvent evt) {                                         
        // TODO add your handling code here:
        //Object obj = evt.getSource();
        show shw=new show();
        shw.setVisible(true);
        System.out.println("HELLO");
        //this.setVisible(false);

    } 

 }



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

    public class show extends JPanel {

        private JFrame converterFrame;
        String s1,s2,s3;

        private int checkInput(String inString) {

            StringBuffer tStringBuf;
            int flag=0; 
            tStringBuf=new StringBuffer(inString);
            //check each input character to see if it is a number
            for(int index=0; index<tStringBuf.length(); index++){
                char ch=tStringBuf.charAt(index);
                if( (ch<'0' || ch>'9') && (ch !='.')){
                    flag=1;
                    break;
                }
            }
            if(flag==1) return 0;
            //not a number input
            else return 1;  //input is right

        }

        private void doubleit(){
            if( (txt_fld.getText().length()!=0 && checkInput(txt_fld.getText()) ==0))
                {
                    JOptionPane.showMessageDialog(converterFrame,
                    "input is not a number", "Warning",
                    JOptionPane.WARNING_MESSAGE);
                return;
            }

        }

        /** Creates new form show */
        public show() {
            initComponents();
        }


        @SuppressWarnings("unchecked")

        private void initComponents() {

            jLabel1 = new javax.swing.JLabel();
            dou_btn = new javax.swing.JButton();
            txt_fld = new javax.swing.JTextField();

            setBackground(new java.awt.Color(204, 204, 0));

            jLabel1.setText("Double the Value");

            dou_btn.setBackground(new java.awt.Color(204, 204, 255));
            dou_btn.setForeground(new java.awt.Color(255, 0, 0));
            dou_btn.setText("Double");
            dou_btn.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    dou_btnActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(99, 99, 99)
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                    .addComponent(txt_fld, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(161, 161, 161)
                    .addComponent(dou_btn)
                    .addContainerGap(174, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(55, 55, 55)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txt_fld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(51, 51, 51)
                    .addComponent(dou_btn)
                    .addContainerGap(151, Short.MAX_VALUE))
            );
        }// </editor-fold>                        

        private void dou_btnActionPerformed(java.awt.event.ActionEvent evt) {                                        

            if(evt.getSource()==dou_btn)
            {
                s1=txt_fld.getText();
                int i = Integer.parseInt(s1);
                int result=i*2;
                s2 = new Integer(result).toString();
                txt_fld.setText(s2);

            }
        }                                       
    public static void shw() {
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new show().setVisible(true);
                }
            });
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton dou_btn;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JTextField txt_fld;
        // End of variables declaration                   
    }

As you all said an applet class call a panel or frame class. But here is the code what I write for that. There is no effect after clicking the button in input applet class. The ActionPerformed method work well but...can not call the panel class.

You extended JPanel, not JFrame. You can't display a JPanel except inside a top-level container eg JFrame.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.