El tema es que, usando esos gets, tampoco me permite acceder a otros componentes de la pantalla...
Cómo lo hago?
Aquí dejo el código:
Código:
package Consulta; import java.io.*; import java.awt.*; import java.awt.event.*; import java.io.FileOutputStream; import javax.swing.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import com.mysql.jdbc.StringUtils; import com.sun.org.apache.xml.internal.serialize.LineSeparator; import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.XMLSerializer; public class MenuItemListener implements ActionListener{ public void actionPerformed(ActionEvent a) { JMenuItem item= (JMenuItem) a.getSource(); //forma 1 /*JMenu m =(JMenu)(item.getParent()); JMenuBar mb=(JMenuBar)(m.getParent()); JFrame f=(JFrame)(mb.getParent());*/ //forma 2 Container p= (Container)item.getParent(); JPopupMenu pop=(JPopupMenu)item.getParent(); Component invoker=pop.getInvoker(); JComponent component=(JComponent)invoker; Container top=(Container)component.getTopLevelAncestor(); //JMenuBar menubar=(JMenuBar)component.getParent(); //Component invokerdemenu=(menubar.getParent()); //System.out.println("menubar parent---"+(((menubar.getParent()).getParent()).getParent()).getClass().getName()); /* * * System.out.println("container---"+top.getClass().getName()); System.out.println("invoker---"+invoker.getClass().getName()); System.out.println("compp---"+component.getClass().getName()); System.out.println("top---"+top.getClass().getName());*/ //System.out.println(item.getParent().getClass().getName()); //JMenu menu= (JMenu)item.getParent();//principal //JMenuBar bar=(JMenuBar)menu.getParent(); //JFrame f=(JFrame)bar.getParent(); //Container p2=(Container)bar.getParent(); //JTabbedPane pes=(JTabbedPane).getParent();//contenedores de pestañas //FALTA UBICAR EL FIELD CON LA RUTA //JFrame frame= (JFrame) a.getSource(); //Container p2= (Container)frame.getParent(); String ite=item.getLabel(); if(ite.equals("Opcion 1")) { System.out.println("OPCION 1 ACTIVADA"); } if(ite.equals("Opcion 2")) { System.out.println("OPCION 2 ACTIVADA"); } if(ite.equals("Opcion 3")) //Ok { System.out.println("OPCION 3 ACTIVADA"); }//cierra if }//cierra action }
Código:
package Consulta; import java.awt.BorderLayout; import java.awt.Rectangle; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Vector; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSeparator; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.WindowConstants; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.SwingUtilities; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class PantallaPrincipal extends javax.swing.JFrame { private JTable tablasectores; private JLabel pestania; private JLabel pestania2; private JPanel jPanel1; private JTextField jTextField1; private JTree jTree; private JTabbedPane jTabbedPane1; private JMenuItem jMenuItem7; private JMenuItem jMenuItem6; private JMenuItem jMenuItem5; private JMenu jMenu2; private JMenuBar jMenuBar1; private JPanel pest1; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { PantallaPrincipal inst = new PantallaPrincipal(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } // public PantallaPrincipal() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); { jMenuBar1 = new JMenuBar(); getContentPane().add(jMenuBar1);// //jMenuBar1.setName("Barra"); setJMenuBar(jMenuBar1); { jMenu2 = new JMenu(); jMenuBar1.add(jMenu2); jMenu2.setText("Opciones"); { jMenuItem5 = new JMenuItem(); jMenu2.add(jMenuItem5); jMenuItem5.setText("Opcion 1"); jMenuItem5.addActionListener(new MenuItemListener()); } { jMenuItem6 = new JMenuItem(); jMenu2.add(jMenuItem6); jMenuItem6.setText("Opcion 2"); jMenuItem6.addActionListener(new MenuItemListener()); } { jMenuItem7 = new JMenuItem(); jMenu2.add(jMenuItem7); jMenuItem7.setText("Opcion 3"); jMenuItem7.addActionListener(new MenuItemListener()); } } } { jTabbedPane1 = new JTabbedPane(); getContentPane().add(jTabbedPane1); jTabbedPane1.setBounds(182, 19, 673, 348); jTabbedPane1.setName("Pestaña");//componente 3 { jPanel1 = new JPanel(); jTabbedPane1.addTab("Pestaña 1", null, jPanel1, null); jPanel1.setPreferredSize(new java.awt.Dimension(452, 400)); jPanel1.setName("pestaña1");//compo 0 jPanel1.setLayout(null); JPanel jPanel2 = new JPanel(); jTabbedPane1.addTab("Pestaña 2", null, jPanel2, null); jPanel2.setLayout(null); { JLabel jLabel2 = new JLabel(); jPanel2.add(jLabel2); jLabel2.setText("Prueba:"); jLabel2.setBounds(200, 13, 303, 16); } { jTextField1 = new JTextField(); jPanel2.add(jTextField1); jTextField1.setText("jTextField1"); jTextField1.setBounds(246, 197, 62, 23); } } } pack(); this.setSize(877, 627); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } }