2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1265125
  • 2,608
  • 8
  • 42
  • 65
  • 1
    1) *"..with the `setsize()` as same, both of them produce different results."* An applet size is typically set by HTML. 2) For a Swing applet, start with `JApplet`3) Suggest some size using `JTextArea ta1 = new JTextArea(4,20); //play with the numbers` 4) When you get the frame right, abandon the applet and launch the frame using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Nov 19 '12 at 21:25
  • @AndrewThompson After that abandon JWS and start Java EE. – Roman C Nov 19 '12 at 21:31
  • 1
    @RomanC Why would the user want to give up a rich client interface for HTML? – Andrew Thompson Nov 19 '12 at 21:36
  • @AndrewThompson Because of user want to give up a JWS. – Roman C Nov 19 '12 at 21:55

1 Answers1

3

+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>
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • *facepalm*. Seems like it was just a matter of changing Applet to JApplet. Thanks! – user1265125 Nov 19 '12 at 21:33
  • 1
    @user1265125 At least its fixed :) . I would always recommend use `JApplet`, but please note the rest about the `SwingUtilities` is important – David Kroukamp Nov 19 '12 at 21:34
  • @trashgod I am not sure now that you mention it. on what would you call `pack()`? the `JApplet` instance? – David Kroukamp Nov 19 '12 at 21:36
  • 1
    @trashgod `J/Applet` has no `pack()` method, I generally call `validate()`. – Andrew Thompson Nov 19 '12 at 21:58
  • 1
    I'd already up-voted, before I even got to.. *"There also might be this which too would need to be altered:.. (`deployJava.js`)"*. I often forget to mention it, though I'm always thinking '..& when we get the paths sorted, convert it to use the script'. Great answer. – Andrew Thompson Nov 19 '12 at 22:05
  • @AndrewThompson Thank you and I would love to take all the credit, but Netbeans IDE did help me generate the script :P – David Kroukamp Nov 20 '12 at 04:04