aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to use menu event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to use menu event" Watch "How to use menu event" New topic
Author

How to use menu event

Subitha Sangar
Greenhorn

Joined: Jan 10, 2006
Posts: 8
Hi i am a basic leaner in java. Anyone please help me how can i change the colour when i choose the coloritem from menubar. May be my OOPs comcept is wrong. But i should keep 4 different programs. Please tell me how i have to use the OOP concept to keep 4 programs and do this work. My 4 programes are bellow. Advance thanks for your help.



EDIT by mw: Added Code Tags to original poster's indentation.
[ January 16, 2006: Message edited by: marc weber ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Moving to the Swing forum.


GenRocket - Experts at Building Test Data
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it's just a matter of adding the colors to PMenuBar, then a method to
return the selected color (note I changed the class to extend JMenuBar),
then adding some refences between the classes, so that when paintComponent()
is called, it gets the selected color from the menu bar

(for simplicity (mine) I combined all the classes into one jave file)

Subitha Sangar
Greenhorn

Joined: Jan 10, 2006
Posts: 8
Thanks for your help. If i have another menu item call "save" and i have one class savefile() in DrawareaPanel then how can i call savefile() from PMenuBar. Please give me an idea. Thanks in advance. My programes are below.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
what is to be saved?

how are you getting the file path/name to save to?
Subitha Sangar
Greenhorn

Joined: Jan 10, 2006
Posts: 8
I am going to save the drawing into an arraylist and save that arraylist as a file. my coding under savefile() is bellow. when i use the button from paintpanel it is working fine and i can able to save it... but i should use the menu and get the event from menu. i donno how to get the menu even from PMenuBar.java. Please give me an idea how can i call this save method from PMenuBar. my savefile() in DrawareaPanel is bellow.

public void savefile()
{
JFileChooser filecho = new JFileChooser(".");
filecho.setDialogTitle("Save File As");
int action = fichooser.showSaveDialog(this);

if (action != JFileChooser.APPROVE_OPTION) {
// When canceled.
return;
}


File file = filecho.getSelectedFile();

if (file.exists()) {
action = JOptionPane.showConfirmDialog(this,"File exists, overwrite the file?");
if (action != JOptionPane.YES_OPTION)
return;
}


try {
ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(file));
//out.writeObject(getBackground());
out.writeObject(arrayline);
out.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(this,
"Error :\t" + e.getMessage());
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this might be one way to approach the problem.

instead of passing a reference of PMenuBar to the drawingPanel,
reverse this so you now pass a reference of the drawingPanel to PMenuBar.
(PMenuBar is now approaching the stage where it's re-usability is suspect,
so perhaps working with a standard JMenuBar, as part of the main gui's code,
might be worth considering).

For the moment, working with PMenuBar, you now have a reference to the
drawingPanel, so, first up, you have put the colorSelection variable in the
drawingPanel and 'set' the number whenever the color items are clicked in the menu
(instead of 'getting' the number from PMenuBar).

next, I would have menuItems of 'Save' and 'Save As', with the JFileChooser
attached to 'Save As' (you may want to disable 'Save' until a fileName is set)

include in the method signature of drawingPanel's save(), the file name to save as
public void save(File fileName)
{
//save the data to 'fileName'
}

so, when save (or save as) is clicked, PMenuBar calls drawingPanel's save
method, passing it an argument of the filename.
Subitha Sangar
Greenhorn

Joined: Jan 10, 2006
Posts: 8
Hi, Michael Dunn. Thank you so much for your help. i did the same way and it is working fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use menu event