0

I am trying to build an Applet, that contains a Menu on the left side and some Content on the right side. So when someone clicks on something different in the Menu, it should load the corresponding content on the right side. Therefore, I added a MouseListener. Just doing stuff like contentPanel.add(new AnlegenNeu()); in the corresponding if's is just stupid in my opinion. It work's as far as I can tell here, but it doesn't feel right.

This is my code so far:

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;


public class MainGUI extends JApplet{
    private static final long serialVersionUID = 1L;

    private JTree menuTree;
    private JPanel contentPanel;

    public MainGUI() {
        JPanel menuPanel = new JPanel();
        contentPanel = new JPanel();
        contentPanel.setPreferredSize(new Dimension(500,500));
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setDividerLocation(0.5);
        splitPane.setLeftComponent(menuPanel);
        splitPane.setRightComponent(contentPanel);
        menuTree = new MenuTree().getElement();
        JScrollPane menuScrollPane = new JScrollPane(menuTree);
        menuScrollPane.setPreferredSize(new Dimension(150, 200));
        menuScrollPane.setMinimumSize(new Dimension(150, 200));
        menuScrollPane.setMaximumSize(new Dimension(200, 200));
        menuPanel.add(menuScrollPane);      

        getContentPane().add(splitPane);
        getContentPane().setVisible(true);

        menuTree.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 1) {
                    String clicked = MainGUI.this.menuTree.getSelectionPath().getLastPathComponent().toString();    

                    if(clicked.equals("Test")) {
                        MainGUI.this.contentPanel.add(new AufgabeNeu().getElement());
                        MainGUI.this.repaint();
                    }
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

        });
    }
}

What would be a good way to code this? I feel like i am doing this wrong...

EDIT: Also the way I access variables in my Code via MainGUI.this.contentPanel.add() is weird and I don't like it. Isn't there a better way?

user1803551
  • 12,965
  • 5
  • 47
  • 74
  • Where is `init()`? For greater flexibility, consider a [hybrid](http://stackoverflow.com/q/12449889/230513). – trashgod Oct 25 '14 at 11:40
  • http://codereview.stackexchange.com – Dawood ibn Kareem Oct 25 '14 at 11:45
  • And you can just write `contentPanel.add()` without prefixing it with `MainGUI.this`. – Dawood ibn Kareem Oct 25 '14 at 11:48
  • You will probably want a `MouseAdapter` to make your code cleaner. – user1803551 Oct 25 '14 at 11:48
  • If the list on the left side corresponsd to components to be displayed on the right side, you might want to keep a `Map` between them. Also, If the components are not the same size when they replace each other it can look weird. – user1803551 Oct 25 '14 at 11:58
  • Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Oct 25 '14 at 12:21

0 Answers0