Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:24:29+00:00 2026-06-14T14:24:29+00:00

* (Legend) Blue-mainpanel, Red-upanel, Black-ulpanel, Green-urpanel * So I have the same code for

  • 0

enter image description here
*

(Legend) Blue-mainpanel, Red-upanel, Black-ulpanel, Green-urpanel

*
So I have the same code for both the Applet and the JFrame. As you can see, with the `setsize() as same, both of them produce different results. I cannot figure out how to make my Applet behave the same way.

    import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Test2 extends Applet {

    JPanel mainpanel = new JPanel();
    JPanel upanel = new JPanel();
    JPanel dpanel = new JPanel();
    JPanel ulpanel = new JPanel();
    JPanel urpanel = new JPanel(); 
    JPanel dlpanel = new JPanel();
    JPanel drpanel = new JPanel();

    JLabel l1 = new JLabel("Label 1");
    JLabel l2 = new JLabel("Label 2");
    JLabel l3 = new JLabel("Label 3");
    JLabel l4 = new JLabel("Label 4");

    JTextField tb1 = new JTextField();
    JTextField tb2 = new JTextField();
    JTextField tb3 = new JTextField();

    JTextArea ta1 = new JTextArea();

    public void init() {

        mainpanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
        ulpanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        urpanel.setBorder(BorderFactory.createLineBorder(Color.green, 2));
        upanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));
        mainpanel.setLayout(new GridLayout(1, 1));
        upanel.setLayout(new GridLayout(1, 2));
        urpanel.setLayout(new BorderLayout());
        urpanel.add(ta1,BorderLayout.CENTER);
        ulpanel.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.ipadx = 2;
        g.ipady = 2;
        g.insets = new Insets(2, 2, 2, 2);
//      g.anchor = GridBagConstraints.EAST;
        g.gridx = 0; g.gridy = 0;
        ulpanel.add(l1, g);
        g.gridx = 0; g.gridy = 1;
        ulpanel.add(l2, g);
        g.gridx = 2; g.gridy = 1;
        ulpanel.add(l3, g);
        g.gridx = 0; g.gridy = 2;
        ulpanel.add(l4, g);

        g.gridwidth = GridBagConstraints.REMAINDER;
        g.fill = GridBagConstraints.HORIZONTAL;
//      g.anchor = GridBagConstraints.WEST;
        g.weightx = 1;
        g.gridx = 1; g.gridy = 0;
        ulpanel.add(tb1, g);
        g.gridwidth = 1;
        g.gridx = 1; g.gridy = 1;
        ulpanel.add(tb2,g);
        g.gridx = 3; g.gridy = 1;
        ulpanel.add(tb3,g);

        upanel.add(ulpanel);
        upanel.add(urpanel);
        mainpanel.add(upanel);
        add(mainpanel);

    }
}

I’ve tried to embed the Applet in HTML and specify the size there, but still, only the outer ‘shell’ of the Applet takes that shell and not the inner Panel which I’ve added onto the applet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:24:30+00:00Added an answer on June 14, 2026 at 2:24 pm

    +1 for SSCCE

    1)I would not mix Swing components with AWT Applet rather use JApplet instead.

    public class Test2 extends JApplet {
    

    2) Use SwingUtilities.invokeAndWait() block to wrap up your UI creation in overridden init() see here.

    Like so (Exception Handling omitted).

    @Override
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                //create UI here
                }
            });
    }
    

    Seems like it works for me or am I missing something?:

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.BorderFactory;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class Test extends JApplet {
    
        JPanel mainpanel = new JPanel();
        JPanel upanel = new JPanel();
        JPanel dpanel = new JPanel();
        JPanel ulpanel = new JPanel();
        JPanel urpanel = new JPanel();
        JPanel dlpanel = new JPanel();
        JPanel drpanel = new JPanel();
        JLabel l1 = new JLabel("Label 1");
        JLabel l2 = new JLabel("Label 2");
        JLabel l3 = new JLabel("Label 3");
        JLabel l4 = new JLabel("Label 4");
        JTextField tb1 = new JTextField();
        JTextField tb2 = new JTextField();
        JTextField tb3 = new JTextField();
        JTextArea ta1 = new JTextArea();
    
        @Override
        public void init() {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        initComponents();
                    }
                });
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            }
        }
    
        private void initComponents() {
            mainpanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
            ulpanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
            urpanel.setBorder(BorderFactory.createLineBorder(Color.green, 2));
            upanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));
            mainpanel.setLayout(new GridLayout(1, 1));
            upanel.setLayout(new GridLayout(1, 2));
            urpanel.setLayout(new BorderLayout());
            urpanel.add(ta1, BorderLayout.CENTER);
            ulpanel.setLayout(new GridBagLayout());
            GridBagConstraints g = new GridBagConstraints();
            g.ipadx = 2;
            g.ipady = 2;
            g.insets = new Insets(2, 2, 2, 2);
            //      g.anchor = GridBagConstraints.EAST;
            g.gridx = 0;
            g.gridy = 0;
            ulpanel.add(l1, g);
            g.gridx = 0;
            g.gridy = 1;
            ulpanel.add(l2, g);
            g.gridx = 2;
            g.gridy = 1;
            ulpanel.add(l3, g);
            g.gridx = 0;
            g.gridy = 2;
            ulpanel.add(l4, g);
    
            g.gridwidth = GridBagConstraints.REMAINDER;
            g.fill = GridBagConstraints.HORIZONTAL;
            //      g.anchor = GridBagConstraints.WEST;
            g.weightx = 1;
            g.gridx = 1;
            g.gridy = 0;
            ulpanel.add(tb1, g);
            g.gridwidth = 1;
            g.gridx = 1;
            g.gridy = 1;
            ulpanel.add(tb2, g);
            g.gridx = 3;
            g.gridy = 1;
            ulpanel.add(tb3, g);
    
            upanel.add(ulpanel);
            upanel.add(urpanel);
            mainpanel.add(upanel);
            add(mainpanel);
        }
    }
    

    UPDATE

    I see (after reading @AndrewThompson comment) you might be talking about the size and why everything is squashed? well as he said, with aJFrame you can set the size or call pack() which will set the size of the frame to fit its components.

    For an Applet/JApplet however you will change the size via the HTML launcher. Find this in the HTML and changing it as you need:

        <applet width="600" height="600">         <!-- ALTER WIDTH AND HEIGHT HERE -->
            <param name="jnlp_href" value="launch.jnlp"/>
        </applet>
    

    There also might be this which too would need to be altered:

        <script>
            var attributes = {
                code:       "Test",
                archive:    "TestJApplet.jar",
                width:      600,            <!-- ALTER WIDTH HERE -->
                height:     600             <!-- ALTER HEIGHT HERE -->
            };
            var parameters = {jnlp_href:"launch.jnlp"}; <!-- Applet Parameters -->
            var version = "1.7"; <!-- Required Java Version -->
            deployJava.runApplet(attributes, parameters, version);
        </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have filled splitcontainer. legend for image: SplitContainer ListViews Spliter Need: blue lines show
I'm trying to make the LED of the HTC Legend color blue along with
Another ggplot legend question! I have a dataset of the form test <- data.frame(
I have problem with items order in legend, when I using Google Chrome browser
I am creating a horizontal legend in html/css. I have a colored box with
I have the following picture : And I would like to make a legend
Ok, in CakePHP, I have the following code in my view add.ctp : echo
I have following R code: ggplot(data=curve,aes(x = expected, y=result/games)) + geom_point(aes(x=-expected, colour=games)) + stat_function(fun=funx,
I have css code to do layout. i have basic header panel, footer, left
I have a linebreak in a legend in R and my problem is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.