Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Cannot impliment the menu bar

  1. #1
    newguy11 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Cannot impliment the menu bar

    Hi Guys
    I really need some help with this project as it is driving me insane now! I need to impliment a menu bar on to the top of the jframe that says file. When the menu is opened the 3 options read: Load data, save data, and exit. i have tried to do this but i am having no luck. Can you guys tell me what im doing wrong and how to fix it please?
    Here is all the code so far:
    IOmanager class:
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class InputOutputManager
    {
        private ArrayList<Team> teams;
        private ArrayList<Player> players;
    
        public InputOutputManager()
        {
            teams = new ArrayList<Team>();
            players = new ArrayList<Player>();
        }
    
        public ArrayList<Team> getTeams()
        {
            return teams;
        }
    
        public ArrayList<Player> getPlayers()
        {
            return players;
        }
    
        public void readDataFromFile() throws Exception
        {
            try
            {
                Scanner inFile = new Scanner(new File("DataFile.txt"));
                inputTeams(inFile);
                inputPlayers(inFile, getTeams());
                inFile.close();
            }
            catch (FileNotFoundException fnfe)
            {
                throw new Exception("The data file was not available for input");
            }
        }
    
        private void inputTeams(Scanner inFile)
        {
            teams = new ArrayList<Team>();
    
            String line = inFile.nextLine();
            line = inFile.nextLine();
    
            while (!line.equalsIgnoreCase("Players"))
            {
                String[] part = line.split(":");
    
                String name = part[0];
                int teamSize = Integer.parseInt(part[1]);
    
                Team t = new Team(name, teamSize);
                getTeams().add(t);
                line = inFile.nextLine();
            }
        }
    
        private void inputPlayers(Scanner inFile, ArrayList<Team> teams)
        {
            players = new ArrayList<Player>();
    
            while (inFile.hasNext())
            {
                String line = inFile.nextLine();
                String[] names = line.split(":");
    
                Player p = new Player(names[0]);
                getPlayers().add(p);
    
                if (names.length == 2)
                {
                    Team t = findTeam(names[1], teams);
                    t.addPlayer(p);
                }
            }
        }
    
        private Team findTeam(String teamName, ArrayList<Team> teams)
        {
            Team t = null;
            boolean found = false;
    
            for (int i = 0; !found && i < teams.size(); i++)
            {
                if (teams.get(i).getName().equalsIgnoreCase(teamName))
                {
                    found = true;
                    t = teams.get(i);
                }
            }
    
            return t;
        }
    
        public void writeDataToFile() throws Exception
        {
            PrintWriter outFile = new PrintWriter("DataFile.txt");
            outputTeams(outFile);
            outputPlayers(outFile);
            outFile.close();
        }
    
        private void outputPlayers(PrintWriter outFile)
        {
            outFile.println("Players");
            for (Player p : getPlayers())
            {
                outFile.println(p.getName() + ":" + (p.getTeam() == null ? "" : p.getTeam().getName()));
            }
        }
    
        private void outputTeams(PrintWriter outFile)
        {
            outFile.println("Teams");
            for (Team t : getTeams())
            {
                outFile.println(t.getName() + ":" + t.getRequiredNumberOfPlayers());
            }
        }
    }
    Player Class:
    Java Code:
    public class Player
    {
        private String name;
        private Team team;
        
        public Player(String name)
        {
            this.name = name;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public Team getTeam()
        {
            return team;
        }
    
        public void setTeam(Team team)
        {
            this.team = team;
        }
        
        @Override
        public String toString()
        {
            return getName();
        }
    }
    Team Class:
    Java Code:
    import java.util.ArrayList;
    
    public class Team
    {
        private String name;
        private int requiredNumberOfPlayers;
        private ArrayList<Player> players;
        
        public Team(String name, int requiredNumberOfPlayers)
        {
            this.name = name;
            this.requiredNumberOfPlayers = requiredNumberOfPlayers;
            players = new ArrayList<Player>(requiredNumberOfPlayers);
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public int getRequiredNumberOfPlayers()
        {
            return requiredNumberOfPlayers;
        }
    
        public boolean addPlayer(Player p)
        {
            boolean playerAdded = false;
            
            if (players.size() < requiredNumberOfPlayers &&
                !players.contains(p))
            {
                playerAdded = players.add(p);
                p.setTeam(this);
                
            }
            return playerAdded;
        }
        
        public void removePlayer(Player p)
        {
            if (players.remove(p))
            {
                p.setTeam(null);
            }
        }
        
        public ArrayList<Player> getPlayers()
        {
            return players;
        }
        
        public boolean canCompete()
        {
            return players.size() == requiredNumberOfPlayers;
        }
        
        @Override
        public String toString()
        {
            return getName();
        }
    }
    Team and player application class:
    Java Code:
    public class TeamAndPlayerApplication
    {
        public static void main(String args[])
        {
            try
            {
                new TeamAndPlayerJFrame();
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
    Team and player j frame:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class TeamAndPlayerJFrame extends JFrame implements ActionListener, ListSelectionListener, WindowListener
    {
        InputOutputManager ioMgr;
        ArrayList<Team> teams;
        JList teamList;
        ArrayList<Player> players;
        JList playerList;
        JButton reset;
        private JMenuItem loadData, saveData;
        private JMenu viewMenu;
        
        public TeamAndPlayerJFrame() throws Exception
        {
            super("GDM's Team and Player");
            setSize(400, 300);
            setLocation(200, 200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            addWindowListener(this);
    
            ioMgr = new InputOutputManager();
            inputData();
    
            setupGUI();
    
            setVisible(true);
        }
    
        private void inputData() throws Exception
        {
            ioMgr.readDataFromFile();
            teams = ioMgr.getTeams();
            players = ioMgr.getPlayers();
        }
    
        private void outputData()
        {
                try
                {
                    ioMgr.writeDataToFile();
                }
                catch (Exception ex)
                {
                    JOptionPane.showMessageDialog(this, ex.getMessage(), "Save error", JOptionPane.ERROR_MESSAGE);
                }
        }
    
        private void setupGUI()
        {
            JPanel listPanel = setupListPanel();
            JPanel buttonPanel = setupButtonPanel();
    
            getContentPane().setBackground(new Color(0xfedcba));
            getContentPane().add(listPanel, BorderLayout.CENTER);
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        }
    
        private JPanel setupButtonPanel()
        {
            reset = new JButton("Reset");
            reset.addActionListener(this);
    
            JPanel p = new JPanel();
            p.setOpaque(false);
            p.add(reset);
            return p;
        }
    
        private JPanel setupListPanel()
        {
            JPanel teamPanel = setupTeamPanel();
            JPanel playerPanel = setupPlayerPanel();
    
            JPanel p = new JPanel(new GridLayout(1, 2, 20, 0));
            p.setOpaque(false);
            p.add(teamPanel);
            p.add(playerPanel);
            return p;
        }
    
        private JPanel setupTeamPanel()
        {
            teamList = new JList(teams.toArray());
            teamList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            teamList.addListSelectionListener(this);
            JScrollPane jsp = new JScrollPane(teamList);
    
            JPanel p = new JPanel(new BorderLayout());
            p.setOpaque(false);
            p.add(new JLabel("Teams"), BorderLayout.NORTH);
            p.add(jsp, BorderLayout.CENTER);
    
            return p;
        }
    
        private JPanel setupPlayerPanel()
        {
            playerList = new JList(players.toArray());
            playerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            playerList.addListSelectionListener(this);
            JScrollPane jsp = new JScrollPane(playerList);
    
            JPanel p = new JPanel(new BorderLayout());
            p.setOpaque(false);
            p.add(new JLabel("Players"), BorderLayout.NORTH);
            p.add(jsp, BorderLayout.CENTER);
    
            return p;
        }
    
        @Override
        public void actionPerformed(ActionEvent e)
        {
            teamList.setListData(teams.toArray());
            playerList.setListData(players.toArray());
        }
    
        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            if (!e.getValueIsAdjusting())
            {
                if (e.getSource() == teamList)
                {
                    if (!teamList.isSelectionEmpty())
                    {
                        displayPlayers((Team) teamList.getSelectedValue());
                    }
                }
                else
                {
                    if (!playerList.isSelectionEmpty())
                    {
                        displayTeam((Player) playerList.getSelectedValue());
                    }
                }
            }
        }
    
        private void displayPlayers(Team t)
        {
            playerList.setListData(t.getPlayers().toArray());
        }
    
        private void displayTeam(Player p)
        {
            Team[] t =
            {
                p.getTeam()
            };
            teamList.setListData(t);
        }
        
        private JMenuBar setupMenuBar()
        {
            JMenu appMenu = setupApplicationMenu();
            viewMenu = setupViewMenu();
    
            JMenuBar bar = new JMenuBar();
            bar.add(appMenu);
            bar.add(viewMenu);
            return bar;
        }
        
        private JMenu setupViewMenu()
        {
            viewCraftList = setupViewCraftListMenu();
    
            JMenu menu = new JMenu("View");
            menu.setMnemonic(KeyEvent.VK_V);
            menu.add(viewCraftList);
            return menu;
        }
        
        private JMenu setupViewCraftListMenu()
        {
            ArrayList<SpaceCraft> spaceships = spaceshipCanvas.getAllSpaceCraft();
    
            JMenu menu = new JMenu("Craft");
            menu.setMnemonic(KeyEvent.VK_C);
    
            for (SpaceCraft s : spaceships)
            {
                JMenuItem jmi = new JMenuItem(s.getName());
                jmi.addActionListener(this);
                menu.add(jmi);
            }
    
            return menu;
        }
        
        private JMenu setupApplicationMenu()
        {
            growApp = new JMenuItem("Grow");
            growApp.setMnemonic(KeyEvent.VK_G);
            growApp.addActionListener(this);
            KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.ALT_MASK);
            growApp.setAccelerator(key);
    
            shrinkApp = new JMenuItem("Shrink");
            shrinkApp.setMnemonic(KeyEvent.VK_S);
            shrinkApp.addActionListener(this);
            key = KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK);
            shrinkApp.setAccelerator(key);
    
            exitApp = new JMenuItem("Exit");
            exitApp.setMnemonic(KeyEvent.VK_X);
            exitApp.addActionListener(this);
            key = KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK);
            exitApp.setAccelerator(key);
    
            JMenu menu = new JMenu("Application");
            menu.setMnemonic(KeyEvent.VK_P);
            menu.add(growApp);
            menu.add(shrinkApp);
            menu.addSeparator();
            menu.add(exitApp);
            return menu;
        }
    
    
        @Override
        public void windowOpened(WindowEvent e){}
        @Override
        public void windowClosed(WindowEvent e){}
        @Override
        public void windowActivated(WindowEvent e){}
        @Override
        public void windowDeactivated(WindowEvent e){}
        @Override
        public void windowIconified(WindowEvent e){}
        @Override
        public void windowDeiconified(WindowEvent e){}
        @Override
        public void windowClosing(WindowEvent e)
        {
            outputData();
        }
    }
    Thanks guys for your help

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Cannot impliment the menu bar

    Nowhere in all that code you call your setupMenuBar() method so no menubar is created and put in your JFrame...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    newguy11 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Cannot impliment the menu bar

    Thanks for the reply.
    How would i call the setupMenuBar() method so that it is created?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Cannot impliment the menu bar

    Quote Originally Posted by newguy11 View Post
    Thanks for the reply.
    How would i call the setupMenuBar() method so that it is created?
    I don't know; call that method in the part of your code where you're decorating your JFrame and before you set it to visible.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    newguy11 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Cannot impliment the menu bar

    Thank you so much for your help. I finally managed to crack it. You were right it wasnt being called.

    regards

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Cannot impliment the menu bar

    Quote Originally Posted by newguy11 View Post
    Thank you so much for your help. I finally managed to crack it. You were right it wasnt being called.
    It was easy; I saw all that code and though OMG and made my browser search for 'menubar' and found that method. Then I made my browser search for that method name and it couldn't find it anywhere ...

    kind regards,

    Jos
    k1ng likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Cannot impliment the menu bar

    For future reference: To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Not reams of irrelevant code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2012, 12:41 PM
  2. how to impliment minimize button in swing
    By prasad.vara in forum AWT / Swing
    Replies: 1
    Last Post: 07-20-2010, 09:34 AM
  3. Fill a menu dynamically when menu is shown
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •