Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom painting of JPanel
    text
    copied!<p>I'm not very good at this and I hope to get some help from people who understands the issue a lot more that I do. </p> <p>So here's the deal. In my application there is background JPanel with Image drawn over it. Then there is a small JPanel which I'm trying to create custom painting for. I wanted to have JPanel with rounded corners and semi-transparent background so I modified paintComponent method to fill semi-transparent rounded rectangle. But when I place components inside like say JComboBox, the list of items appears and I click somewhere else to close it JPanel paints itself in original way making it semitransparent all around but with small rectangle painted with original grey background color. I see that it has to do something with invoking paintComponent on its parrent or paintChildren but I don't know how to organize those methods or where to put them. I also have proble with transparent colors overlaping each other.</p> <p>Here is an example source code:</p> <pre><code>public class RoundedPanel extends JPanel { private final int radius; public RoundedPanel(int cornerRadius) { radius=cornerRadius; } public void paintComponent(Graphics g) { Color bg = getBackground(); g.setColor(new Color(bg.getRed(),bg.getGreen(),bg.getBlue(),40)); g.fillRoundRect(0,0, getWidth()-1, getHeight()-1, radius, radius); g.setColor(new Color(0,0,0,70)); g.drawRoundRect(0,0, getWidth()-1, getHeight()-1, radius, radius); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 300); frame.setLocation(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel content = new JPanel(); JPanel wl = new JPanel(); JPanel el = new JPanel(); JPanel sl = new JPanel(); JPanel nl = new JPanel(); RoundedPanel rp = new RoundedPanel(50); JComboBox combobox = new JComboBox(); frame.setContentPane(content); content.setBackground(Color.red); content.setLayout(new BorderLayout()); wl.add(new JButton("west")); el.add(new JButton("east")); sl.add(new JButton("south")); nl.add(new JButton("north")); content.add(wl,BorderLayout.WEST); content.add(el,BorderLayout.EAST); content.add(nl,BorderLayout.NORTH); content.add(sl,BorderLayout.SOUTH); content.add(rp,BorderLayout.CENTER); rp.setBackground(Color.BLACK); combobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Třída 1.B", "Třída 1.C", "Třída 2.C" })); rp.add(combobox); frame.setVisible(true); } } </code></pre> <p>I hope some of will help me out :-) thanks</p> <p>EDIT: I found out that JComboBox (and its pop-up menu) draws correctly if pop-up menu overlaps outside the JPanel that contains JComboBox and has the custom paintComponent method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload