0

Well I'm making a client for my server and the JMenu worked fine until I added the applet, now when I click on the menu to look at the drop down menu, it doesn't show. I assume its hiding behind the applet but I can't fix it

public class Loader extends JApplet implements Serializable, ActionListener {

private static final long serialVersionUID = 7639088664641445302L;

private static final HashMap<String, String> THEME_LIST = new HashMap<String, String>();

private final String[] THEME_NAMES = { "Magma", "Emerald", "Raven", "Black Steel", "Challenger", "Sahara", "Silver", "Blue" };

public Properties params = new Properties();
public JFrame mainframe;
public JPanel mainpanel = new JPanel();

public static boolean useIsaac = false;

private static Robot robot;

private static final String HOME = System.getProperty("user.home") + "\\";

public static String THEME = "org.jvnet.substance.skin.SubstanceBusinessLookAndFeel";
public static String IP = "77.248.12.184";
public static boolean LOBBY_ENABLED;
public static boolean DISABLE_XTEA_CRASH = true;
public static boolean DISABLE_USELESS_PACKETS = true;
public static boolean DISABLE_RSA = false;

public static final int REVISION = 718;
public static int SUB_REVISION = 3;
public static Loader instance;

private JMenuItem forums, vote, donate;
private JCheckBoxMenuItem lastChecked;
private JMenu links, options, themes;
private JCheckBox lobbyCheckBox;
private JMenuBar menuBar;

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(THEME);
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JPopupMenu.setDefaultLightWeightPopupEnabled(true);
    // final GUI gui = instance = (GUI) loadSerializedFile();
    robot = new Robot();
    final Loader gui = new Loader();
    LOBBY_ENABLED = gui.lobbyCheckBox.isSelected();
    gui.mainframe.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            storeSerializableClass(gui);
        }
    });
    gui.mainframe.setVisible(true);
    gui.startClient();
}

public void startClient() {
    try {
        client clnt = new client();
        clnt.supplyApplet(this);
        clnt.init();
        clnt.start();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

public Loader() {
    params.put("separate_jvm", "true");
    params.put("boxbgcolor", "black");
    params.put("image", "http://www.runescape.com/img/game/splash2.gif");
    params.put("centerimage", "true");
    params.put("boxborder", "false");
    params.put("java_arguments", "-Xmx256m -Xss2m -Dsun.java2d.noddraw=true -XX:CompileThreshold=1500 -Xincgc -XX:+UseConcMarkSweepGC -XX:+UseParNewGC");
    params.put("27", "0");
    params.put("1", "0");
    params.put("16", "false");
    params.put("17", "false");
    params.put("21", "1"); // WORLD ID
    params.put("30", "false");
    params.put("20", IP);
    params.put("29", "");
    params.put("11", "true");
    params.put("25", "1378752098");
    params.put("28", "0");
    params.put("8", ".runescape.com");
    params.put("23", "false");
    params.put("32", "0");
    params.put("15", "wwGlrZHF5gKN6D3mDdihco3oPeYN2KFybL9hUUFqOvk");
    params.put("0", "IjGJjn4L3q5lRpOR9ClzZQ");
    params.put("2", "");
    params.put("4", "1"); // WORLD ID
    params.put("14", "");
    params.put("5", "8194");
    params.put("-1", "QlwePyRU5GcnAn1lr035ag");
    params.put("6", "0");
    params.put("24", "true,false,0,43,200,18,0,21,354,-15,Verdana,11,0xF4ECE9,candy_bar_middle.gif,candy_bar_back.gif,candy_bar_outline_left.gif,candy_bar_outline_right.gif,candy_bar_outline_top.gif,candy_bar_outline_bottom.gif,loadbar_body_left.gif,loadbar_body_right.gif,loadbar_body_fill.gif,6");
    params.put("3", "hAJWGrsaETglRjuwxMwnlA/d5W6EgYWx");
    params.put("12", "false");
    params.put("13", "0");
    params.put("26", "0");
    params.put("9", "77");
    params.put("22", "false");
    params.put("18", "false");
    params.put("33", "");
    params.put("haveie6", "false");

    mainframe = new JFrame("Matrix RSPS");
    mainframe.getContentPane().setLayout(new BorderLayout());
    mainpanel.setLayout(new BorderLayout());
    mainpanel.setPreferredSize(new Dimension(765, 503));
    mainframe.getContentPane().add(mainpanel, "Center");
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainpanel.add(this);

    menuBar = new JMenuBar();
    mainframe.setJMenuBar(menuBar);

    lobbyCheckBox = new JCheckBox("Enable Lobby");
    lobbyCheckBox.addActionListener(this);
    lobbyCheckBox.setHorizontalTextPosition(SwingConstants.LEFT);
    menuBar.add(lobbyCheckBox);

    links = new JMenu("Links");
    menuBar.add(links);

    forums = new JMenuItem("Forums");
    forums.addActionListener(this);
    links.add(forums);

    vote = new JMenuItem("Vote");
    vote.addActionListener(this);
    links.add(vote);

    donate = new JMenuItem("Donate");
    donate.addActionListener(this);
    links.add(donate);

    options = new JMenu("Options");
    menuBar.add(options);

    themes = new JMenu("Themes");
    options.add(themes);

    JCheckBoxMenuItem temp;
    for (int i = 0; i < THEME_NAMES.length; i++) {
        String theme = THEME_NAMES[i];
        temp = new JCheckBoxMenuItem(theme);
        temp.addActionListener(this);

        if (THEME.equalsIgnoreCase(THEME_LIST.get(theme))) {
            lastChecked = temp;
            temp.setSelected(true);
        }
        themes.add(temp);
    }
    mainframe.pack();
    mainframe.setVisible(true);
}

public String getParameter(String string) {
    return (String) params.get(string);
}

public URL getDocumentBase() {
    return getCodeBase();
}

public URL getCodeBase() {
    try {
        return new URL("http://" + IP);
    } catch (Exception exception) {
        exception.printStackTrace();
        return null;
    }
}

public static final Object loadSerializedFile() {
    try {
        File saveDirect = new File(HOME + "Saradomin");
        File saveFile = new File(HOME + "Saradomin\\Client Save.sv");
        if (!saveDirect.exists() || !saveFile.exists())
            return new Loader();
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(saveFile));
        Object object = in.readObject();
        in.close();
        return object;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new Loader();
}

public static final void storeSerializableClass(Serializable o) {
    try {
        File saveDirect = new File(HOME + "Saradomin");
        File saveFile = new File(HOME + "Saradomin\\Client Save.sv");
        if (!saveDirect.exists())
            saveDirect.mkdir();
        if (!saveFile.exists())
            saveFile.createNewFile();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(saveFile));
        out.writeObject(o);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

static {
    THEME_LIST.put("Magma", "org.jvnet.substance.skin.SubstanceMagmaLookAndFeel");
    THEME_LIST.put("Emerald", "org.jvnet.substance.skin.SubstanceEmeraldDuskLookAndFeel");
    THEME_LIST.put("Black Steel", "org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel");
    THEME_LIST.put("Raven", "org.jvnet.substance.skin.SubstanceRavenGraphiteGlassLookAndFeel");
    THEME_LIST.put("Challenger", "org.jvnet.substance.skin.SubstanceChallengerDeepLookAndFeel");
    THEME_LIST.put("Sahara", "org.jvnet.substance.skin.SubstanceSaharaLookAndFeel");
    THEME_LIST.put("Silver", "org.jvnet.substance.skin.SubstanceBusinessLookAndFeel");
    THEME_LIST.put("Blue", "org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel");
}

private void openURL(String url) throws Exception {
    Desktop.getDesktop().browse(new URI(url));
}

public String getFormattedDate() {
    DateFormat dateFormat = new SimpleDateFormat("hh.mm.ss a");
    Date date = new Date();
    String formattedDate = dateFormat.format(date);
    return formattedDate;
}

private Rectangle getGameScreen() {
    Rectangle rect = this.getBounds();
    rect.setLocation(this.getLocationOnScreen());
    return rect;
}

private void confirmImage(BufferedImage createScreenCapture) {
    new ConfirmImage(createScreenCapture);
}

@Override
public void actionPerformed(ActionEvent e) {
    try {
        String command = e.getActionCommand();
        System.out.println(command);
        String selectedTheme = THEME_LIST.get(e.getActionCommand());
        if (selectedTheme != null) {
            if (lastChecked != null)
                lastChecked.setSelected(false);
            lastChecked = (JCheckBoxMenuItem) e.getSource();
            UIManager.setLookAndFeel(selectedTheme);
        } else if (command.equalsIgnoreCase("enable lobby")) {
            confirmImage(robot.createScreenCapture(getGameScreen()));//temp until screenshot is 100%
            //LOBBY_ENABLED = lobbyCheckBox.isSelected();
        } else if (command.equalsIgnoreCase("forums")) {
            openURL("http://www.saradom.in/");
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

private class ConfirmImage implements ActionListener {

    private JFrame frame;
    private JButton btnSave, btnDiscard, btnUpload;
    private BufferedImage image;

    public ConfirmImage(BufferedImage image) {
        this.image = image;
        openFrame();
    }

    public void openFrame() {
        frame = new JFrame("Decide the fate of you screenshot.");
        Insets insets = frame.getInsets();
        frame.setSize(image.getWidth()+insets.left + insets.right, image.getHeight()+insets.top + insets.bottom);
        frame.setResizable(false);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(frame, "Are you sure you want to discard this image(you will not be able to get it back)?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) {
                    return;
                }
                frame.dispose();
            }

        });
        frame.setDefaultCloseOperation(0);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        frame.setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel picLabel = new JLabel(new ImageIcon(image));
        picLabel.setBounds(0, 0, image.getWidth(), image.getHeight());
        contentPane.add(picLabel);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        btnSave = new JButton("Save Image");
        menuBar.add(btnSave);
        btnSave.addActionListener(this);

        btnDiscard = new JButton("Discard Image");
        menuBar.add(btnDiscard);
        btnDiscard.addActionListener(this);

        btnUpload = new JButton("Upload Image");
        menuBar.add(btnUpload);
        btnUpload.addActionListener(this);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 484, 362);
        contentPane.add(panel);

        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            String action = e.getActionCommand();
            if (action.equalsIgnoreCase("save image")) {
                ImageIO.write(image, "PNG", new File(HOME + "Saradomin" + getFormattedDate() + ".png"));
                JOptionPane.showConfirmDialog(frame, "Screenshot saved to: " + HOME + "Saradomin", "Screenshot save completed.", JOptionPane.PLAIN_MESSAGE);
                frame.dispose();
            } else if (action.equalsIgnoreCase("discard image")) {
                if (JOptionPane.showConfirmDialog(frame, "Are you sure you want to discard this image(you will not be able to get it back)?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) {
                    return;
                }
                frame.dispose();
            } else if (action.equalsIgnoreCase("upload image")) {
                new ImageLink(Utils.upload(image));
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
}

Edit: I know why this is caused you guys don't have to keep posting below on what the cause is, I'm asking for a way AROUND it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jonathan Beaudoin
  • 2,158
  • 4
  • 27
  • 63
  • I can't see how you applet would actually be able to load - you've not overridden `init` or `start` - Hell, I can't even see why you would need a applet, you create `JFrame`s all over the place... – MadProgrammer Jan 25 '13 at 05:23
  • !! You're adding a top level container inside your application - `mainpanel.add(this);` - This is not going to end well. You shouldn't be doing this !! – MadProgrammer Jan 25 '13 at 05:25
  • 1
    Umm? Did you even read through the code? client clnt = new client(); clnt.supplyApplet(this); clnt.init(); clnt.start(); – Jonathan Beaudoin Jan 25 '13 at 05:28
  • That's not how applets are suppose to work, `init` and `start` are empty methods used to provide notification to the applet that the browser wants it it initialize and start (as well as stop and dispose). Yes I read through your 404 lines of code, but around about the time I saw you were trying to add one top level container to another, I panicked and ran for the hills! – MadProgrammer Jan 25 '13 at 05:34
  • You also failed to supply the implementation of `client` – MadProgrammer Jan 25 '13 at 05:34
  • I would post my Client class if it was 10k lines. Theirs no other way I could add the applet without rewriting 90% of the client – Jonathan Beaudoin Jan 25 '13 at 05:36
  • 1
    The problem I can see, is you are trying to add a `JApplet` (the `Loader`) to a `JFrame`. You have not overridden the `init`, `start`, `stop` or `dispose` methods of this applet, you're relying on the constructor to do all that for you. From what I can tell, you don't intend to deploy this as an applet?!? – MadProgrammer Jan 25 '13 at 05:37
  • 1
    Look I'm obviously missing something here, what is it you are actually trying to do? The normal course would be to write your application so it was contained in something like a `JPanel`, that way you could add it to a `JFrame` OR `JApplet` simply by adding the panel to it...(excuse my freakzoid - but I think the main problem you're going to have is the fact that you are mixing two top level containers) – MadProgrammer Jan 25 '13 at 05:46
  • Where is the `JMenu` referenced in the tags of this question? If it is not a `JMenu` that goes behind the applet, what is it, an JS/HTML menu? This question is long and still very unclear. – Andrew Thompson Jan 25 '13 at 05:54
  • It's a regular .jar file not website related. – Jonathan Beaudoin Jan 25 '13 at 05:54
  • @JonathanBeaudoin I believe you run it the mix heavy/light weight issue of AWT/Swing. This would cause the applet to appear above the Swing components (the menus) – MadProgrammer Jan 25 '13 at 07:51
  • 1) Add @PersonName to notify them of a new comment. 2) *"It's a regular .jar file not website related."* So WTF does the text mention 'applet'? Why is the `JApplet` tag slapped on it? 3) `panel.setLayout(null); panel.setBounds(0, 0, 484, 362);` Fix this, it is bound to cause problems later, if not now. ***Use layouts!*** 4) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 25 '13 at 08:18

1 Answers1

5

I'm wary of frame.setContentPane(contentPane), which effectively replaces the contentPane with one having a (default) FlowLayout. Instead use frame.add(contentPane), which forwards "to the contentPane as necessary." A different name may also be helpful. See also Using Top-Level Containers and this answer.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I quite commonly set a content pane and have not noticed any artifacts from doing so. Mind you. If strange things began to happen, I might try doing it another way. – Andrew Thompson Jan 25 '13 at 17:33
  • @AndrewThompson: You're right; reproducing the problem in this way requires some additional contrivance like inadvertently replacing the root pane or setting the menu bar to `null`. – trashgod Jan 25 '13 at 18:37