0

I am trying to improve myself on java programming.

What I am trying to do is, I have a menu and submenus, For example I am clicking on File menu, and picking Converter (sub menu) and I want it to open new window for my converter program.

Here is my converter window

public class Converter extends JFrame {

    private static final long   serialVersionUID    = 1L;
    private MoneyDetails        convertMe           = new MoneyDetails();
    private JLabel              tlLabel             = new JLabel("     Amount of TL");
    private JLabel              dollarsLabel        = new JLabel("Amount of Dollars");
    private JTextField          tlField             = new JTextField("0.0");
    private JTextField          dollarsField        = new JTextField("0.0");
    private JButton             tlButton            = new JButton("Convert to $");
    private JButton             dollarsButton       = new JButton("<<< Convert to TL");
    private JButton             setRates            = new JButton("Set Rates");

    public Converter() {
        JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
        dataPanel.add(tlLabel);

        dataPanel.add(dollarsLabel);
        dataPanel.add(tlField);
        dataPanel.add(dollarsField);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(tlButton);
        buttonPanel.add(dollarsButton);
        Container container = getContentPane();
        container.add(dataPanel, BorderLayout.CENTER);
        container.add(buttonPanel, BorderLayout.SOUTH);
        tlButton.addActionListener(new TLConverter());
        dollarsButton.addActionListener(new DollarsConverter());
        buttonPanel.add(setRates);
    }
    private class TLConverter implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
                String input = tlField.getText();
                double tl = Double.parseDouble(input);
                convertMe.setTL(tl);
                double dollars = convertMe.getDollars();
                dollarsField.setText(String.format("%.2f", dollars));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Please enter the amount that will be converted.");
            }
        }
    }

    private class DollarsConverter implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String input = dollarsField.getText();
            double dollars = Double.parseDouble(input);
            convertMe.setDollars(dollars);
            double tl = convertMe.getTL();
            tlField.setText(String.format("%.2f", tl));
        }
    }

    public static void main(String[] args) {
        Converter theGUI = new Converter();
        theGUI.setTitle("TL to $ or $ to TL Converter");
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theGUI.pack();
        theGUI.setVisible(true);
    }
}

and here is my menu which is in my converter class

private JMenuBar menuBar = new JMenuBar(); // Window menu bar
public Converter(String title) {
    setTitle(title);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setJMenuBar(menuBar); // Add the menu bar to the window
    JMenu fileMenu = new JMenu("File"); // Create File menu
    JMenu elementMenu = new JMenu("Elements"); // Create Elements menu
    JMenuItem subTest = new JMenuItem("Test");
    menuBar.add(fileMenu); // Add the file menu
    menuBar.add(elementMenu); // Add the element menu

    fileMenu.add(subTest);

  }

public static void main(String [] args)
{

Converter window = new Converter("Para Dönüstürücü"); 
window.setBounds(30, 30, 300, 300);
window.setVisible(true);
Converter theGUI = new Converter(); 

}

When I click on on my submenu, I want it to open my conveter window. How could I do it?

Ben
  • 51,770
  • 36
  • 127
  • 149
Yeliz Il
  • 49
  • 2
  • 2
  • 10
  • 1
    *"want it to open new window"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 02 '12 at 10:11

2 Answers2

0

What you need is an event listener, something like so:

fileMenu.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        new Converter();
      }
    });

You can see a more complete example here.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

You need to add Listener to your menuItem, listeners provide you callback when a particular events occurs like click, mouseout e.t.c.

You can add an ActionListener to your menuItem and in the callback method you show your GUI.

subTest.addActionListener(new ActionListener(){     
   public void actionPerformed(ActionEvent actionEvent){
       Converter convert = new Converter("Testing Opening this from Menu Item");
       convert.pack();
       convert.setVisible(true);
   }
});

You can read about all the different listeners that Swing provide here.

mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • Thank you very much, but I have tried this one and It compiles very well but it doesnt pop up my Converter window when I click on my submenu :/ – Yeliz Il May 02 '12 at 08:42
  • Edited my answer, in your constructor you are just adding component to the frame not making it visible. Try this should work – mprabhat May 02 '12 at 08:49
  • Yeah , that one worked. Thank you very much :) All I have to do is configuring my Converter window for the new window. Is there any way to directly pop my window to the new window named Testing Opening this Menu Item ? – Yeliz Il May 02 '12 at 08:55
  • Glad it helped you :) I dont understand your question. – mprabhat May 02 '12 at 08:58