[java] Call japplet from jframe

I Designed JApplet class manually and I designed main class by using Netbean Builder which generate the code automatically by default the class will be extends JFrame. so how can I run the JApplet class from JFrame .
this JApplet code
the problem now is buttons does not appear
Updated

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable;  public class GalaxyTable2 extends JFrame{ //JApplet      private static final int PREF_W = 700;     private static final int PREF_H = 600;     String[] columnNames                     = {"Phone Name", "Brief Description", "Picture", "price",                         "Buy"};  // Create image icons     ImageIcon Image1 = new ImageIcon(                     getClass().getResource("s1.png"));     ImageIcon Image2 = new ImageIcon(                     getClass().getResource("s2.png"));     ImageIcon Image3 = new ImageIcon(                     getClass().getResource("s3.png"));     ImageIcon Image4 = new ImageIcon(                     getClass().getResource("s4.png"));     ImageIcon Image5 = new ImageIcon(                     getClass().getResource("note.png"));     ImageIcon Image6 = new ImageIcon(                     getClass().getResource("note2.png"));     ImageIcon Image7 = new ImageIcon(                     getClass().getResource("note3.png"));      Object[][] rowData = {         {"Galaxy S", "3G Support,CPU 1GHz",             Image1, 120, false},         {"Galaxy S II", "3G Support,CPU 1.2GHz",             Image2, 170, false},         {"Galaxy S III", "3G Support,CPU 1.4GHz",             Image3, 205, false},         {"Galaxy S4", "4G Support,CPU 1.6GHz",             Image4, 230, false},         {"Galaxy Note", "4G Support,CPU 1.4GHz",             Image5, 190, false},         {"Galaxy Note2 II", "4G Support,CPU 1.6GHz",             Image6, 190, false},         {"Galaxy Note 3", "4G Support,CPU 2.3GHz",             Image7, 260, false},};      MyTable ss = new MyTable(                     rowData, columnNames);      // Create a table     JTable jTable1 = new JTable(ss);      public GalaxyTable2() {     jTable1.setRowHeight(70);      add(new JScrollPane(jTable1),    BorderLayout.CENTER);    JFrame f = new JFrame();     this.setTitle("Galaxy Phones");    JButton button = new JButton("Home");   button.setSize(new Dimension(200, 500));   button.setLocation(400, 500);   f.getContentPane().add(button);   JButton button2 = new JButton("Confirm");   button2.setSize(new Dimension(100, 500));   button2.setLocation(100, 300);   // button2.addActionListener(null);   f.getContentPane().add(button2);   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     this.setSize(800, 700);   this.setLocation(300, 0);   }     @Override      public Dimension getPreferredSize() {         return new Dimension(PREF_W, PREF_H);     }      public void actionPerformed(ActionEvent e) {         new AMainFrame7().setVisible(true);     }      public static void main(String[] args) {           GalaxyTable2 b=new GalaxyTable2();         /*         JFrame f = new JFrame();          JButton button = new JButton("Home");         button.setSize(new Dimension(100, 50));         button.setLocation(400, 500);         f.getContentPane().add(button);         JButton button2 = new JButton("Confirm");         button2.setSize(new Dimension(100, 50));         button2.setLocation(200, 500);         button2.addActionListener(null);         f.getContentPane().add(button2);         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         f.setTitle("Galaxy Phones");         f.setSize(500, 500);         f.getContentPane().add(f, button2);         applet.init();         applet.start();*/         b.pack();          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();         b.setLocation((d.width - b.getSize().width) / 2,                         (d.height - b.getSize().height) / 2);         b.setVisible(true);      } } 

and this action code in main class

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                          new GalaxyTable().setVisible(true);  dispose();  }    

This question is related to java swing jbutton japplet

The answer is


First of all, Applets are designed to be run from within the context of a browser (or applet viewer), they're not really designed to be added into other containers.

Technically, you can add a applet to a frame like any other component, but personally, I wouldn't. The applet is expecting a lot more information to be available to it in order to allow it to work fully.

Instead, I would move all of the "application" content to a separate component, like a JPanel for example and simply move this between the applet or frame as required...

ps- You can use f.setLocationRelativeTo(null) to center the window on the screen ;)

Updated

You need to go back to basics. Unless you absolutely must have one, avoid applets until you understand the basics of Swing, case in point...

Within the constructor of GalzyTable2 you are doing...

JApplet app = new JApplet(); add(app); app.init(); app.start(); 

...Why are you adding another applet to an applet??

Case in point...

Within the main method, you are trying to add the instance of JFrame to itself...

f.getContentPane().add(f, button2); 

Instead, create yourself a class that extends from something like JPanel, add your UI logical to this, using compound components if required.

Then, add this panel to whatever top level container you need.

Take the time to read through Creating a GUI with Swing

Updated with example

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ActionEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;  public class GalaxyTable2 extends JPanel {      private static final int PREF_W = 700;     private static final int PREF_H = 600;      String[] columnNames                     = {"Phone Name", "Brief Description", "Picture", "price",                         "Buy"};  // Create image icons     ImageIcon Image1 = new ImageIcon(                     getClass().getResource("s1.png"));     ImageIcon Image2 = new ImageIcon(                     getClass().getResource("s2.png"));     ImageIcon Image3 = new ImageIcon(                     getClass().getResource("s3.png"));     ImageIcon Image4 = new ImageIcon(                     getClass().getResource("s4.png"));     ImageIcon Image5 = new ImageIcon(                     getClass().getResource("note.png"));     ImageIcon Image6 = new ImageIcon(                     getClass().getResource("note2.png"));     ImageIcon Image7 = new ImageIcon(                     getClass().getResource("note3.png"));      Object[][] rowData = {         {"Galaxy S", "3G Support,CPU 1GHz",             Image1, 120, false},         {"Galaxy S II", "3G Support,CPU 1.2GHz",             Image2, 170, false},         {"Galaxy S III", "3G Support,CPU 1.4GHz",             Image3, 205, false},         {"Galaxy S4", "4G Support,CPU 1.6GHz",             Image4, 230, false},         {"Galaxy Note", "4G Support,CPU 1.4GHz",             Image5, 190, false},         {"Galaxy Note2 II", "4G Support,CPU 1.6GHz",             Image6, 190, false},         {"Galaxy Note 3", "4G Support,CPU 2.3GHz",             Image7, 260, false},};      MyTable ss = new MyTable(                     rowData, columnNames);      // Create a table     JTable jTable1 = new JTable(ss);      public GalaxyTable2() {         jTable1.setRowHeight(70);          add(new JScrollPane(jTable1),                         BorderLayout.CENTER);          JPanel buttons = new JPanel();          JButton button = new JButton("Home");         buttons.add(button);         JButton button2 = new JButton("Confirm");         buttons.add(button2);          add(buttons, BorderLayout.SOUTH);     }      @Override      public Dimension getPreferredSize() {         return new Dimension(PREF_W, PREF_H);     }      public void actionPerformed(ActionEvent e) {         new AMainFrame7().setVisible(true);     }      public static void main(String[] args) {          EventQueue.invokeLater(new Runnable() {             @Override             public void run() {                 try {                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {                     ex.printStackTrace();                 }                  JFrame frame = new JFrame("Testing");                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 frame.add(new GalaxyTable2());                 frame.pack();                 frame.setLocationRelativeTo(null);                 frame.setVisible(true);             }         });     } } 

You also seem to have a lack of understanding about how to use layout managers.

Take the time to read through Creating a GUI with Swing and Laying components out in a container


Questions with java tag:

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error Instantiating a generic type When to create variables (memory management) java doesn't run if structure inside of onclick listener String method cannot be found in a main class method Are all Spring Framework Java Configuration injection examples buggy? Calling another method java GUI I need to know how to get my program to output the word i typed in and also the new rearranged word using a 2D array Java and unlimited decimal places? Read input from a JOptionPane.showInputDialog box Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable Two Page Login with Spring Security 3.2.x Hadoop MapReduce: Strange Result when Storing Previous Value in Memory in a Reduce Class (Java) Got a NumberFormatException while trying to parse a text file for objects Best way for storing Java application name and version properties Call japplet from jframe FragmentActivity to Fragment Comparing two joda DateTime instances Maven dependencies are failing with a 501 error IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Why am I getting Unknown error in line 1 of pom.xml? Gradle: Could not determine java version from '11.0.2' Error: Java: invalid target release: 11 - IntelliJ IDEA Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Why is 2 * (i * i) faster than 2 * i * i in Java? must declare a named package eclipse because this compilation unit is associated to the named module How do I install Java on Mac OSX allowing version switching? How to install JDK 11 under Ubuntu? Java 11 package javax.xml.bind does not exist IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Difference between OpenJDK and Adoptium/AdoptOpenJDK OpenJDK8 for windows How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Find the smallest positive integer that does not occur in a given sequence Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 How to uninstall Eclipse? Failed to resolve: com.google.firebase:firebase-core:16.0.1 How to resolve Unable to load authentication plugin 'caching_sha2_password' issue

Questions with swing tag:

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background JFrame background image JFrame: How to disable window resizing? Adding image to JFrame Adding items to a JComboBox How to make a JFrame button open another JFrame class in Netbeans? Set Icon Image in Java Java - Check if JTextField is empty or not Swing vs JavaFx for desktop applications Resize a picture to fit a JLabel Save file/open file dialog box, using Swing & Netbeans GUI editor How to layout multiple panels on a jFrame? (java) How does paintComponent work? Execute an action when an item on the combobox is selected How to set the height and the width of a textfield in Java? DTO and DAO concepts and MVC getting integer values from textfield Allowing the "Enter" key to press the submit button, as opposed to only using MouseClick How to add text to JFrame? add controls vertically instead of horizontally using flow layout JFrame.dispose() vs System.exit() JPanel vs JFrame in Java JOptionPane - input dialog box program Get combobox value in Java swing Triangle Draw Method Display calendar to pick a date in java JFrame in full screen Java Restricting JTextField input to Integers Open a link in browser with java button? Most simple code to populate JTable from ResultSet Adding JPanel to JFrame Java :Add scroll into text area JPanel setBackground(Color.BLACK) does nothing Limiting the number of characters in a JTextField JTable - Selected Row click event JCheckbox - ActionListener and ItemListener? "Could not find the main class" error when running jar exported by Eclipse Drawing in Java using Canvas Java swing application, close one window and open another when button is clicked The Use of Multiple JFrames: Good or Bad Practice? How to position the form in the center screen?

Questions with jbutton tag:

Call japplet from jframe How can I check that JButton is pressed? If the isEnable() is not work? Java - Check if JTextField is empty or not how to create a window with two buttons that will open a new window How to close a GUI when I push a JButton? How to Disable GUI Button in Java How to add action listener that listens to multiple buttons Swing/Java: How to use the getText and setText string properly How to clear the JTextField by clicking JButton How do I add an image to a JButton How to set background color of a button in Java GUI? How to place a JButton at a desired location in a JFrame using Java How to disable javax.swing.JButton in java? How do you add an ActionListener onto a JButton in Java

Questions with japplet tag:

Call japplet from jframe Difference between JPanel, JFrame, JComponent, and JApplet