aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes painting problems using JmenuBar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "painting problems using JmenuBar" Watch "painting problems using JmenuBar" New topic
Author

painting problems using JmenuBar

alan partridge
Greenhorn

Joined: Jan 08, 2002
Posts: 4
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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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


GenRocket - Experts at Building Test Data
alan partridge
Greenhorn

Joined: Jan 08, 2002
Posts: 4
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

try repaint(), validate() (AWT) or reValidate() (SWING)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: painting problems using JmenuBar