Vinayak Garg
Vinayak Garg

Reputation: 6606

Strange display issue with JFrame

In the following simple code I simply create a Frame, and add a JPanel and menubar to it.

public class MainFrame extends JFrame {
    private DrawPanel drawPanel;
    public MainFrame()
    {
        super("Coordinate Geometry Visualiser");
        drawPanel = new DrawPanel();
        add(drawPanel);

        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic('F');

        JMenuItem newItem = new JMenuItem("New");
        newItem.setMnemonic('N');
        fileMenu.add(newItem);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);

        JMenu editMenu = new JMenu("Edit");
        editMenu.setMnemonic('E');
        menuBar.add(editMenu);
    }
}

Draw Panel code -

public class DrawPanel extends JPanel {
    public DrawPanel()
    {
    }
    public void paintComponent(Graphics g) 
    {
        super.paintComponents(g);
        setBackground(Color.BLACK);
        g.setColor(Color.RED);
        g.drawLine(100, 50, 150, 100);
    }
}

and finally the application with main()

public class CGVApplication {
    public static void main(String[] args) {
        MainFrame appFrame = new MainFrame();
        appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        appFrame.setSize(300, 275);
        appFrame.setVisible(true);
    }
}

On running the application inside eclipse, this is what i get - enter image description here enter image description here

Why the double menubar and line? This is very annoying. On cycling through applications or when a popup comes the redrawn window is fine (Right side image).

Also in my DrawPanel paintComponent i have set background as black, but no effect?

What is the reason for above two issues? Please help!

Upvotes: 2

Views: 203

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94635

You are calling Container.paintComponents() method. It must be super.paintComponent(g).

@Override
public void paintComponent(Graphics g) 
{
    super.paintComponent(g); //super.paintComponents(g);
    setBackground(Color.BLACK);
    g.setColor(Color.RED);
    g.drawLine(100, 50, 150, 100);
}

Upvotes: 5

JB Nizet
JB Nizet

Reputation: 691635

The javadoc mentions that

  • isOpaque must be true
  • that depending on the L&F, this property may be ignored
  • that subclasses of JComponent must override paintComponent to honor this property.

Putting setBackground in the constructor, and adding these two lines of code in paintComponent (before painting the red line) makes the panel black.

g.setColor(getBackground());
g.fillRect(getX(), getY(), getWidth(), getHeight());

Also note that Swing components should always be created and modified in the EDT. Your main method should be like this:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            MainFrame appFrame = new MainFrame();
            appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            appFrame.setSize(300, 275);
            appFrame.setVisible(true);
        }
    });
}

Upvotes: 3

Related Questions