Code Drogba Code Drogba - 21 days ago 7
Java Question

File explorer not opening when using JFileChooser not sure why

I am trying to implement code to save a file when the "save" JMenuItem is clicked but nothing is happening and I cant figure out why, my code is below, any help will be greatly appreciated. I want to save all the items in the arraylist on to txt file and be able to select the folder where to save the file and then open the file using jfilechooser() but I am stuck on the saving functionality.

public class ShapeMainQ2 extends JFrame implements MouseListener{
JMenuBar jmb;
JMenu newMenu, openMenu, openfileMenu, shapelist;
JMenuItem newItem, closeItem, saveMenu, exitItem, rItem, sItem, cItem;
JCheckBoxMenuItem codeItem, commentItem;

ArrayList<Shape> list = new ArrayList<Shape>();
int x, y, l, w, r, sl;
public static int tracker = 0;

public ShapeMainQ2() {
Container container=getContentPane();
container.setLayout(new FlowLayout());

jmb = new JMenuBar();
setJMenuBar(jmb);


newMenu = new JMenu("New");
saveMenu = new JMenu("Save");
openfileMenu = new JMenu("Open");
shapelist = new JMenu("View Shapes");


codeItem = new JCheckBoxMenuItem("code");
commentItem = new JCheckBoxMenuItem("comment");



/*cutMenu.add(codeItem);
cutMenu.add(commentItem);
saveMenu.add(cutMenu);*/

//newItem = new JMenuItem("New");
openMenu = new JMenu("Shape");
rItem = new JMenuItem("Rectangle");
sItem = new JMenuItem("Square");
cItem = new JMenuItem("Circle");

jmb.add(newMenu);
jmb.add(saveMenu);
jmb.add(openfileMenu);
jmb.add(shapelist);

newMenu.add(openMenu);

rItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
tracker = 1;
l = Integer.parseInt(JOptionPane.showInputDialog("Enter the length of the rectangle"));
w = Integer.parseInt(JOptionPane.showInputDialog("Enter the width of the rectangle"));
Shape rec = new Rectangle("Rectangle", l, w);
list.add(rec);
} catch(Exception ex) {
System.out.println("User cancelled the operation");
}
}
});

sItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
tracker = 3;
sl = Integer.parseInt(JOptionPane.showInputDialog("Enter the length of the square"));
Shape squ = new Rectangle("Square", sl, sl);
list.add(squ);
} catch(Exception ex) {
System.out.println("User cancelled the operation");
}
}
});

cItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
tracker = 2;
r = Integer.parseInt(JOptionPane.showInputDialog("Enter the radius of the circle"));
Shape cir = new Circle("Circle", r);
list.add(cir);
} catch(Exception ex) {
System.out.println("User cancelled the operation");
}
}
});

saveMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sb = "TEST CONTENT";
JFileChooser chooser = new JFileChooser();
int retrival = chooser.showSaveDialog(null);
if (retrival == JFileChooser.APPROVE_OPTION) {
try {
FileWriter fw = new FileWriter(chooser.getSelectedFile()+".txt");
fw.write(sb.toString());
fw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});

openMenu.add(rItem);
openMenu.add(sItem);
openMenu.add(cItem);

container.addMouseListener(this);

container.add(jmb);
}

public static void main(String args[]) {
ShapeMainQ2 win = new ShapeMainQ2();
win.setTitle("Menu Demo");
win.setSize(500, 500);
win.setDefaultCloseOperation(EXIT_ON_CLOSE);
win.setLocationRelativeTo(null);
win.setVisible(true);
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
x = e.getX();
y = e.getY();
repaint();
}
public void drawRectangle(int x, int y) {
Graphics g = this.getGraphics();
g.drawRect(x, y, l, w);
g.setColor(Color.BLACK);
}

public void drawCircle(int x, int y) {
Graphics g = this.getGraphics();
g.drawOval(x, y, r*2, r*2);
g.setColor(Color.BLACK);
}

public void drawSquare(int x, int y) {
Graphics g = this.getGraphics();
g.drawRect(x, y, sl, sl);
g.setColor(Color.BLACK);
}

@Override
public void paint(Graphics g) {
super.paint(g);
switch(tracker) {
case 1:
drawRectangle(x, y);
break;
case 2:
drawCircle(x, y);
break;
case 3:
drawSquare(x, y);
break;
}
}
}

Answer
jmb.add(saveMenu);

You are adding the JMenuItem to the JMenuBar, which is wrong. You can't add and JMenu or JMenuItem directly to the JMenuBar and use an ActionListener.

You need to add a JMenu to the JMenuBar and then you add JMenuItems to the JMenu.

container.add(jmb);

Don't add the menubar to the container. You already used the setJMenuBar(...) method which is the correct way to display a JMenuBar.

Read the section from the Swing tutorial on How to Use Menus for more information and working examples.

Also, your custom painting is wrong. Don't override paint() of the frame. Custom painting is done by overriding paintComponent(...) of a JPanel and then you add the panel to the frame. Look at the table of contents from the tutorial link I gave you above and you will find a section on Custom Painting with more information and working examples.