Results 1 to 2 of 2
  1. #1
    jecarfor is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default How to obtain text from Selected Menus

    Hi there!!!

    I need to get the current selection path from a JMenuItem, but i need this information to show as a String, let me give you an example (just an example no)

    Let's say my JMenuBar with the following JMenus

    -2006
    -2007

    and I added some JMenus to 2007 Menu,

    -French
    -German

    so now I have

    -2006
    -2007-French
    -German

    then I finally added the JMenuItems to French

    -Level 1
    -Level 2

    So my whole JmenuBar has the following menus

    -2006
    -2007-French
    -German-Level 1
    -Level 2

    So, if I Select Level 2 i want to get the selection path "2007 German Level 1" and show the selection as the JFrame Title.

    I show you my code (not the whole code) as example of the program I'm bulding. I explained all this because some parts of my code are in Spanish

    The Title of the jFrame I want to set is jfSelection

    Java Code:
    public class saberpro implements ActionListener{
    
        //Creación Frame
        JFrame jfSaberPro = new JFrame("Saber Pro");
        JFrame jfSelection;
    
        //Creación JPanel
        JPanel jpSaberPro = new JPanel();
    
        //Creacion String de Titulo Frame Selection
        String StringField;
    
        //Años en Arrays
        JMenuItem jmiYear[][] = new JMenuItem[16][6];
    
        //Creacion JTextFields
        JTextField
            jtfPart = new JTextField(15),
            jtfProg = new JTextField(18),
            jtfPercent = new JTextField(13),
            jtfResult = new JTextField(15);
    
    private void pvElements(){
    
        //JMenu en Array
        JMenu jmArea[] = new JMenu[8];
    
        //Creación JMenus Principales
        jmArea[0]= new JMenu("Matemáticas");jmArea[0].setMnemonic(KeyEvent.VK_M);
        jmArea[1] = new JMenu("Algoritmos y Estructura de Datos");jmArea[1].setMnemonic(KeyEvent.VK_A);
        jmArea[2] = new JMenu("Bases de Datos");jmArea[2].setMnemonic(KeyEvent.VK_B);
        jmArea[3] = new JMenu("Ingenieria de Software");jmArea[3].setMnemonic(KeyEvent.VK_S);
        jmArea[4] = new JMenu("Redes");jmArea[4].setMnemonic(KeyEvent.VK_R);
        jmArea[5] = new JMenu("Ética Social");jmArea[5].setMnemonic(KeyEvent.VK_T);
        jmArea[6] = new JMenu("Introducción a Computadores");jmArea[6].setMnemonic(KeyEvent.VK_C);
        jmArea[7] = new JMenu("Inglés");jmArea[7].setMnemonic(KeyEvent.VK_I);
    
        //Adición JMenus a JMenuBar a travez de un For Loop
        for(int nIncArea = 0;nIncArea<=7;nIncArea=nIncArea+1){
        jmbSaberPro.add(jmArea[nIncArea]);
        }
    
        //JMenu Sistema y Electronica
        JMenu jmSistema[] = new JMenu[8];
        JMenu jmElectronica[] = new JMenu[8];
    
        //Creación JMenus Sistema travez de un For Loop
        for(int nIncSis = 0;nIncSis<=7;nIncSis=nIncSis+1){
        jmSistema[nIncSis] = new JMenu("Sistema");jmSistema[nIncSis].setMnemonic(KeyEvent.VK_T);}
    
        //Creación JMenus Electronica a travez de un For Loop
        for(int nIncEl = 0;nIncEl<=7;nIncEl=nIncEl+1){
        jmElectronica[nIncEl] = new JMenu("Electronica");jmElectronica[nIncEl].setMnemonic(KeyEvent.VK_E);}
    
        //Adición Sistema y Electronica A Areas a traves de un for Loop
        for(int nIncAdArea = 0;nIncAdArea<=7;nIncAdArea=nIncAdArea+1){
        jmArea[nIncAdArea].add(jmSistema[nIncAdArea]);
        jmArea[nIncAdArea].add(jmElectronica[nIncAdArea]);
        }
    
        //Creación JMenuItems Años
        for(int nYearList=0;nYearList<=15;nYearList = nYearList+1){
        jmiYear[nYearList][0] = new JMenuItem("2005", KeyEvent.VK_5);
        jmiYear[nYearList][1] = new JMenuItem("2006", KeyEvent.VK_6);
        jmiYear[nYearList][2] = new JMenuItem("2007", KeyEvent.VK_7);
        jmiYear[nYearList][3] = new JMenuItem("2009", KeyEvent.VK_9);
        jmiYear[nYearList][4] = new JMenuItem("2010", KeyEvent.VK_1);
        }
        //jmSistema[0].add(jmiYear[0]);
        
        //Addición Años a Sistema y Electronica
        for(int nYearListAdd=0;nYearListAdd<=7;nYearListAdd = nYearListAdd+1)
            for(int nYearItemAdd=0;nYearItemAdd<=4;nYearItemAdd = nYearItemAdd+1){
                jmSistema[nYearListAdd].add(jmiYear[nYearListAdd][nYearItemAdd]);
                jmiYear[nYearListAdd][nYearItemAdd].addActionListener(this);        ;
                jmElectronica[nYearListAdd].add(jmiYear[nYearListAdd+8][nYearItemAdd]);
                jmiYear[nYearListAdd+8][nYearItemAdd].addActionListener(this);
            }
    
        //Adición del JPanel al JFrame
        jfSaberPro.add(jpSaberPro);
    
        //Adición JMenuBar a JPanel
        jpSaberPro.add(jmbSaberPro);
    
        //Tamaño JFrame
        jfSaberPro.setSize(900,200);
    
        //Fijar JFrame y JPanel como visibles y a su vez mostrarlos, Bloquear Redimension
        jfSaberPro.setVisible(true);
        jfSaberPro.setResizable(false);
        jpSaberPro.setVisible(true);
        jfSaberPro.show();
    
        //Cerrar al dar clic en Botón Cerrar
        jfSaberPro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    See the API for MenuSelectionManager.
    Java Code:
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class Breadcrumbs {
    
      JFrame frame = new JFrame();
      MenuSelectionManager manager = MenuSelectionManager.defaultManager();
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new Breadcrumbs().makeUI();
          }
        });
      }
    
      public void makeUI() {
        JMenuItem level1 = new JMenuItem("Level 1");
        JMenuItem level2 = new JMenuItem("Level 2");
        JMenu french = new JMenu("French");
        french.add(level1);
        french.add(level2);
        JMenu german = new JMenu("German");
        JMenu m2007 = new JMenu("2007");
        m2007.add(french);
        m2007.add(german);
        JMenu m2006 = new JMenu("2006");
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(m2006);
        menuBar.add(m2007);
        frame.setJMenuBar(menuBar);
    
        manager.addChangeListener(new ChangeListener() {
    
          @Override
          public void stateChanged(ChangeEvent e) {
            MenuElement[] path = manager.getSelectedPath();
            StringBuilder title = new StringBuilder();
            for (MenuElement element : path) {
              if (element instanceof JMenuItem) {
                title.append(((JMenuItem) element).getText()).append(" ");
              }
            }
            frame.setTitle(title.toString().trim());
          }
        });
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    db

Similar Threads

  1. JQuery Menus
    By anithajerome in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-01-2010, 10:28 AM
  2. Menus Not Displaying
    By kahaj in forum AWT / Swing
    Replies: 12
    Last Post: 09-23-2009, 04:31 AM
  3. Replies: 8
    Last Post: 08-13-2009, 04:23 AM
  4. Replies: 0
    Last Post: 06-25-2008, 10:34 AM
  5. Show Text Fields on combo Box selected value
    By smajidali26 in forum AWT / Swing
    Replies: 0
    Last Post: 11-29-2007, 09:28 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
  •