1

I've created a JFrame. Inside this JFrame, I've created a JPanel. Inside this JPanel I've created another JPanel (lets call it "A").

I've drawn in "A" a rectangle, and wanted to create buttons using graphics. There is no rectangle in my gui. I could see that the paintComponent() method inside "A" is not being invoked.

Code: The JPanels: (the child JPanel is inner class)

public class MemoryPanel extends JPanel {
    
    public MemoryPanel(){
        setPreferredSize(new Dimension(350,448));
    }
    
    @Override 
    public void paintComponent(Graphics g) {    
        //POSITIONING
        setLayout(new BorderLayout());
        
        //CREATE MEMORY BUTTONS
        MemButton a=new MemButton();
        
        //Drawing Rectangles for Memory
        add(a,BorderLayout.CENTER);

    } 
    

    
    private class MemoryButton extends JPanel{
        public MemoryButton(){
            setLayout(null);
            setPreferredSize(new Dimension(87,40));
        }
        
        @Override
        public void paintComponent(Graphics g){
            Graphics2D td= (Graphics2D)g;
            td.drawRect(0, 0, 20, 20);
        }
    }
}
ALUFTW
  • 1,914
  • 13
  • 24
  • 1
    1) The `paintComponent` method should be used to draw graphics, not for creating & adding components. Do that in the constructor. 2) If you're adding the button to `BorderLayout.CENTER`, `setPreferredSize` won't do what you expect. The button will take up the whole available space. 3) Don't forget to call `super.paintComponent()` in the beginning of the `paintComponent` method. Also, is `MemButton` the same as `MemoryButton`? - Please consider to post a [mcve] that reproduces the problem. – Lukas Rotter Apr 27 '16 at 13:46

3 Answers3

2

You should program the JButtons first in order for your graphics to work as buttons. I belive this post will help you with that:

Creating a custom button in Java

I you want a rectangle to be the background for your buttons you can draw it in your main panel and add the buttons on it. Try using different Layouts to mantain some order.

Community
  • 1
  • 1
adriman2
  • 21
  • 5
2

I've made a simple GUI to test your code and the rectangle appears correctly. I made no relevant changes in the code that you posted.

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SimpleJFrameProgram extends JFrame {
     private static final long serialVersionUID = 1L;

     public SimpleJFrameProgram() {
         super("TEST");

         initComponents();

         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.pack();
         this.setLocationRelativeTo(null);
         this.setVisible(true);
     }


    private void initComponents() {
        MemoryPanel memoryPanel = new MemoryPanel();

        this.add(memoryPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new SimpleJFrameProgram();
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
       });
    }
 }

I've applyed minor changes to your MemoryPanel: replaced MemButton by your MemoryButton and fill the rectangle in red to improve its visibility for the test. Without this last change, the rectangle appears too.

  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;

  import javax.swing.JPanel;

  public class MemoryPanel extends JPanel {

    public MemoryPanel(){
         setPreferredSize(new Dimension(350,448));
    }

    @Override
    public void paintComponent(Graphics g) {
        // POSITIONING
        setLayout(new BorderLayout());

        // CREATE MEMORY BUTTONS
        MemoryButton a = new MemoryButton();

        // Drawing Rectangles for Memory
        add(a,BorderLayout.CENTER);

    }

    private class MemoryButton extends JPanel{
        public MemoryButton(){
            setLayout(null);
            setPreferredSize(new Dimension(87,40));
        }

        @Override
        public void paintComponent(Graphics g) {

            Graphics2D td = (Graphics2D) g;
            td.setColor(Color.red);
            td.fillRect(0, 0, 20, 20);
        }
    }
 }

This is the obtained result:

enter image description here

Maybe your problem is located on initializing the parent JFrame.

RubioRic
  • 2,442
  • 4
  • 28
  • 35
  • 1
    Thank you, I've found the problem. I had another package in the same project with the name "MemButton and the object initialized from there". – ALUFTW Apr 27 '16 at 14:19
0

Changing the class name of MemoryButton fixed it.

I had another package with the same class name.

ALUFTW
  • 1,914
  • 13
  • 24