Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repaint JPanel which is inside another JPanel

Tags:

java

swing

jpanel

Here is the thing:

public class ParentPanel extends JPanel{
    private ChildPanel childPanel;
    public ParentPanel() {
        super();
        initComponents();
    }
    public void pushData(int data){
        childPanel.pushData(data);
    }
    private void initComponents() {
        childPanel= new ChildPanel();
}

I have another class:

public class ChildPanel extends JPanel{
    public ChildPanel(){
        super();
    }
    public void paintComponent(Graphics g){
        //I draw some lines in this JPanel according to data
    }
    public void pushData(int data){
        repaint();
    }
}

What I want is to draw some lines when the pushData() method inside ChildPanel is called. But now there is no responding inside the ChildPanel. What should I do?

like image 576
Cacheing Avatar asked Aug 09 '12 05:08

Cacheing


1 Answers

childPanel is not part of any container, its paintComponent is not going to be called. Try adding it as a child to ParentPanel, ie inside initComponents call add(childPanel).

EDIT: sample based on posted code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestChild {
    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final ParentPanel parent = new ParentPanel();
        JButton button = new JButton("Push");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                parent.pushData(7);
            }
        });

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        panel.add(parent, BorderLayout.CENTER);
        panel.add(button, BorderLayout.SOUTH);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public static class ParentPanel extends JPanel {
        private ChildPanel childPanel;

        public ParentPanel() {
            super();
            initComponents();
        }

        public void pushData(int data) {
            childPanel.pushData(data);
        }

        private void initComponents() {
            childPanel = new ChildPanel();
            add(childPanel);
        }
    }

    public static class ChildPanel extends JPanel {
        private Random random = new Random();

        public ChildPanel() {
            super();
            setPreferredSize(new Dimension(200, 200));
        }

        @Override
        public void paintComponent(Graphics g) {
            System.out.println(getWidth());
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.getHSBColor(random.nextFloat(), 0.9f, 1.0f));
            g.fillOval(0, 0, getWidth(), getHeight());
        }

        public void pushData(int data) {
            repaint();
        }
    }
}
like image 53
tenorsax Avatar answered Sep 30 '22 15:09

tenorsax