1

Yes, I am mixing awt and swing components, but maybe there is an easy fix because I dont know Java all that well.

My canvas object overrides paint and update:

package demo;
import java.awt.*;

public class rectangle extends Canvas {
    public rectangle() {
        this.setSize(500,300);
        this.setVisible(true);
    }
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.YELLOW);

        g2.fill3DRect(0, 0, 500, 300, true);
    }
    public void update(Graphics g) { paint(g); }
}

When my JComboBox opens over top of this it doesnt paint on top of it. As an example, here is a JFrame that demonstrates what i'm talking about:

package demo;
import javax.swing.*;
import java.util.*;
import java.awt.*;

public class ASframe extends JFrame {
    public ASframe() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ArrayList listNames = new ArrayList();
        listNames.add("One");
        listNames.add("Two");
        listNames.add("Three");
        listNames.add("Four");

        rectangle r = new rectangle();
        JComboBox listBox = new JComboBox(listNames.toArray());
        listBox.setVisible(true);

        JPanel listPane = new JPanel();
        listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
        listPane.add(listBox);
        listPane.add(r);

        this.setResizable(false);
        this.add(listPane);
        this.pack();
    }
    public static void main(String arg[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ASframe().setVisible(true);
            }
        });
    }
}

Whats really interesting is that if the rectangle is smaller then the JComboBox, no issues at all. So, change the rectangle to 300x20 and it works as expected.

Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bryan Bueter
  • 95
  • 1
  • 5
  • 3
    "*Yes, I am mixing awt and swing components, but maybe*"..No, do not mix heavyweight and lightweight UI objects. – mre Nov 02 '12 at 14:31
  • 5
    Use JPanel instead of Canvas, and better paintComponent i.o. paint. – Joop Eggen Nov 02 '12 at 14:44
  • Ok, JPanel with setPreferredSize seems to satisfy what i need. Thanks both of you for the quick response! – Bryan Bueter Nov 02 '12 at 15:01
  • @mre mixing is no longer an issue (since a late jdk6 update release), but agree: don't except there's a pressing need. – kleopatra Nov 02 '12 at 15:54
  • _setPreferredSize seems to satisfy what i need_ then you are doing something wrong somewhere: as a general rule, [do not use any of the setXXSize methods](http://stackoverflow.com/q/7229226/203657), instead override the getXXSize as appropriate. BTW: please learn java naming conventions and stick to them. – kleopatra Nov 02 '12 at 15:57
  • @kleopatra: Ok, I did not know this. I'm sorry for not conforming to naming conventions in my demo code, I threw it together. Not a java programmer really, google taught me everything i know, which apparently is not much. Thanks again. – Bryan Bueter Nov 02 '12 at 17:16

1 Answers1

2

Try telling Swing use the heavyweight component and see if that works.

JComboBox listBox = new JComboBox(listNames.toArray());
listBox.setVisible(true);

// additional line below    
listBox.setLightWeightPopupEnabled(false); // use heavyweight component
Greg Jennings
  • 1,611
  • 16
  • 25