Help with GUI

Hi im a student and have been racking my brain trying to create a gui, it needs a file menu at the top (save, open, exit) as well as an edit menu (cut, copy, paste), this is the code i have but its probebly well wrong, but i cant check cos i cant get it to compile thanks to a couple of errors. please read and help.

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.FlavorEvent;

import java.awt.datatransfer.FlavorListener;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.AbstractAction;

import javax.swing.JFileChooser;

import java.io.*;

import java.awt.*;

import java.*;

import java.lang.*;

import java.awt.image.*;

import java.io.File;

import java.util.List;

import java.util.ArrayList;

import java.util.Iterator;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

class MenuExp extends JFrame {

private Object DataFlavor;

public MenuExp() {

setTitle("Menu Example");

setSize(400, 300);

// Creates a menubar for a JFrame

JMenuBar menuBar = new JMenuBar();

// Add the menubar to the frame

setJMenuBar(menuBar);

// Define and add two drop down menu to the menubar

JMenu fileMenu = new JMenu("File");

JMenu editMenu = new JMenu("Edit");

menuBar.add(fileMenu);

menuBar.add(editMenu);

// Create and add simple menu item to one of the drop down menu

JMenuItem newAction = new JMenuItem("New");

JMenuItem openAction = new JMenuItem("Open");

JMenuItem saveAction = new JMenuItem("Save");

JMenuItem exitAction = new JMenuItem("Exit");

JMenuItem cutAction = new JMenuItem("Cut");

JMenuItem copyAction = new JMenuItem("Copy");

JMenuItem pasteAction = new JMenuItem("Paste");

ButtonGroup bg = new ButtonGroup();

fileMenu.add(newAction);

fileMenu.add(openAction);

fileMenu.add(saveAction);

fileMenu.addSeparator();

fileMenu.add(exitAction);

editMenu.add(cutAction);

editMenu.add(copyAction);

editMenu.add(pasteAction);

// Add a listener to the New menu item. actionPerformed() method will

// invoked, if user triggred this menu item

exitAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {exit();}

});

openAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {open();}

});

cutAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {cut();}

});

pasteAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {paste();}

});

PropertyChangeListener focusListener = new PropertyChangeListener() {

public void propertyChange(PropertyChangeEvent arg0) {

if (arg0.getPropertyName() == "permanentFocusOwner") {

// The permanent focus owner has changed.

newPermanentFocusOwner((Component)e.getNewValue());

}

}

};

KeyboardFocusManager.getCurrentKeyboardFocusManager().

addPropertyChangeListener(focusListener);

}

public static void MenuExp(String[] args) {

MenuExp me = new MenuExp();

me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

me.setVisible(true);

}

private void exit()

{

system.exit(0);

}

private void cut()

{

}

public void open()

{

MenuExp fileChooser = new MenuExp();

JFileChooser fc = new JFileChooser();

FileFilter filter = new FileFilter();

filter.addExtension("txt");

fc.setFileFilter(filter);

int returnVal = fc.showOpenDialog(parent);

fc.showOpenDialog(fileChooser);

}

private void paste()

{

FlavorListener flavorListener = new FlavorListener(){

public void flavorsChanged(FlavorEvent arg0) {flavorsChanged();}

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

clipboard.addFlavorListener(flavorListener);

};

private void flavorsChanged()

{

Object PASSWORD_ENTRY_DATA_FLAVOR;

Object entryList;

CutCopyPasteHelper.registerDataFlavors(component, DataFlavor);

entryList.setTransferHandler(new ListTransferHandler());

CutCopyPasteHelper.registerCutCopyPasteBindings(entryList, true);

CutCopyPasteHelper.registerDataFlavors(entryList,PASSWORD_ENTRY_DATA_FLAVOR);

CutCopyPasteHelper.setPasteEnabled(entryList, true);

ListSelectionListener selectionListener = new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {

if (!e.getValueIsAdjusting()) {

boolean hasSelection = (entryList.getMinSelectionIndex() != -1);

CutCopyPasteHelper.setCopyEnabled(entryList, hasSelection);

CutCopyPasteHelper.setCutEnabled(entryList, hasSelection);

entryList.addListSelectionListener(new ListSelectionHandler());

}

}

};

};

}

}

[5500 byte] By [Chiia] at [2007-11-27 2:36:22]
# 1
you should:- put your code into statements- give the stack trace of the exception throwntks
calvino_inda at 2007-7-12 2:55:36 > top of Java-index,Java Essentials,Java Programming...
# 2

errm yeeh, this is the code which has the 2 errors, "private void flavorsChanged()" is getting an illegal start of expression, and "clipboard.addFlavorListener(flavorListener);" is getting an <identifier> expected error.

private void paste()

{

FlavorListener flavorListener = new FlavorListener(){

public void flavorsChanged(FlavorEvent arg0) {flavorsChanged();}

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

clipboard.addFlavorListener(flavorListener);

};

private void flavorsChanged()

{

Object PASSWORD_ENTRY_DATA_FLAVOR;

Object entryList;

CutCopyPasteHelper.registerDataFlavors(component, DataFlavor);

entryList.setTransferHandler(new ListTransferHandler());

CutCopyPasteHelper.registerCutCopyPasteBindings(entryList, true);

CutCopyPasteHelper.registerDataFlavors(entryList,PASSWORD_ENTRY_DATA_FLAVOR);

CutCopyPasteHelper.setPasteEnabled(entryList, true);

ListSelectionListener selectionListener = new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {

if (!e.getValueIsAdjusting()) {

boolean hasSelection = (entryList.getMinSelectionIndex() != -1);

CutCopyPasteHelper.setCopyEnabled(entryList, hasSelection);

CutCopyPasteHelper.setCutEnabled(entryList, hasSelection);

entryList.addListSelectionListener(new ListSelectionHandler());

}

}

};

};

}

Chiia at 2007-7-12 2:55:37 > top of Java-index,Java Essentials,Java Programming...
# 3

I have no idea about your program. I tried to correct some pbs...

try this :

package test;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.FlavorEvent;

import java.awt.datatransfer.FlavorListener;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.AbstractAction;

import javax.swing.JFileChooser;

import java.io.*;

import java.awt.*;

import java.*;

import java.lang.*;

import java.awt.image.*;

import java.io.File;

import java.util.List;

import java.util.ArrayList;

import java.util.Iterator;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

class MenuExp extends JFrame {

private Object DataFlavor;

public MenuExp() {

setTitle("Menu Example");

setSize(400, 300);

// Creates a menubar for a JFrame

JMenuBar menuBar = new JMenuBar();

// Add the menubar to the frame

setJMenuBar(menuBar);

// Define and add two drop down menu to the menubar

JMenu fileMenu = new JMenu("File");

JMenu editMenu = new JMenu("Edit");

menuBar.add(fileMenu);

menuBar.add(editMenu);

// Create and add simple menu item to one of the drop down menu

JMenuItem newAction = new JMenuItem("New");

JMenuItem openAction = new JMenuItem("Open");

JMenuItem saveAction = new JMenuItem("Save");

JMenuItem exitAction = new JMenuItem("Exit");

JMenuItem cutAction = new JMenuItem("Cut");

JMenuItem copyAction = new JMenuItem("Copy");

JMenuItem pasteAction = new JMenuItem("Paste");

ButtonGroup bg = new ButtonGroup();

fileMenu.add(newAction);

fileMenu.add(openAction);

fileMenu.add(saveAction);

fileMenu.addSeparator();

fileMenu.add(exitAction);

editMenu.add(cutAction);

editMenu.add(copyAction);

editMenu.add(pasteAction);

// Add a listener to the New menu item. actionPerformed() method will

// invoked, if user triggred this menu item

exitAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

exit();

}

});

openAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

open();

}

});

cutAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

cut();

}

});

pasteAction.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

paste();

}

});

PropertyChangeListener focusListener = new PropertyChangeListener() {

public void propertyChange(PropertyChangeEvent e) {

if (e.getPropertyName() == "permanentFocusOwner") {

// The permanent focus owner has changed.

new PermanentFocusOwner((Component) e.getNewValue());

}

}

};

KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(focusListener);

}

public static void MenuExp(String[] args) {

MenuExp me = new MenuExp();

me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

me.setVisible(true);

}

private void exit() {

System.exit(0);

}

private void cut() {

}

public void open() {

MenuExp fileChooser = new MenuExp();

JFileChooser fc = new JFileChooser();

FileFilter filter = new FileFilter();

filter.addExtension("txt");

fc.setFileFilter(filter);

int returnVal = fc.showOpenDialog(parent);

fc.showOpenDialog(fileChooser);

}

private void paste()

{

FlavorListener flavorListener = new FlavorListener(){

public void flavorsChanged(FlavorEvent e) {

flavorsChanged(e);

}

};

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

clipboard.addFlavorListener(flavorListener);

}

private void flavorsChanged() {

Object PASSWORD_ENTRY_DATA_FLAVOR;

Object entryList;

CutCopyPasteHelper.registerDataFlavors(component, DataFlavor);

entryList.setTransferHandler(new ListTransferHandler());

CutCopyPasteHelper.registerCutCopyPasteBindings(entryList, true);

CutCopyPasteHelper.registerDataFlavors(entryList, PASSWORD_ENTRY_DATA_FLAVOR);

CutCopyPasteHelper.setPasteEnabled(entryList, true);

ListSelectionListener selectionListener = new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {

if (!e.getValueIsAdjusting()) {

boolean hasSelection = (entryList.getMinSelectionIndex() != -1);

CutCopyPasteHelper.setCopyEnabled(entryList, hasSelection);

CutCopyPasteHelper.setCutEnabled(entryList, hasSelection);

entryList.addListSelectionListener(new ListSelectionHandler());

}

}

};

};

}

java_2006a at 2007-7-12 2:55:37 > top of Java-index,Java Essentials,Java Programming...
# 4
Is there a shortcut key for indentation in NetBeans?
su_penguina at 2007-7-12 2:55:37 > top of Java-index,Java Essentials,Java Programming...
# 5
dunno but for eclipse it's:CTRL+A (select all) and CTRL+SHIFT+i
calvino_inda at 2007-7-12 2:55:37 > top of Java-index,Java Essentials,Java Programming...