• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to use menu event

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Swing forum.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is to be saved?

how are you getting the file path/name to save to?
 
Subitha Sangar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Michael Dunn. Thank you so much for your help. i did the same way and it is working fine.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic