• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

painting problems using JmenuBar

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having problems with this code, when Click on a menu the String "Hello World" paints over the top of the menus.
//
// Swing.java
//
//
//
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Swing extends JFrame
implements ActionListener


{
static final String message = "Hello World!";
private Font font = new Font("serif", Font.ITALIC+Font.BOLD, 36);



// Declarations for menus
static final JMenuBar mainMenuBar = new JMenuBar();

static final JMenu fileMenu = new JMenu("File");
protected JMenuItem miNew;
protected JMenuItem miOpen;
protected JMenuItem miClose;
protected JMenuItem miSave;
protected JMenuItem miSaveAs;
protected JMenuItem miAbout;

static final JMenu editMenu = new JMenu("Edit");
protected JMenuItem miUndo;
protected JMenuItem miCut;
protected JMenuItem miCopy;
protected JMenuItem miPaste;
protected JMenuItem miClear;
protected JMenuItem miSelectAll;


public void addFileMenuItems() {
miNew = new JMenuItem ("New");
miNew.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.Event.META_MASK));
fileMenu.add(miNew).setEnabled(true);
miNew.addActionListener(this);
miOpen = new JMenuItem ("Open...");
miOpen.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.META_MASK));
fileMenu.add(miOpen).setEnabled(true);
miOpen.addActionListener(this);

miClose = new JMenuItem ("Close");
miClose.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.Event.META_MASK));
fileMenu.add(miClose).setEnabled(true);
miClose.addActionListener(this);

miSave = new JMenuItem ("Save");
miSave.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.META_MASK));
fileMenu.add(miSave).setEnabled(true);
miSave.addActionListener(this);

miSaveAs = new JMenuItem ("Save As...");
fileMenu.add(miSaveAs).setEnabled(true);
miSaveAs.addActionListener(this);

mainMenuBar.add(fileMenu);
}


public void addEditMenuItems() {
miUndo = new JMenuItem("Undo");
miUndo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.Event.META_MASK));
editMenu.add(miUndo).setEnabled(true);
miUndo.addActionListener(this);
editMenu.addSeparator();
miCut = new JMenuItem("Cut");
miCut.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.Event.META_MASK));
editMenu.add(miCut).setEnabled(true);
miCut.addActionListener(this);
miCopy = new JMenuItem("Copy");
miCopy.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.META_MASK));
editMenu.add(miCopy).setEnabled(true);
miCopy.addActionListener(this);
miPaste = new JMenuItem("Paste");
miPaste.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.Event.META_MASK));
editMenu.add(miPaste).setEnabled(true);
miPaste.addActionListener(this);
miClear = new JMenuItem("Clear");
editMenu.add(miClear).setEnabled(true);
miClear.addActionListener(this);
editMenu.addSeparator();
miSelectAll = new JMenuItem("Select All");
miSelectAll.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.Event.META_MASK));
editMenu.add(miSelectAll).setEnabled(true);
miSelectAll.addActionListener(this);
mainMenuBar.add(editMenu);
}

public void addMenus() {
addFileMenuItems();
addEditMenuItems();
setJMenuBar (mainMenuBar);
}
public Swing() {
super("Swing");

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

this.getContentPane().setLayout(null);

addMenus();

//Toolkit.getDefaultToolkit();
setBounds(50,50,400,400);
setVisible(true);
}


// ActionListener interface (for menus)
public void actionPerformed(ActionEvent newEvent) {
if (newEvent.getActionCommand().equals(miNew.getActionCommand())) doNew();
else if (newEvent.getActionCommand().equals(miOpen.getActionCommand())) doOpen();
else if (newEvent.getActionCommand().equals(miClose.getActionCommand())) doClose();
else if (newEvent.getActionCommand().equals(miSave.getActionCommand())) doSave();
else if (newEvent.getActionCommand().equals(miSaveAs.getActionCommand())) doSaveAs();
else if (newEvent.getActionCommand().equals(miUndo.getActionCommand())) doUndo();
else if (newEvent.getActionCommand().equals(miCut.getActionCommand())) doCut();
else if (newEvent.getActionCommand().equals(miCopy.getActionCommand())) doCopy();
else if (newEvent.getActionCommand().equals(miPaste.getActionCommand())) doPaste();
else if (newEvent.getActionCommand().equals(miClear.getActionCommand())) doClear();
else if (newEvent.getActionCommand().equals(miSelectAll.getActionCommand())) doSelectAll();
}
public void doNew() {}

public void doOpen() {}

public void doClose() {}

public void doSave() {}

public void doSaveAs() {}

public void doUndo() {}

public void doCut() {}

public void doCopy() {}

public void doPaste() {}

public void doClear() {}

public void doSelectAll() {}

public static void main(String args[]) {
new Swing();
}

public void paint(Graphics g){
super.paint(g);
g.setColor(Color.blue);
g.setFont (font);
g.drawString(message, 40, 80);
mainMenuBar.repaint();
}
}
I have tried a few things out with no success.
thankyou,
alan
 
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
Where are you wanting the "hello world" to show up? I also don't know why you are over-riding a paint() method in a JFrame with just a menu bar.
I think that maybe you are wanting the JFrame to say Hello World in the title bar? If that is the case, you need a constructor in your class

If this is not what you are wanting, let me know
 
alan partridge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not that I want to paint straight to Graphics context of the JFrame, I also tried overiding the paint method of a JPanel added to the JFrame and this also produced unexpected results(from my point of view), what I was wondering is there a set way of redrawing components that become overlapped with menus where you are overiding the paint method?
 
Gregg Bolinger
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
try repaint(), validate() (AWT) or reValidate() (SWING)
 
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic