Page 1 of 1

Java applet menus

Posted: Mon Jan 08, 2007 6:12 pm
by shiznatix
I am trying to start this java applet that I want and I figure this will be the first part.

I want to make a little menu that has the 'file' and stuff options at the top. Nothing fancy yet, just like file -> open and whatnot. How do I go about doing this? I searched but could not come up with anything that I understood or that answered my question.

Re: Java applet menus

Posted: Mon Jan 08, 2007 6:32 pm
by Chris Corbyn
shiznatix wrote:I am trying to start this java applet that I want and I figure this will be the first part.

I want to make a little menu that has the 'file' and stuff options at the top. Nothing fancy yet, just like file -> open and whatnot. How do I go about doing this? I searched but could not come up with anything that I understood or that answered my question.
If you haven't read the Swing tutorials from the very beginning this will go right over your head ;)

Posted: Tue Jan 09, 2007 3:12 pm
by shiznatix
Ok this will be a 2 part question. 1st part: static variables and calling them.

I have 3 classes, the 'shiznatix' class (called first, displays the menu and content), the 'VisualElements' class (builds and returns my menu and content), and the 'ActionHandler' class (handles actions, hopefully).

Now im shiznatix, I have var:
public static String name = "andrew";

and I call it in my shiznatix init method like so:
this.name

no problems. But then I have class VisualElements that has this var:
private static JLabel content;

and I try to call it in one of its methods like this:
this.content = new JLabel(name);

but that gives me the error that I am calling a non-static variable statically. I dont understand the difference or how that is working.

-----------------------

Part 2!

I am trying to change this text in the middle of the applet when they click on a menu item. No luck so far sadly. The only error is that when I compile it, it says that in the ActionHandler class, shiznatix can not be called statically. This is the code for the 3 classes that I have come up with thus far. They are fairly straight forward.

Code: Select all

//shiznatix class
import java.awt.*;
import javax.swing.*;

public class shiznatix extends JApplet
{
    public static String name = "andrew";

    //main class
    public void init()
    {
        //create the menu
        setJMenuBar(VisualElements.CreateMenu());

        //set the middle content
        add(VisualElements.GetFrameContent(this.name), BorderLayout.CENTER);
    }
}


//visual elements class
import javax.swing.*;

public class VisualElements
{
    private static JLabel content;
    private static JMenuBar menu_bar;
    private static JMenu menu_file;
    private static JMenuItem menu_file_open;

    public static JLabel GetFrameContent(String name)
    {
        content = new JLabel(name);
        content.setHorizontalAlignment(JLabel.CENTER);

        return content;
    }

    public static JMenuBar CreateMenu()
    {
        menu_bar = new JMenuBar();

        menu_file = new JMenu("File");
        menu_bar.add(menu_file);

        menu_file_open = new JMenuItem("Open");
        menu_file_open.addActionListener(new ActionHandler());

        menu_file.add(menu_file_open);

        return menu_bar;
    }
}


//action handler class

import java.awt.event.*;

class ActionHandler implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        //not working!
        shiznatix.add(VisualElements.GetFrameContent("wins"));
    }
}

Posted: Tue Jan 09, 2007 3:31 pm
by Chris Corbyn
shiznatix wrote:Ok this will be a 2 part question. 1st part: static variables and calling them.

I have 3 classes, the 'shiznatix' class (called first, displays the menu and content), the 'VisualElements' class (builds and returns my menu and content), and the 'ActionHandler' class (handles actions, hopefully).

Now im shiznatix, I have var:
public static String name = "andrew";

and I call it in my shiznatix init method like so:
this.name

no problems. But then I have class VisualElements that has this var:
private static JLabel content;

and I try to call it in one of its methods like this:
this.content = new JLabel(name);

but that gives me the error that I am calling a non-static variable statically. I dont understand the difference or how that is working.
No, no.... drop the "this." part and just refer to it as a variable with scope accessible to the entire class. For external access use Shiznatix.name (ClassName.staticVar). BTW, in Java you should start class names with an uppercase letter for good practise. ;)

[[ EDIT | Basically, the difference is that this refers to an object. Same in PHP, you can't call statics with $this ;) ]]

shiznatix wrote: Part 2!

I am trying to change this text in the middle of the applet when they click on a menu item. No luck so far sadly. The only error is that when I compile it, it says that in the ActionHandler class, shiznatix can not be called statically. This is the code for the 3 classes that I have come up with thus far. They are fairly straight forward.

Code: Select all

//shiznatix class
import java.awt.*;
import javax.swing.*;

public class shiznatix extends JApplet
{
    public static String name = "andrew";

    //main class
    public void init()
    {
        //create the menu
        setJMenuBar(VisualElements.CreateMenu());

        //set the middle content
        add(VisualElements.GetFrameContent(this.name), BorderLayout.CENTER);
    }
}


//visual elements class
import javax.swing.*;

public class VisualElements
{
    private static JLabel content;
    private static JMenuBar menu_bar;
    private static JMenu menu_file;
    private static JMenuItem menu_file_open;

    public static JLabel GetFrameContent(String name)
    {
        content = new JLabel(name);
        content.setHorizontalAlignment(JLabel.CENTER);

        return content;
    }

    public static JMenuBar CreateMenu()
    {
        menu_bar = new JMenuBar();

        menu_file = new JMenu("File");
        menu_bar.add(menu_file);

        menu_file_open = new JMenuItem("Open");
        menu_file_open.addActionListener(new ActionHandler());

        menu_file.add(menu_file_open);

        return menu_bar;
    }
}


//action handler class

import java.awt.event.*;

class ActionHandler implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        //not working!
        shiznatix.add(VisualElements.GetFrameContent("wins"));
    }
}
You're over-using statics. If there's no "need" to use statics then use an object. In any case, you need to pass an instance of shiznatix to your action handler:

Code: Select all

//shiznatix class
import java.awt.*;
import javax.swing.*;

public class shiznatix extends JApplet
{
    public static String name = "andrew";

    //main class
    public void init()
    {
        VisualElements.setApplet(this);
        //create the menu
        setJMenuBar(VisualElements.CreateMenu());

        //set the middle content
        add(VisualElements.GetFrameContent(this.name), BorderLayout.CENTER);
    }
}


//visual elements class
import javax.swing.*;

public class VisualElements
{
    private static JLabel content;
    private static JMenuBar menu_bar;
    private static JMenu menu_file;
    private static JMenuItem menu_file_open;
    private static JApplet targetApplet = null;

    public static void setApplet(JApplet target)
    {
        targetApplet = target;
    }

    public static JLabel GetFrameContent(String name)
    {
        content = new JLabel(name);
        content.setHorizontalAlignment(JLabel.CENTER);

        return content;
    }

    public static JMenuBar CreateMenu()
    {
        menu_bar = new JMenuBar();

        menu_file = new JMenu("File");
        menu_bar.add(menu_file);

        menu_file_open = new JMenuItem("Open");
        menu_file_open.addActionListener(new ActionHandler(targetApplet));

        menu_file.add(menu_file_open);

        return menu_bar;
    }
}


//action handler class

import java.awt.event.*;

class ActionHandler implements ActionListener
{
    private JApplet targetApplet = null;
    
    public ActionHandler(JApplet target)
    {
        this.targetApplet = target;
    }
    
    public void actionPerformed(ActionEvent event)
    {
        this.targetApplet.add(VisualElements.GetFrameContent("wins"));
    }
}
My suggestion: Create a registry to store the applet in, or make shiznatix a singleton.

Posted: Tue Jan 09, 2007 6:06 pm
by shiznatix
Edit: I signed it, I got it to work, I win!