1

why when I add a component

config.add(my_cirle);

this is not added

intellij tells me in purple enter image description here public static void main(String[] args){

    JFrame app = new JFrame("Draw Draw");
    JButton cirle = new JButton("Circle");
     final JPanel config = new JPanel();

    cirle.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int cirle = Integer.parseInt(JOptionPane.showInputDialog("Insert circumference"));
            Cirle my_cirle = new Cirle(cirle);
            config.add(my_cirle);  //this Problem
        }
    });

    config.add(cirle);
    app.add(config);
    app.setVisible(true);
    app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    app.setSize(300,300);
}

this in my class Circle it work:

    public class Cirle extends JComponent{
    private int width,height;
    private Ellipse2D miniCircle;
    public Cirle(int radius){
        miniCircle = new Ellipse2D.Double(0,0,2*radius,2*radius);
    }
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;

        g2.draw(miniCircle);
    }

}

this work:

    public static void main(String[] args){

    JFrame app = new JFrame("Draw Draw");

    int cirle = Integer.parseInt(JOptionPane.showInputDialog("Insert circumference"));
    Cirle my_cirle = new Cirle(cirle);

    app.add(my_cirle);
    app.setVisible(true);
    app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    app.setSize(300,300);
}

enter image description here

Aymen
  • 1,476
  • 18
  • 28
  • 1
    Don't forget to `revalidate()` and `repaint()` the container after adding or removing components from it. – Hovercraft Full Of Eels Nov 30 '17 at 14:52
  • @HovercraftFullOfEels i tested, but not work?? – Aymen Nov 30 '17 at 14:56
  • Then post a valid [mcve] with code that **we** can compile and test unchanged. – Hovercraft Full Of Eels Nov 30 '17 at 14:57
  • It would help to know what Circle is. Your own class that extends `JComponent`? [javafx.scene.shape.Circle](https://docs.oracle.com/javase/9/docs/api/javafx/scene/shape/Circle.html)? – AJNeufeld Nov 30 '17 at 14:58
  • 1
    @AJNeufeld: exactly, which is why I want to see his MCVE. As it stands if the question is not a duplicate, then it remains very incomplete. – Hovercraft Full Of Eels Nov 30 '17 at 15:00
  • 1
    @Aymen: no code in comments please. Again update your question by [edit] and show your [mcve] there. – Hovercraft Full Of Eels Nov 30 '17 at 15:00
  • 1
    OK, you're misunderstanding the problem. Your object **is** being added to the JPanel, but its default size, its `preferredSize` is 0,0 so no graphics are being seen. Either change its preferred size or the layout of the container that you're adding it to so that it displays and has a decent size. – Hovercraft Full Of Eels Nov 30 '17 at 15:05
  • @HovercraftFullOfEels whitout ActionListener work all, the problem is whit ActionListener – Aymen Nov 30 '17 at 15:06
  • [for example](https://stackoverflow.com/questions/11376252/not-showing-graphics-in-jpanel-which-is-added-to-another-jpanel) – Hovercraft Full Of Eels Nov 30 '17 at 15:07
  • no :(, i tested – Aymen Nov 30 '17 at 15:14
  • `the problem is whit ActionListener` - and you were give the answer 1) by default a component has a size of (0, 0) so there is nothing to paintt, so you need to override the getPreferredSize() method. 2) When you add a component you need to invoke the layout manager so the layout manager can give the component a size and location. This is done with the revalidate(), Post your updated [mcve] AFTER making these changes if you need more help. – camickr Nov 30 '17 at 15:46

0 Answers0