Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Menu Problems in Java

Ok, so I have a menu, with a submenu, that has like 5 options to choose from.  I have two submenus, and for each, i created a seperate actionListener like so:

class selectionMenuListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String menuName = event.getActionCommand();
            // THIS IS FOR SELECTION SORT ALWAYS [INCREASING]
            <b>JOptionPane.showMessageDialog(null,menuName);</b>
            if (menuName.equals("Name"))
            {
                sortSelectNameIncAlgo();
                sortByString = "NAME";
            }
            else if (menuName.equals("PPG"))
            {
                sortSelectPPGIncAlgo();
                sortByString = "PPG";
            }
            else if (menuName.equals("APG"))
                {
                sortSelectAPGIncAlgo();
                sortByString = "APG";
            }
            else if (menuName.equals("RPG"))
                {
                sortSelectRPGIncAlgo();
                sortByString = "RPG";
            }
            else if (menuName.equals("MPG"))
                {
                sortSelectMPGIncAlgo();
                sortByString = "MPG";
            }
            menuName = "";
            typeSortString = "INCREASING";
            algoSortString = "SELECTION";
           
            createPlayerInfoPanel();
            bottomMessage.setText("Info sorted by " + sortByString + " " + typeSortString + " using " + algoSortString + " SORT");
        }
    }
   
    class bubbleMenuListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String menuName = event.getActionCommand();
            // THIS IS FOR BUBBLE SORT ALWAYS [INCREASING]
           
            if (menuName.equals("Name"))
            {
                sortBubbleNameIncAlgo();
                sortByString = "NAME";
            }
            else if (menuName.equals("PPG"))
            {
                sortBubblePPGIncAlgo();
                sortByString = "PPG";
            }
            else if (menuName.equals("APG"))
                {
                sortBubbleAPGIncAlgo();
                sortByString = "APG";
            }
            else if (menuName.equals("RPG"))
                {
                sortBubbleRPGIncAlgo();
                sortByString = "RPG";
            }
            else if (menuName.equals("MPG"))
                {
                sortBubbleMPGIncAlgo();
                sortByString = "MPG";
            }
           
            typeSortString = "INCREASING";
            algoSortString = "BUBBLE";
 
            createPlayerInfoPanel();
            bottomMessage.setText("Info sorted by " + sortByString + " " + typeSortString + " using " + algoSortString + " SORT");
        }
    }

Now, when I click on the menu, select Bubble submenu, which is the second actionListener, it doesn't work.  But then again it kinda does.  The if statements and such work, because the bottomMessage.setText is set to the right stuff.  But the createPlayerInfoPanel(); doesn't seem to work.  That panel makes all the JLabels and such and sorts them, then adds the panel to the CENTER of a BorderLayout.  Now, the thing is, i was testing the first actionListener which is the Selection submenu, and it works fine, when I output the menuName with a JOptionPane.  So it repaints it perfectly.  But if I take out the JOptionPane, it only sorts by the first if statements function.  But still changes to the right string.   I think I explained that pretty bad, but I can try if no one understands what I mean.
0
mm7627
Asked:
mm7627
  • 14
  • 13
1 Solution
 
objectsCommented:
perhaps the problem is in createPlayerInfoPanel()
0
 
mm7627Author Commented:
Well, the thing is, if I add the JOptionPane.showMessageDialog(), it works perfectly.  But without it, nothing happens.
0
 
objectsCommented:
are you calling revalidate() after adding components to your container?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mm7627Author Commented:
Would I want to do it before I add it to the main container (BorderLayout.CENTER) where that panel is at?
0
 
mm7627Author Commented:
private void createPlayerInfoPanel()
    {
        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Player Information"));
       
        panel.setLayout(new GridLayout(15,6));
       
        panel.add(new JLabel("<html><u>Last Name</u></html>"));
        panel.add(new JLabel("<html><u>First Name</u></html>"));
        panel.add(new JLabel("<html><u>PPG</u></html>"));
        panel.add(new JLabel("<html><u>APG</u></html>"));
        panel.add(new JLabel("<html><u>RPG</u></html>"));
        panel.add(new JLabel("<html><u>MPG</u></html>"));
       
        DecimalFormat df = new DecimalFormat("0.0");
       
        for (int i = 0; i <= 13; i++)
        {
            String[] result = teamMember[i].split(",");
            panel.add(new JLabel(result[1]));
            panel.add(new JLabel(result[0]));
            panel.add(new JLabel(df.format(Double.parseDouble(result[2]))));
            panel.add(new JLabel(df.format(Double.parseDouble(result[3]))));
            panel.add(new JLabel(df.format(Double.parseDouble(result[4]))));
            panel.add(new JLabel(df.format(Double.parseDouble(result[5]))));
        }
       
        this.getContentPane().add(panel, BorderLayout.CENTER);
    }
0
 
objectsCommented:
 this.getContentPane().add(panel, BorderLayout.CENTER);
  ((JComponent)this.getContentPane()).revalidate();
0
 
mm7627Author Commented:
Hmm, that didn't work.  What's weird is, I have menus and then radiobuttons, with a submit button, and if I choose something different, then go to the menu and run it, it only sorts it by the first if statement, NAME.
0
 
objectsCommented:
check the action name. you don't set a default so the sort will be whatever it was previously if it doesn't match anything.
0
 
mm7627Author Commented:
Ok, I made a default.  Still nothing.  I'm just confused about how the JOptionPane causes the thing to work flawlessly.
0
 
objectsCommented:
try a revalidate() and a repaint()
0
 
mm7627Author Commented:
Calling both just causes it to just mess up.  Is there a better way to test which menu item was selected without having to create a new actionlistener for EACH menu item?
0
 
objectsCommented:
> Calling both just causes it to just mess up.

well it definitely should not do that. Sounds like theirs a problem with your layout
how exactly does it mess up.

> Is there a better way to test which menu item was selected without having to create a new actionlistener for EACH menu item?

you could create an ActionListener that stored algorith and sort by as member vars.
But I don't think its going to fix your problem.
0
 
mm7627Author Commented:
By messed up, I should say it doesn't repaint at all.  Not even with the radiobuttons and submit button that work.
....
Yeah, I don't think so either.  Was just a though.
0
 
objectsCommented:
any errors?
0
 
mm7627Author Commented:
java.lang.IllegalThreadStateException


it's a run-time error
0
 
objectsCommented:
can you post the stack trace
0
 
mm7627Author Commented:
Ok, I tried to run it again to get the stack trace, and it didn't show up.  So, I guess it doesn't give an error anymore.

this.getContentPane().add(panel, BorderLayout.CENTER);
((JComponent)this.getContentPane()).revalidate();
((JComponent)this.getContentPane()).repaint();


... If I take out .repaint();  it redraws the panel perfectly when I hit submit.  But once I add that in, it never redraws.
0
 
objectsCommented:
thats suggests theres something wrong somewhere.
do u have any custom painting?
0
 
mm7627Author Commented:
Nah man, I just have three panels added to the main ContentPane.
0
 
objectsCommented:
if you can put together a small compilable example that reproduces the problem I can try it here for you.
0
 
mm7627Author Commented:
I condensed the program considerably and threw it all in one java file.  It doesn't work perfect, but you should be able to see my error.

import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class NewClass extends JFrame implements ActionListener
{
    private String[] teamMember;
    private JMenu fileMenu;
    private JMenu sortMenu;
   
    public NewClass(String[] aMember)
    {
        teamMember = aMember;
        setSize     (500,700);
        setResizable(false);
        setLocation (10,10);
       
        createFileMenu();
        createSortMenu();
       
        createSortInfoPanel();
        createPlayerInfoPanel();
        createStringInfoPanel();
       
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);
        menuBar.add(sortMenu);
       
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }
   
    private void sortSelectNameIncAlgo()
    {
        int min;
        String temp;
        for (int start = 0; start <= teamMember.length - 2; start++)
        {
            min = start;
            for (int j = start; j <= teamMember.length - 1; j++)
            {
                String[] result = teamMember[j].split(",");
                String[] result2 = teamMember[min].split(",");
                if (result[1].compareTo(result2[1])<0)
                {
                    min = j;
                }
            }
            temp = teamMember[start];
            teamMember[start] = teamMember[min];
            teamMember[min] = temp;
        }
    }
   
    private void sortSelectPPGIncAlgo()
    {
        int min;
        String temp;
        for (int start = 0; start <= teamMember.length - 2; start++)
        {
            min = start;
            for (int j = start; j <= teamMember.length - 1; j++)
            {
                String[] result = teamMember[j].split(",");
                String[] result2 = teamMember[min].split(",");
                if (Double.parseDouble(result[2]) > Double.parseDouble(result2[2]))
                {
                    min = j;
                }
            }
            temp = teamMember[start];
            teamMember[start] = teamMember[min];
            teamMember[min] = temp;
        }
    }
   
    private void createSortInfoPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0,1));
        panel.add(sortByPanel());
        panel.add(sortOptionPanel());
        this.getContentPane().add(panel, BorderLayout.NORTH);
    }
   
    private JRadioButton nameButton;
    private JRadioButton ppgButton;
    private JRadioButton selectionButton;
    private JRadioButton incButton;
   
   
    private JPanel sortByPanel()
    {
        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Sort By"));
        panel.setLayout(new GridLayout(1,5));
        nameButton = new JRadioButton("Name");
        nameButton.addItemListener(new sortByListener());
        ppgButton = new JRadioButton("PPG");
        ppgButton.addItemListener(new sortByListener());
       
        ButtonGroup theGroup = new ButtonGroup();
       
        nameButton.setSelected(true);
       
        theGroup.add(nameButton);
        theGroup.add(ppgButton);
       
        panel.add(nameButton);
        panel.add(ppgButton);
       
        return panel;
    }
   
    private JPanel sortOptionPanel()
    {
        JPanel panel = new JPanel();
       
        JPanel sortAlgo = new JPanel();
        JPanel sortOrder = new JPanel();
       
        JButton submit = new JButton("Submit");
       
        submit.addActionListener(new submitButtonListener());
       
        selectionButton = new JRadioButton("Selection");
        incButton = new JRadioButton("Increasing");
       
        selectionButton.addItemListener(new sortAlgoListener());
        incButton.addItemListener(new sortTypeListener());
       
        ButtonGroup algoGroup = new ButtonGroup();
        ButtonGroup sortGroup = new ButtonGroup();
       
        algoGroup.add(selectionButton);
       
        sortGroup.add(incButton);
       
        selectionButton.setSelected(true);
        incButton.setSelected(true);
       
        sortAlgo.setBorder(new TitledBorder(new EtchedBorder(), "Sort Algorithm"));
        sortOrder.setBorder(new TitledBorder(new EtchedBorder(), "Sort Ordering"));
       
        sortAlgo.add(selectionButton);
       
        sortOrder.add(incButton);
       
        panel.add(sortAlgo);
        panel.add(sortOrder);
        panel.add(submit);
       
        return panel;
    }
   
    private void createPlayerInfoPanel()
    {
        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Player Information"));
       
        panel.setLayout(new GridLayout(6,3));
       
        panel.add(new JLabel("<html><u>Last Name</u></html>"));
        panel.add(new JLabel("<html><u>First Name</u></html>"));
        panel.add(new JLabel("<html><u>PPG</u></html>"));
       
        DecimalFormat df = new DecimalFormat("0.0");
       
        for (int i = 0; i <= teamMember.length-1; i++)
        {
            String[] result = teamMember[i].split(",");
            panel.add(new JLabel(result[1]));
            panel.add(new JLabel(result[0]));
            panel.add(new JLabel(df.format(Double.parseDouble(result[2]))));
        }
       
        this.getContentPane().add(panel, BorderLayout.CENTER);
        //((JComponent)this.getContentPane()).revalidate();
        //((JComponent)this.getContentPane()).repaint();
    }
   
    private JLabel bottomMessage;
   
    private void createStringInfoPanel()
    {
        JPanel panel = new JPanel();
        bottomMessage = new JLabel("Info sorted by NAME INCREASING using SELECTION SORT");
        panel.add(bottomMessage);
        this.getContentPane().add(panel, BorderLayout.SOUTH);
    }
   
    private void createFileMenu()
    {
        JMenuItem item;
       
        fileMenu = new JMenu("File");
       
        item = new JMenuItem("Quit");
        item.addActionListener(this);
        fileMenu.add(item);
    }
   
    private void createSortMenu()
    {
        JMenuItem item;
       
        sortMenu = new JMenu("Sort");
       
        JMenuItem selectionMenu = new JMenu("Selection Sort");
        item = new JMenuItem("Name");
        item.addActionListener(new selectionMenuListener());
        selectionMenu.add(item);
        item = new JMenuItem("PPG");
        item.addActionListener(new selectionMenuListener());
        selectionMenu.add(item);
       
        sortMenu.add(selectionMenu);
    }
    private String sortByString,algoSortString,typeSortString;
    class sortByListener implements ItemListener
    {
        public void itemStateChanged(ItemEvent event)
        {
            if (nameButton.isSelected())
                sortByString = "NAME";
            if (ppgButton.isSelected())
                sortByString = "PPG";
        }
    }
   
    class sortAlgoListener implements ItemListener
    {
        public void itemStateChanged(ItemEvent event)
        {
            if (selectionButton.isSelected())
                algoSortString = "SELECTION";
        }
    }
   
    class sortTypeListener implements ItemListener
    {
        public void itemStateChanged(ItemEvent event)
        {
            if (incButton.isSelected())
                typeSortString = "INCREASING";
        }
    }
   
    class submitButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {            
            if (sortByString.equals("NAME") && typeSortString.equals("INCREASING") && algoSortString.equals("SELECTION"))
                sortSelectNameIncAlgo();
            if (sortByString.equals("PPG") && typeSortString.equals("INCREASING") && algoSortString.equals("SELECTION"))
                sortSelectPPGIncAlgo();
           
            createPlayerInfoPanel();
            bottomMessage.setText("Info sorted by " + sortByString + " " + typeSortString + " using " + algoSortString + " SORT");
        }
    }

    class selectionMenuListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String menuName = event.getActionCommand();
            // THIS IS FOR SELECTION SORT ALWAYS [INCREASING]

            if (menuName.equals("Name"))
            {
                sortSelectNameIncAlgo();
                sortByString = "NAME";
            }
            else if (menuName.equals("PPG"))
            {
                sortSelectPPGIncAlgo();
                sortByString = "PPG";
            }
           
            typeSortString = "INCREASING";
            algoSortString = "SELECTION";
           
            createPlayerInfoPanel();
            bottomMessage.setText("Info sorted by " + sortByString + " " + typeSortString + " using " + algoSortString + " SORT");
        }
    }

    public static void main(String[] args)
    {
        String[] teamMember = null;
        teamMember = new String[5];
       
            teamMember[0] = "Katie,Gearlds,15.6,3.4,3.9,35.6";
            teamMember[1] = "Aya,Traore,13.0,2.3,4.9,30.5";
            teamMember[2] = "Lindsay,Wisdom,10.1,1.25,5.6,25.9";
            teamMember[3] = "Erin,Lawless,9.7,1.07,3.8,25.8";
            teamMember[4] = "Sharika,Webb,4.8,3.43,3.0,26";
            NewClass bTeam = new NewClass(teamMember);
            bTeam.setVisible(true);
    }
   
    public void actionPerformed(ActionEvent event)
    {
        String menuName = event.getActionCommand();
       
        if (menuName.equals("Quit"))
            System.exit(0);
    }
   
}
0
 
mm7627Author Commented:
I'll up the point value, this has been quite the annoyance.
0
 
mm7627Author Commented:
This being the problem I am having.  :)
0
 
objectsCommented:
you need to first remove the existing panel, something like:

    JPanel panel;
   
    private void createPlayerInfoPanel()
    {
          if (panel!=null) this.getContentPane().remove(panel);
          
        panel = new JPanel();
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Player Information"));
       
        panel.setLayout(new GridLayout(6,3));
       
        panel.add(new JLabel("<html><u>Last Name</u></html>"));
        panel.add(new JLabel("<html><u>First Name</u></html>"));
        panel.add(new JLabel("<html><u>PPG</u></html>"));
       
        DecimalFormat df = new DecimalFormat("0.0");
       
        for (int i = 0; i <= teamMember.length-1; i++)
        {
            String[] result = teamMember[i].split(",");
            panel.add(new JLabel(result[1]));
            panel.add(new JLabel(result[0]));
            panel.add(new JLabel(df.format(Double.parseDouble(result[2]))));
        }
       
        this.getContentPane().add(panel, BorderLayout.CENTER);
        ((JComponent)this.getContentPane()).revalidate();
        ((JComponent)this.getContentPane()).repaint();
    }
0
 
objectsCommented:
Or alternately you could leave the panel and and instead clear and replaces its content.
0
 
mm7627Author Commented:
thanks alot.  that worked perfect.
0
 
objectsCommented:
no worries :)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 14
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now