Foros del Web » Programación para mayores de 30 ;) » Java »

Como llegar de JMenuItem a JFrame

Estas en el tema de Como llegar de JMenuItem a JFrame en el foro de Java en Foros del Web. Hola! Tengo el siguiente problema: Quiero acceder al JFrame de mi programa para (a su vez)acceder a un componente que esta en una pestaña de ...
  #1 (permalink)  
Antiguo 31/12/2013, 03:41
 
Fecha de Ingreso: diciembre-2013
Mensajes: 3
Antigüedad: 10 años, 3 meses
Puntos: 0
Como llegar de JMenuItem a JFrame

Hola! Tengo el siguiente problema: Quiero acceder al JFrame de mi programa para (a su vez)acceder a un componente que esta en una pestaña de mi programa y a algún futuro componente que este dentro del frame pero fuera de las pestañas. El tema es que al hacer getParent() al menú item (ver el MenuItemListener), muestra otra cosa, a menos que use los métodos getInvoker() y getTopLevelAncestor()...
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();
		}
	}
	
}
  #2 (permalink)  
Antiguo 04/01/2014, 17:14
Avatar de HackmanC  
Fecha de Ingreso: enero-2008
Ubicación: Guatemala
Mensajes: 1.817
Antigüedad: 16 años, 2 meses
Puntos: 260
Sonrisa Respuesta: Como llegar de JMenuItem a JFrame

Hola,

Posiblemente,


Código Java:
Ver original
  1. package desktop.menu;
  2.  
  3. public class JMyMenuItem extends javax.swing.JMenuItem {
  4.  
  5.     private static final long serialVersionUID = 1L;
  6.  
  7.     private JFrame jFrame;
  8.  
  9.     public JJFrameMenuItem(JFrame jFrame) {
  10.         super();
  11.         this.jFrame = jFrame;
  12.     }
  13.  
  14.     public JFrame getJFrame() {
  15.         return jFrame;
  16.     }
  17.  
  18. }

Código Java:
Ver original
  1. public void actionPerformed(ActionEvent a)
  2.         {
  3.         if (a.getSource() instanceof desktop.menu.JMyMenuItem) {
  4.             JFrame theFrame = ((desktop.menu.JMyMenuItem) a.getSource()).getFrame();
  5.         }

Código Java:
Ver original
  1. jMenuItem5 = new JMyMenuItem(this);
  2.                         jMenu2.add(jMenuItem5);
  3.                         jMenuItem5.setText("Opcion 1");
  4.                         jMenuItem5.addActionListener(new MenuItemListener());

Saludos,

Etiquetas: jframe, programa, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:47.