Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Cannot pass info from Menu to Panel

Avatar of P-R-W
P-R-WFlag for Netherlands asked on
Programming Languages-OtherJava
5 Comments1 Solution379 ViewsLast Modified:
Hello,

Have a problem with passing info from one class to another. Have three classes: the main class (GUI) which creates a Menu and a Panel (see source). I want to open a file within the menu and pass the info to the panel. Somehow this doesn't work. When, I take out the comments from row 91 and /or 92 in menu, I get an error?

Anybody have an idea?
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author PWiltjer
 */
public class Panel extends JPanel {

  private JButton go;
  private JLabel filePath;
  private JTextField textField;

  public void setTextField(String s) {
    textField.setText(s);
  }

  public String getLabel() {
    return filePath.getText();
  }

  public Panel() {

    go = new JButton("GO");
    go.addActionListener(new GoKnopHandler());
    add(go);

    filePath = new JLabel("Filepath:");
    add(filePath);

    textField = new JTextField("The filenaam + path will get placed here");
    add(textField);
  }

  class GoKnopHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      //   Do Something
    }
  }
}
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 *
 * @author PWiltjer
 */
public class Menu {

   private Frame frame;
   private JPanel panelGui;

    public JMenuBar createMenuBar(Frame frame, JPanel panelViaGui) {
        this.frame = frame;
        this.panelGui = panelViaGui;

        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(menu);

        //a group of JMenuItems
        menuItem = new JMenuItem("Open...",
                                 KeyEvent.VK_O);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead

        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        menuItem.addActionListener (new openHandler());
        menu.add(menuItem);
       
        menu.addSeparator();

        menuItem = new JMenuItem("Exit");
        menuItem.setMnemonic(KeyEvent.VK_X);
        menuItem.addActionListener (new exitHandler());
        menu.add(menuItem);
             
        //Build second menu in the menu bar.
        menu = new JMenu("Help");
        menu.setMnemonic(KeyEvent.VK_H);
        menuBar.add(menu);

        menuItem = new JMenuItem("About");
        menuItem.setMnemonic(KeyEvent.VK_A);
        menuItem.addActionListener (new aboutHandler());
        menu.add(menuItem);

        return menuBar;
    }
    class openHandler implements ActionListener {
      public void actionPerformed( ActionEvent e) {
        System.out.println("pressed open");
        openFile();
      }
    }
    class exitHandler implements ActionListener {
      public void actionPerformed( ActionEvent e) {
    System.out.println("pressed exit");
      }
    }
    class aboutHandler implements ActionListener {
      public void actionPerformed( ActionEvent e) {
    System.out.println("pressed about");
      }
    }

    private void openFile() {
      FileDialog filedialog =
        new FileDialog( frame,
                        "Open a file", FileDialog.LOAD );
      filedialog.show();
      String filenaam = filedialog.getFile();
      String directorynaam = filedialog.getDirectory();
      System.out.println(filenaam);
      System.out.println(directorynaam);

//        panelGui.textField.setText(filenaam);
//        panelGui.setTextField (filenaam);
    }
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;

// @author PWiltjer
public class Gui {

  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

      public void run() {
        createAndShowGUI();
      }
    });
  }

  private static void createAndShowGUI() {

    //Create and set up the window.
    JFrame frame = new JFrame("Test-Menu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create the panel in the content pane.
    Panel panel = new Panel();
    panel.setOpaque(true);
    panel.setBackground(new Color(255, 255, 255));
    panel.setPreferredSize(new Dimension(550, 150));

     //Create and place the menu.
    Menu menu = new Menu();
    frame.setJMenuBar(menu.createMenuBar(frame, panel));

    //Add the panel to the content pane.
    frame.getContentPane().add(panel, BorderLayout.EAST);

    //Display the window.
    frame.pack();
  
    frame.setVisible(true);
  }

  public Gui() {
  }

  }
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers