# Kommunikation zwischen Zwei GUI-Klassen



## till123 (4. Aug 2008)

Hallo,

ich habe zwei Frames(zweite ist eher ein Dialog).
Nun soll aus dem "Dialog" eine Methode aus dem Frame aufgerufen werden.

Hier meine zwei Klassen:
Frame:

```
package gui;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.management.ManagementFactory;

import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.WindowConstants;



public class MainWindow extends javax.swing.JFrame {

	
	private JMenuBar jMenuBar1;
	private JMenu jMenu1;
	private JMenu jMenu2;
	private JMenu jMenu3;
	private JMenu jMenu4;
	private JMenu jMenu5;
	private JMenu jMenu6;
	private JMenuItem jMenuItem1;
	private JMenuItem jMenuItem3;
	private JMenuItem jMenuItem2;
	private JMenuItem jMenuItem4;
	private JMenuItem jMenuItem5;
	private JMenuItem jMenuItem6;
	private int acUserId;

	private AbstractAction chooseUser;
	private AbstractAction chooseTable;
	private AbstractAction chooseDB;
	private AbstractAction Manual;
	private AbstractAction Specification;
	private AbstractAction JavaDoc;
	private interfaceGuiBusinessLogic.InterfaceGBL oneInterfaceGuiBusinessLogic;

	
	public MainWindow(interfaceGuiBusinessLogic.InterfaceGBL oneInterfaceGuiBusinessLogic, int acUserId) {
		this.oneInterfaceGuiBusinessLogic=oneInterfaceGuiBusinessLogic;
		this.acUserId = acUserId;
		initGUI();
	}
	
	private void initGUI() {
		try {


			
			BorderLayout thisLayout = new BorderLayout();
			this.setVisible(true);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Startseite");
			this.setLocation(0,0);

				jMenuBar1 = new JMenuBar();
				setJMenuBar(jMenuBar1);
				{
					jMenu1 = new JMenu();
					jMenuBar1.add(jMenu1);
					jMenu1.setText("FuZZyDB");
					
					jMenu2 = new JMenu();
					jMenuBar1.add(jMenu2);
					jMenu2.setText("Abfragen");
					jMenu2.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jMenu2MouseClicked(evt);
							
						}
					});
				
					jMenu3 = new JMenu();
					jMenuBar1.add(jMenu3);
					jMenu3.setText("Verwalten");
					{
						//this option is for admin only
						//START-->
						jMenuItem1 = new JMenuItem();
						jMenu3.add(jMenuItem1);
						jMenuItem1.setText("Benutzerverwaltung");
						jMenuItem1.setAction(getChooseUser());
						if(	acUserId!=0)  
							jMenuItem1.setEnabled(false);
						//<-- END
						
						jMenuItem2 = new JMenuItem();
						jMenu3.add(jMenuItem2);
						jMenuItem2.setText("Datenbankverwaltung");
						jMenuItem2.setAction(getChooseDB());
						
							System.out.println("Anfang_1");
						jMenuItem6 = new JMenuItem();
							System.out.println("Anfang_2");
						jMenu3.add(jMenuItem6);
							System.out.println("Anfang_3");
						jMenuItem6.setText("Tabellenverwaltung");
							System.out.println("Anfang_4");
						jMenuItem6.setAction(getChooseTable());
							System.out.println("Anfang_5");
						jMenuItem6.setEnabled(false); //it will be true, if one DB is choosen
							System.out.println("Anfang_6");
						
					}
					
					
					jMenu4 = new JMenu();
					jMenuBar1.add(jMenu4);
					jMenu4.setText("Hilfe");	
					{
						jMenuItem4 = new JMenuItem();
						jMenu4.add(jMenuItem4);
						jMenuItem4.setText("Handbuch");
						jMenuItem4.setAction(getManual());
						
						jMenuItem5 = new JMenuItem();
						jMenu4.add(jMenuItem5);
						jMenuItem5.setText("JavaDoc");
						jMenuItem5.setAction(getJavaDoc());
						
						jMenuItem3 = new JMenuItem();
						jMenu4.add(jMenuItem3);
						jMenuItem3.setText("Lastenheft");
						jMenuItem3.setAction(getSpecification());
					}

					jMenu5 = new JMenu();
					jMenuBar1.add(jMenu5);
					jMenu5.setText("Beenden");
					jMenu5.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jMenu1MouseClicked(evt);
							
						}
					});
				
			
			
			
			
			}
			pack();
			this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jMenu1MouseClicked(MouseEvent evt) {
		ExitProgramm oneExit= new ExitProgramm(oneInterfaceGuiBusinessLogic);
		oneExit.setLocationRelativeTo(null);
		oneExit.setSize(400,200);
		oneExit.setVisible(true);
	}
	private void jMenu2MouseClicked(MouseEvent evt) {
		InsertQueries oneQueries= new InsertQueries(oneInterfaceGuiBusinessLogic);
		oneQueries.setLocationRelativeTo(null);
		oneQueries.setSize(950,400);
		oneQueries.setVisible(true);
	}
	
	/**
	 * @return
	 */
	private AbstractAction getChooseUser() {
		if(chooseUser == null) {
			chooseUser = new AbstractAction("Benutzer", null) {
				public void actionPerformed(ActionEvent evt) {
					AdministrateUser oneAdministrateUser= new AdministrateUser(oneInterfaceGuiBusinessLogic);
					oneAdministrateUser.setLocationRelativeTo(null);
					oneAdministrateUser.setSize(950,400);
					oneAdministrateUser.setVisible(true);
				}
			};
		}
		return chooseUser;
	}
	/**
	 * @return
	 */
	private AbstractAction getChooseDB() {
		if(chooseDB == null) {
			chooseDB= new AbstractAction("Datenbanken", null) {
				public void actionPerformed(ActionEvent evt) {
					AdministrateDb oneAdministrateDb= new AdministrateDb(oneInterfaceGuiBusinessLogic, acUserId);
					oneAdministrateDb.setLocationRelativeTo(null);
					oneAdministrateDb.setSize(950,400);
					oneAdministrateDb.setVisible(true);
				}
			};
		}
		return chooseDB;
	}
	
	private AbstractAction getChooseTable() {
		if(chooseTable == null) {
			chooseTable= new AbstractAction("Tabellen", null) {
				public void actionPerformed(ActionEvent evt) {
					AdministrateTable oneAdministrateTable= new AdministrateTable(oneInterfaceGuiBusinessLogic);
					oneAdministrateTable.setLocationRelativeTo(null);
					oneAdministrateTable.setSize(950,400);
					oneAdministrateTable.setVisible(true);
				}
			};
		}
		return chooseTable;
	}
	
	/**
	 * @return
	 */
	private AbstractAction getSpecification() {
		if(Specification == null)
		{
			Specification= new AbstractAction("Lastenheft", null) {
				public void actionPerformed(ActionEvent evt){
					try
					{
				        if (ManagementFactory.getOperatingSystemMXBean().getName().equals("Mac OS X"))
				        {
				        	Runtime.getRuntime().exec(new String[]{"open", "../FuzzyDBMS/doc/Lastenheft.pdf"});
				        }
				        else if (ManagementFactory.getOperatingSystemMXBean().getName().contains("Windows")) {
				            
				            Runtime.getRuntime().exec("cmd /c START ../FuzzyDBMS/doc/Lastenheft.pdf");
				        }
				        else {
				        	System.out.print(ManagementFactory.getOperatingSystemMXBean().getName()); 
				        	System.out.print("Unknown operating-system!"); 
				        } 
					}
					catch(Exception e){}

				}
			};
		}
		return Specification;
	}
	
	/**
	 * @return
	 */
	private AbstractAction getManual() {
		if(Manual == null)
		{
			Manual= new AbstractAction("Handbuch", null) {
				public void actionPerformed(ActionEvent evt){
					try
					{
				        if (ManagementFactory.getOperatingSystemMXBean().getName().equals("Mac OS X"))
				        {
				        	Runtime.getRuntime().exec(new String[]{"open", "../FuzzyDBMS/doc/Handbuch.pdf"});
				        }
				        else if (ManagementFactory.getOperatingSystemMXBean().getName().contains("Windows")) {
				            
				            Runtime.getRuntime().exec("cmd /c START ../FuzzyDBMS/doc/Handbuch.pdf");
				        }
				        else {
				        	System.out.print(ManagementFactory.getOperatingSystemMXBean().getName()); 
				        	System.out.print("Unknown operating-system!"); 
				        } 
					}
					catch(Exception e){}

				}
			};
		}
		return Manual;
	}

	/**
	 * @return
	 */
	private AbstractAction getJavaDoc() {
		if(JavaDoc == null)
		{
			JavaDoc= new AbstractAction("JavaDoc", null) {
				public void actionPerformed(ActionEvent evt){
					try
					{
				        if (ManagementFactory.getOperatingSystemMXBean().getName().equals("Mac OS X"))
				        {
				        	Runtime.getRuntime().exec(new String[]{"open", "../FuzzyDBMS/doc/index.html"});
				        }
				        else if (ManagementFactory.getOperatingSystemMXBean().getName().contains("Windows")) {
				            
				            Runtime.getRuntime().exec("cmd /c START ../FuzzyDBMS/doc/index.html");
				        }
				        else {
				        	System.out.print(ManagementFactory.getOperatingSystemMXBean().getName()); 
				        	System.out.print("Unknown operating-system!"); 
				        } 
					}
					catch(Exception e){}

				}
			};
		}
		return JavaDoc;
	}
	
	public void doReload(boolean newStatus) {
		jMenuItem6.setEnabled(true);
		}


}
```

Dialog:

```
package gui;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JButton;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;


public class AdministrateDb extends javax.swing.JDialog {
	private interfaceGuiBusinessLogic.InterfaceGBL oneInterfaceGuiBusinessLogic;
	private int acUserId;
	private JTable tableOfDb;
	private JScrollPane jScrollPane1;
	private JButton createNewDb;
	private JButton exitAdministrateDb;
	private JButton changeDb;
	private Vector vectorOfDb = new Vector();


	public AdministrateDb(interfaceGuiBusinessLogic.InterfaceGBL oneInterfaceGuiBusinessLogic, int acUserId){
		this.oneInterfaceGuiBusinessLogic=oneInterfaceGuiBusinessLogic;
		this.acUserId = acUserId;
		initGUI();
	}
	
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			{
				exitAdministrateDb = new JButton();
				getContentPane().add(exitAdministrateDb, new AnchorConstraint(879, 950, 958, 650, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				exitAdministrateDb.setText("Abbrechen");
				exitAdministrateDb.setPreferredSize(new java.awt.Dimension(77, 22));
				exitAdministrateDb.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						exitAdministrateDbMouseClicked(evt);
					}
				});
			}
			{
				changeDb = new JButton();
				getContentPane().add(changeDb, new AnchorConstraint(879, 650, 958, 350, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				changeDb.setText("Datenbank auswählen");
				changeDb.setPreferredSize(new java.awt.Dimension(141, 22));
				changeDb.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						changeDbMouseClicked(evt);
					}
				});
			}
			{
				createNewDb = new JButton();
				getContentPane().add(createNewDb, new AnchorConstraint(879, 350, 958,50, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				createNewDb.setText("Neue Datenbank anlegen");
				createNewDb.setPreferredSize(new java.awt.Dimension(160, 22));
				createNewDb.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						createNewDbMouseClicked(evt);
					}
				});
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1, new AnchorConstraint(40, 950, 800, 50, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jScrollPane1.setPreferredSize(new java.awt.Dimension(10, 10));
				{
					tableOfDb = getTableColumns();
					jScrollPane1.setViewportView(tableOfDb);
				}
			}
			setSize(700, 400);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doReload() {
		getTableColumns();
		} 
	
	private JTable getTableColumns() {
		vectorOfDb = oneInterfaceGuiBusinessLogic.getListOfDbs();
		int vectorSize = vectorOfDb.size();
		DefaultTableModel tableColumnsModel = 
				new DefaultTableModel(
						new String[vectorSize][4] ,
						new String[] { "ID", "Name der Datenbank", "Ersteller","Zugriffsrechte"})
        {
            public boolean isCellEditable(int row, int col)
            {
                return false;
            }

        }; 
            tableOfDb = new JTable();
			jScrollPane1.setViewportView(tableOfDb);
			tableOfDb.setModel(tableColumnsModel);
		    for (int i=0;i<vectorSize;i++)
            {
            	String[] helpMeArray = (String[])vectorOfDb.elementAt(i);
                for (int j=0;j<4;j++) 
                	{
                	tableOfDb.setValueAt(helpMeArray[j],i,j);
                	}
            }
            
		return tableOfDb;
	}
	
	
	
	private void createNewDbMouseClicked(MouseEvent evt) {
		CreateDb oneCreateDb = new CreateDb(this, oneInterfaceGuiBusinessLogic, acUserId);
		oneCreateDb.setLocationRelativeTo(null);
		oneCreateDb.setVisible(true);
	}
	
	private void exitAdministrateDbMouseClicked(MouseEvent evt) {
		this.dispose();
	}
	
	private void changeDbMouseClicked(MouseEvent evt) {
		frame.doRelaod(true);
		this.dispose();
	}

}
```


Mein Ziel ist es, die Methode "doReload(...)" in Frame aufzurufen. Der Aufruf soll in der Methode private void changeDbMouseClicked(MouseEvent evt) stattfinden). Die Methode soll ein jMenuItem aktivieren, welcher bis dahin deaktiviert war.

Was mache ich falsch,, bzw. wass muss ich wie ändern???
Danke!

P.S.: Ich habe schon mal versucht, die Instanz einfach mit this im Konstruktor zu übergeben. Funktioniert aber nicht, da ich nicht das MainWindow übergeben bekomme, sondern nur "AbstractAction".


----------



## Verjigorm (4. Aug 2008)

übergib anstatt "this"
Klassenname.this

Also bei dir wohl MainWindow.this
dann haste die Referenz auf das MainWindow im Dialog

this bezieht sich nämlich im ActionListener auf den ActionLister


----------



## till123 (4. Aug 2008)

Danke   

Problem behoben....


----------

