-3

I would like to build a JMenu using the content of a text file with this layout / appearance (# for separator ; JMenu X and JMenuItem X will be replaced by String sequences) :

JMenu 1 # JMenuItem 1
JMenu 1 # JMenuItem 2
JMenu 1 # JMenuItem 3
JMenu 2 # JMenuItem 1
JMenu 2 # JMenuItem 2
JMenu 3 # JMenuItem 1
JMenu 3 # JMenuItem 2
JMenu 3 # JMenuItem 3
JMenu 3 # JMenuItem 4

How can i write a loop that will print the content of the file into the each of the appropriate menu items?

Thanks for your support.

D3181
  • 2,037
  • 5
  • 19
  • 44
Fred
  • 169
  • 1
  • 3
  • 19
  • I'm just on sheet of paper before coding because I have no idea for this loop. I thought use FileInputStream and BufferedInputStream to read the file and use HashMap to put strings with JMenu as keys and JMenuItem as values. Then, read my HashMap to build the menu. – Fred Aug 22 '16 at 20:34
  • Ok. Try that. If you run into any errors, ask again. – bradimus Aug 22 '16 at 20:35
  • Of course! I'm just searching for ideas and tracks. – Fred Aug 22 '16 at 20:35
  • I tried to do this part of code... But I'm stuck. – Fred Aug 22 '16 at 21:40
  • You can improve your question. Please read [How to Ask](http://stackoverflow.com/help/how-to-ask) including the link "How to ask questions the smart way." – zhon Aug 23 '16 at 01:42

1 Answers1

0

Here is the loop that works :

      FileReader monFichier = null;
      BufferedReader tampon = null;
      ArrayList<JMenu> tJMenu = new ArrayList<JMenu>();

      try {
        monFichier = new FileReader("param/tableMenu.bat");
        // Connecte les flux :
        tampon = new BufferedReader(monFichier);
        while (true) {
          // Lit une ligne du fichier :
          String ligne = tampon.readLine();
          // Vérifie la fin de fichier
          if (ligne == null)
            break;
          //On récupère la position du "#" :
          int pos = ligne.indexOf("#");
          //On renseigne nos tableaux :
          boolean existant = false;
          for (JMenu m : tJMenu)
          {
              if (m.getText().equals(ligne.substring(0, pos-1)))
              {
                  m.add(new JMenuItem(ligne.substring(pos+2, ligne.length())));
                  existant = true;
              }
          }
          if (!existant)
          {
            tJMenu.add(new JMenu(ligne.substring(0, pos-1)));
            for (JMenu m : tJMenu)
          {
              if (m.getText().equals(ligne.substring(0, pos-1)))
              {
                  m.add(new JMenuItem(ligne.substring(pos+2, ligne.length())));
              }
          }
          }
        } // Fin du while

        JMenuBar menu = new JMenuBar();
        JFrame f = new JFrame();
        JPanel p = new JPanel();
        f.setContentPane(p);
        f.setJMenuBar(menu);
        f.setSize(600, 800);
        f.setVisible(true);

        for (JMenu m : tJMenu)
          {
              menu.add(m);
          }

      } catch (IOException exception) { 
        exception.printStackTrace();
      } finally {  
        try {
            tampon.close();
            monFichier.close();
        } catch(IOException exception1) {
          exception1.printStackTrace();
        }   
      }

And here is the text file :

JMenu 1 # JMenuItem 1.1
JMenu 1 # JMenuItem 2.1
JMenu 1 # JMenuItem 3.1
JMenu 2 # JMenuItem 1.2
JMenu 2 # JMenuItem 2.2
JMenu 3 # JMenuItem 1.3
JMenu 3 # JMenuItem 2.3
JMenu 3 # JMenuItem 3.3
JMenu 3 # JMenuItem 4.3
Fred
  • 169
  • 1
  • 3
  • 19