Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a JPanel (mainPanel) with CardLayout which contains 4 JPanels (childPanel).

Each childPanel contains:

  • a JPopupMenu with 4 JMenuItems to switch between the childPanels (First,Last,Next,Previous)
  • a label with the time when it got visible
  • a toggleButton that shows/hides the label

I created this working version but I think it can be optimized. Any advice is welcome.

public final class Prozor extends JFrame
{
    private static final Dimension SIZE = new Dimension(400, 300);

    private final JPanel panelButtoni = new JPanel();
    private JButton prvi;
    private JButton zadnji;
    private JButton sljedeci;
    private JButton prethodni;

    private CardLayout cardLayout = new CardLayout();
    private JPanel panelCards = new JPanel(cardLayout);

    private JPopupMenu crveniMeni= new JPopupMenu();
    private JPanel crveniPanel= new JPanel();
    private JPopupMenu crveniKontekstni= new JPopupMenu();
    private JLabel crvenaLabelica = new JLabel();
    private JToggleButton crveniButton;

    private JPopupMenu plaviMeni= new JPopupMenu();
    private JPanel plaviPanel = new JPanel();
    private JPopupMenu plaviKontekstni = new JPopupMenu();
    private JLabel plavaLabelica = new JLabel();
    private JToggleButton plaviButton;

    private JPopupMenu zeleniMeni = new JPopupMenu();
    private JPanel zeleniPanel = new JPanel();
    private JPopupMenu zeleniKontekstni = new JPopupMenu();
    private JLabel zelenaLabelica = new JLabel();
    private JToggleButton zeleniButton;

    private JPopupMenu zutiMeni= new JPopupMenu();
    private JPanel zutiPanel = new JPanel();
    private JPopupMenu zutiKontekstni = new JPopupMenu();
    private JLabel zutaLabelica = new JLabel();
    private JToggleButton zutiButton;

    public Prozor(String title)
    {
        super(title);
        setPreferredSize(SIZE);
        setMinimumSize(SIZE);
        setMaximumSize(SIZE);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        kreirajbuttonAkcije();

        kreirajCrveniPanel();
        kreirajPlaviPanel();
        kreirajZeleniPanel();
        kreirajZutiPanel();

        this.add(panelCards);

        pack();
    }

    public static void main(String[] args)
    {
        Prozor p = new Prozor("ime");
        p.setVisible(true);
    }

    private void kreirajCrveniPanel() {
        crveniPanel.setBackground(Color.RED);
        crveniButton = new JToggleButton("Prikazi vrijeme");
        crveniButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                if (crveniButton.isSelected()) {
                    crvenaLabelica.setText(Calendar.getInstance().getTime().toString());
                    crvenaLabelica.setVisible(true);
                }
                else
                {
                    crvenaLabelica.setVisible(false);
                }
            }
        });

        JMenuItem jmPrvi = new JMenuItem("Prvi");
        jmPrvi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.first(panelCards);
            }
        });
        JMenuItem jmZadnji = new JMenuItem("Zadnji");
        jmZadnji.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.last(panelCards);
            }
        });
        JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
        jmSljedeci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.next(panelCards);
            }
        });
        JMenuItem jmPrethodni = new JMenuItem("Prethodni");
        jmPrethodni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.previous(panelCards);
            }
        });

        crveniPanel.addMouseListener(new MouseAdapter() 
        {

            @Override
            public void mouseReleased(MouseEvent me) {
                if (me.isPopupTrigger()) {
                    crveniMeni.show(me.getComponent(), me.getX(), me.getY());
                }
            }


        });

        crveniMeni.add(jmPrvi);
        crveniMeni.add(jmZadnji);
        crveniMeni.add(jmPrethodni);
        crveniMeni.add(jmSljedeci);
        crveniPanel.add(crveniMeni);
        crveniPanel.add(crvenaLabelica);
        crveniPanel.add(crveniButton);
        panelCards.add(crveniPanel);
    }

    private void kreirajPlaviPanel() {
        plaviPanel.setBackground(Color.BLUE);
        plaviButton =new JToggleButton("Prikazi vrijeme");
        plaviButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (plaviButton.isSelected()) {
                    plavaLabelica.setText(Calendar.getInstance().getTime().toString());
                    plavaLabelica.setVisible(true);
                }else{
                    plavaLabelica.setVisible(false);
                }
            }
        });

        JMenuItem jmPrvi = new JMenuItem("Prvi");
        jmPrvi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.first(panelCards);
            }
        });
        JMenuItem jmZadnji = new JMenuItem("Zadnji");
        jmZadnji.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.last(panelCards);
            }
        });
        JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
        jmSljedeci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.next(panelCards);
            }
        });
        JMenuItem jmPrethodni = new JMenuItem("Prethodni");
        jmPrethodni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.previous(panelCards);
            }
        });

        plaviPanel.addMouseListener(new MouseAdapter() 
        {

            @Override
            public void mouseReleased(MouseEvent me) {
                if (me.isPopupTrigger()) {
                    plaviMeni.show(me.getComponent(), me.getX(), me.getY());
                }
            }


        });

        plaviMeni.add(jmPrvi);
        plaviMeni.add(jmZadnji);
        plaviMeni.add(jmPrethodni);
        plaviMeni.add(jmSljedeci);
        plaviPanel.add(plaviMeni);
        plaviPanel.add(plavaLabelica);
        plaviPanel.add(plaviButton);
        panelCards.add(plaviPanel);
    }

    private void kreirajZeleniPanel() {
        zeleniPanel.setBackground(Color.GREEN);
        zeleniButton=new JToggleButton("Prikazi vrijeme");
        zeleniButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (zeleniButton.isSelected()) {
                    zelenaLabelica.setText(Calendar.getInstance().getTime().toString());
                    zelenaLabelica.setVisible(true);
                }else{
                    zelenaLabelica.setVisible(false);
                }
            }
        });

        JMenuItem jmPrvi = new JMenuItem("Prvi");
        jmPrvi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.first(panelCards);
            }
        });
        JMenuItem jmZadnji = new JMenuItem("Zadnji");
        jmZadnji.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.last(panelCards);
            }
        });
        JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
        jmSljedeci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.next(panelCards);
            }
        });
        JMenuItem jmPrethodni = new JMenuItem("Prethodni");
        jmPrethodni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.previous(panelCards);
            }
        });

        zeleniPanel.addMouseListener(new MouseAdapter() 
        {

            @Override
            public void mouseReleased(MouseEvent me) {
                if (me.isPopupTrigger()) {
                    zeleniMeni.show(me.getComponent(), me.getX(), me.getY());
                }
            }


        });

        zeleniMeni.add(jmPrvi);
        zeleniMeni.add(jmZadnji);
        zeleniMeni.add(jmPrethodni);
        zeleniMeni.add(jmSljedeci);
        zeleniPanel.add(zeleniMeni);
        zeleniPanel.add(zelenaLabelica);
        zeleniPanel.add(zeleniButton);
        panelCards.add(zeleniPanel);
    }

    private void kreirajZutiPanel() {
        zutiPanel.setBackground(Color.YELLOW);
        zutiButton=new JToggleButton("Prikazi vrijeme");
        zutiButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (zutiButton.isSelected()) {
                    zutaLabelica.setText(Calendar.getInstance().getTime().toString());
                    zutaLabelica.setVisible(true);
                }else{
                    zutaLabelica.setVisible(false);
                }
            }
        });

        JMenuItem jmPrvi = new JMenuItem("Prvi");
        jmPrvi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.first(panelCards);
            }
        });
        JMenuItem jmZadnji = new JMenuItem("Zadnji");
        jmZadnji.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.last(panelCards);
            }
        });
        JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
        jmSljedeci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.next(panelCards);
            }
        });
        JMenuItem jmPrethodni = new JMenuItem("Prethodni");
        jmPrethodni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                cardLayout.previous(panelCards);
            }
        });

        zutiPanel.addMouseListener(new MouseAdapter() 
        {

            @Override
            public void mouseReleased(MouseEvent me) {
                if (me.isPopupTrigger()) {
                    zutiMeni.show(me.getComponent(), me.getX(), me.getY());
                }
            }


        });

        zutiMeni.add(jmPrvi);
        zutiMeni.add(jmZadnji);
        zutiMeni.add(jmPrethodni);
        zutiMeni.add(jmSljedeci);
        zutiPanel.add(zutiMeni);

        zutiPanel.add(zutaLabelica);
        zutiPanel.add(zutiButton);
        panelCards.add(zutiPanel);
    }

    private void kreirajbuttonAkcije() {
        prvi= new JButton("Prvi");
        prvi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.first(panelCards);
            }
        });

        zadnji= new JButton("Zadnji");
        zadnji.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.last(panelCards);
            }
        });

        sljedeci= new JButton("Sljedeći");
        sljedeci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.next(panelCards);
            }
        });

        prethodni= new JButton("Prethodni");
        prethodni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.previous(panelCards);
            }
        });

        panelButtoni.add(prvi);
        panelButtoni.add(zadnji);
        panelButtoni.add(prethodni);
        panelButtoni.add(sljedeci);
        this.add(panelButtoni,BorderLayout.NORTH);

    }

}
share|improve this question
    
kreirajCrveniPanel() kreirajPlaviPanel() and so on are identical for the different panels, but i would like to make 1 function that handles all 4 since the code is the same –  Goran Štuc Feb 18 at 17:38
add comment

1 Answer

I found the answer and have posted it in case someone has a similar problem:

I created a class that extends JPanel like this:

public class CardPanel extends JPanel{
    private JPopupMenu menu;
    private JLabel label;
    private JToggleButton button;
    private final CardLayout layout;
    private final JPanel parent;

    public CardPanel(CardLayout layout,Color color,JPanel parent)
    {
        super();
        this.layout = layout;
        this.parent=parent;
        setBackground(color);

        kreirajKontekstni();


    }

    private void kreirajKontekstni() {
        button =new JToggleButton("Prikazi vrijeme");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (button.isSelected()) {
                    label.setText(Calendar.getInstance().getTime().toString());
                    label.setVisible(true);
                }else{
                    label.setVisible(false);
                }
            }
        });
        JMenuItem jmPrvi = new JMenuItem("Prvi");
        jmPrvi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                layout.first(parent);
            }
        });
        JMenuItem jmZadnji = new JMenuItem("Zadnji");
        jmZadnji.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                layout.last(parent);
            }
        });
        JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
        jmSljedeci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                layout.next(parent);
            }
        });
        JMenuItem jmPrethodni = new JMenuItem("Prethodni");
        jmPrethodni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                layout.previous(parent);
            }
        });

        addMouseListener(new MouseAdapter() 
        {

            @Override
            public void mouseReleased(MouseEvent me) {
                if (me.isPopupTrigger()) {
                    menu.show(me.getComponent(), me.getX(), me.getY());
                }
            }


        });

        menu.add(jmPrvi);
        menu.add(jmZadnji);
        menu.add(jmPrethodni);
        menu.add(jmSljedeci);
        add(menu);
        add(label);
        add(button);
        parent.add(this);
    }

}

and changed the code in the main Window to:

public class SimpleProzor extends JFrame
{
    private static final Dimension SIZE = new Dimension(400, 300);

    private final JPanel panelButtoni = new JPanel();
    private JButton prvi;
    private JButton zadnji;
    private JButton sljedeci;
    private JButton prethodni;

    private CardLayout cardLayout = new CardLayout();
    private JPanel panelCards = new JPanel(cardLayout);

    private CardPanel crveniPanel;
    private CardPanel plaviPanel;
    private CardPanel zeleniPanel;
    private CardPanel zutiPanel;

    public SimpleProzor(String title)
    {
        super(title);
        setPreferredSize(SIZE);
        setMinimumSize(SIZE);
        setMaximumSize(SIZE);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        kreirajbuttonAkcije();

        crveniPanel = new CardPanel(cardLayout, Color.RED, panelCards);
        plaviPanel = new CardPanel(cardLayout, Color.BLUE, panelCards);
        zeleniPanel = new CardPanel(cardLayout, Color.GREEN, panelCards);
        zutiPanel= new CardPanel(cardLayout, Color.YELLOW, panelCards);

        this.add(panelCards);

        pack();
    }

    public static void main(String[] args)
    {
        Prozor p = new Prozor("ime");
        p.setVisible(true);
    }

    private void kreirajbuttonAkcije() {
        prvi= new JButton("Prvi");
        prvi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.first(panelCards);
            }
        });

        zadnji= new JButton("Zadnji");
        zadnji.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.last(panelCards);
            }
        });

        sljedeci= new JButton("Sljedeći");
        sljedeci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.next(panelCards);
            }
        });

        prethodni= new JButton("Prethodni");
        prethodni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                cardLayout.previous(panelCards);
            }
        });

        panelButtoni.add(prvi);
        panelButtoni.add(zadnji);
        panelButtoni.add(prethodni);
        panelButtoni.add(sljedeci);
        this.add(panelButtoni,BorderLayout.NORTH);   
    }   
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.