0

I am new to java programming and I wish to know how can I call jframe F1 from applet code which is below:

public void actionPerformed(ActionEvent ae) {

        str=ae.getActionCommand();
        if(str=="SUBMIT")
        {
            if(type.getSelectedItem().toString()=="EMPLOYEE")
            {
                //if(t1.getText().equals("E101") && t2.getText().equals("123"))
                {
                    F1 f1=new F1();
                }
            }
        }
}

//Here is the Jframe code:

class F1 extends JFrame{

    JButton submit;

    public F1()
    {
        setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents(); 
    }

    private void initComponents() 
    {
        submit=new JButton("SUBMIT");
        this.setVisible(true);
        this.getContentPane().setLayout(new BorderLayout());
        this.setContentPane(new JLabel(new ImageIcon("f1bge.jpg")));
        this.getContentPane().setLayout(null);

        this.getContentPane().add(submit);
        submit.setBounds(860, 150, 73, 23);

    }

}

I am trying to execute it through applet viewer but it results in an empty frame. Please guide.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Problems: 1) The JFrame is empty because you call `setVisible(true)` before adding all components to it. Call this ***last*** after all has been added. 2) you're using == to check for String equivalence rather than use either the equals(...) or the equalsIgnoreCase(...) method. Understand that == checks if the two [i]objects[/i] are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Apr 10 '16 at 12:43
  • 1
    3) You shouldn't be displaying a secondary JFrame. If you need a dependent window, use a JDialog, or swap views with a CardLayout. Please see: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636). – Hovercraft Full Of Eels Apr 10 '16 at 12:44
  • 4) You're using null layouts and `setBounds(...)`. While null layouts and `setBounds()` might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. – Hovercraft Full Of Eels Apr 10 '16 at 12:45
  • 1
    5) Applets, including JApplets are considered a dead technology and should be studiously avoided, unless you're forced to use these for an academic requirement. Browsers are not supporting them, and neither is Oracle. – Hovercraft Full Of Eels Apr 10 '16 at 12:46
  • I've tried calling setVisible(true) after adding all the components, but it resulted in disapperance of jframe. – Prateekgcs Apr 10 '16 at 12:58
  • I've tried the same using netbeans which included this function in the main method java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new EMP().setVisible(true); } }); – Prateekgcs Apr 10 '16 at 13:06
  • 1
    `setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` This frame cannot be run from an applet, even if the applet has full permissions. In addition to point 5 of @HovercraftFullOfEels : See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). – Andrew Thompson Apr 10 '16 at 13:09
  • 1
    `this.setContentPane(new JLabel(new ImageIcon("f1bge.jpg")));` Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. This is even more important for applets or anything launched from them. – Andrew Thompson Apr 10 '16 at 13:12

0 Answers0