-1

I'm new in Java so I really need help. I want my JMenu open to open file using file chooser only when I clicked it but what happening is that it does not appear.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Myprog extends JFrame {
JMenuBar hard= new JMenuBar();
JMenu file = new JMenu("FILE");
JMenu open = new JMenu("Open");
JMenu histo = new JMenu("History");
JMenuItem exit = new JMenuItem("Exit");
JMenu view = new JMenu("VIEW");
JMenu descript = new JMenu("Description");
JMenu spm = new JMenu("Specs/Model/Version");
JMenu extra = new JMenu("EXTRAS");
JMenu trans = new JMenu("Transaction");
JMenu calcu = new JMenu("Calculator");
JMenu qoutes = new JMenu("Quotes");
JMenuItem game = new JMenuItem("Games");
JMenu about = new JMenu("ABOUT");
JMenuItem java = new JMenuItem("JAVA");
JMenuItem sirjet = new JMenuItem("References");
JMenuItem akoto = new JMenuItem("MyProfile");
JMenuItem mouse = new JMenuItem("Mouse");
JMenuItem cpu = new JMenuItem("CPU");
JMenuItem cam = new JMenuItem("Camera");
JMenuItem key = new JMenuItem("Keyboard");
JMenuItem mem = new JMenuItem("Memory");
JMenuItem mic = new JMenuItem("Microphone");
JMenuItem print= new JMenuItem("Printer");
JMenuItem scan = new JMenuItem("Scanner");
JMenuItem speak = new JMenuItem("Speaker");
JMenuItem mon = new JMenuItem("Monitor");
JFileChooser choosyAko  = new JFileChooser(".");


JLabel direct = new JLabel(" ");
JLabel filename = new JLabel(" ");


public Myprog(){
super("My Program(Hardware)");
setVisible(true);
setBounds(250,250,700,700);
setBackground(Color.GREEN);
direct.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);


setJMenuBar(hard);
hard.add(file);
file.setMnemonic('F');
file.add(open);
open.setMnemonic('O');
file.add(histo);
histo.setMnemonic('H');
histo.add(cam);
histo.add(cpu);
histo.add(key);
histo.add(mem);
histo.add(mic);
histo.add(mon);
histo.add(mouse);
histo.add(print);
histo.add(scan);
histo.add(speak);
file.add(exit);
exit.setMnemonic('X');
hard.add(view);
view.setMnemonic('V');
view.add(descript);
descript.setMnemonic('D');
view.add(spm);
spm.setMnemonic('S');
hard.add(extra);
extra.setMnemonic('E');
extra.add(trans);
trans.setMnemonic('T');
extra.add(calcu);
calcu.setMnemonic('C');
extra.add(qoutes);
qoutes.setMnemonic('Q');
extra.add(game);
game.setMnemonic('G');
hard.add(about);
about.setMnemonic('B');
about.add(java);
java.setMnemonic('J');
about.add(sirjet);
sirjet.setMnemonic('N');
about.add(akoto);
add(direct, BorderLayout.NORTH);
add(filename, BorderLayout.SOUTH);
add(choosyAko,BorderLayout.CENTER);

open.addActionListener(new open());
exit.addActionListener(new exit());
}
class open implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
        {
    JFileChooser choosyAko  = new JFileChooser(".");
    JLabel direct = new JLabel(" ");
    JLabel filename = new JLabel(" ");
    choosyAko.setControlButtonsAreShown(false);
        }
    }
class exit implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
        {
    System.exit(0);
        }
    }
public static void main(String []args)
{ 
Myprog frame = new Myprog();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. – Andrew Thompson Feb 08 '15 at 08:05
  • BTW `new Font("Serif", Font.BOLD | Font.ITALIC, 36)` would better be `new Font(Font.SERIF, Font.BOLD | Font.ITALIC, 36) // for compile time checking!`. More generally, don't use 'magic numbers' when there is a defined constant. – Andrew Thompson Feb 08 '15 at 08:09
  • thank you sir i will always put that in mind – Nhezy Anglo Sabado Feb 08 '15 at 08:11
  • *"will always put that in mind"* Right now is a good time to start, with an [edit to the question](http://stackoverflow.com/posts/28391631/edit). – Andrew Thompson Feb 08 '15 at 08:13

2 Answers2

0

You have initialized the JFileChooser but you didn't show the dialog. You must call the appropriate method to display:

  • int returnVal = chooser.showOpenDialog(parent);
  • int returnVal = chooser.showSaveDialog(parent);
Ayman
  • 1,682
  • 15
  • 17
0

I changed the file a little bit to show the file open dialog after selecting the open menu. What do you use for development? Just try NetBeans. It's much easier with an IDE to create useful user interfaces.

Here is what i did based on your code:

package hw;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Myprog extends JFrame {

    JMenuBar hard = new JMenuBar();
    JMenu file = new JMenu("FILE");
    JMenuItem open = new JMenuItem("Open");
    JMenu histo = new JMenu("History");
    JMenuItem exit = new JMenuItem("Exit");
    JMenu view = new JMenu("VIEW");
    JMenu descript = new JMenu("Description");
    JMenu spm = new JMenu("Specs/Model/Version");
    JMenu extra = new JMenu("EXTRAS");
    JMenu trans = new JMenu("Transaction");
    JMenu calcu = new JMenu("Calculator");
    JMenu qoutes = new JMenu("Quotes");
    JMenuItem game = new JMenuItem("Games");
    JMenu about = new JMenu("ABOUT");
    JMenuItem java = new JMenuItem("JAVA");
    JMenuItem sirjet = new JMenuItem("References");
    JMenuItem akoto = new JMenuItem("MyProfile");
    JMenuItem mouse = new JMenuItem("Mouse");
    JMenuItem cpu = new JMenuItem("CPU");
    JMenuItem cam = new JMenuItem("Camera");
    JMenuItem key = new JMenuItem("Keyboard");
    JMenuItem mem = new JMenuItem("Memory");
    JMenuItem mic = new JMenuItem("Microphone");
    JMenuItem print = new JMenuItem("Printer");
    JMenuItem scan = new JMenuItem("Scanner");
    JMenuItem speak = new JMenuItem("Speaker");
    JMenuItem mon = new JMenuItem("Monitor");
    JFileChooser choosyAko = new JFileChooser(".");

    JLabel direct = new JLabel(" ");
    JLabel filename = new JLabel(" ");

    public Myprog() {
        super("My Program(Hardware)");
        setBounds(250, 250, 700, 700);
        setBackground(Color.GREEN);
        direct.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setJMenuBar(hard);
        hard.add(file);
        file.setMnemonic('F');
        file.add(open);
        open.setMnemonic('O');
        file.add(histo);
        histo.setMnemonic('H');
        histo.add(cam);
        histo.add(cpu);
        histo.add(key);
        histo.add(mem);
        histo.add(mic);
        histo.add(mon);
        histo.add(mouse);
        histo.add(print);
        histo.add(scan);
        histo.add(speak);
        file.add(exit);
        exit.setMnemonic('X');
        hard.add(view);
        view.setMnemonic('V');
        view.add(descript);
        descript.setMnemonic('D');
        view.add(spm);
        spm.setMnemonic('S');
        hard.add(extra);
        extra.setMnemonic('E');
        extra.add(trans);
        trans.setMnemonic('T');
        extra.add(calcu);
        calcu.setMnemonic('C');
        extra.add(qoutes);
        qoutes.setMnemonic('Q');
        extra.add(game);
        game.setMnemonic('G');
        hard.add(about);
        about.setMnemonic('B');
        about.add(java);
        java.setMnemonic('J');
        about.add(sirjet);
        sirjet.setMnemonic('N');
        about.add(akoto);
        add(direct, BorderLayout.NORTH);
        add(filename, BorderLayout.SOUTH);
//        add(choosyAko, BorderLayout.CENTER);

        open.addActionListener(new open());
        exit.addActionListener(new exit());
    }

    class open implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JFileChooser choosyAko = new JFileChooser(".");
            JLabel direct = new JLabel(" ");
            JLabel filename = new JLabel(" ");
            choosyAko.setControlButtonsAreShown(false);
            choosyAko.showOpenDialog(null);
        }
    }

    class exit implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        Myprog frame = new Myprog();
        frame.setVisible(true);
    }
}
chris
  • 1,685
  • 3
  • 18
  • 28