Question

I am trying to draw a Shape object inside an actionPerformed() method of ActionListener class and add it to the InternalFrame.

public class InternalFrame extends JInternalFrame{

    public JSlider redSlider, greenSlider, blueSlider;
    public TextField redField = new TextField();
    public TextField greenField = new TextField();
    public TextField blueField = new TextField();
    Checkbox checkBox = new Checkbox("Filled", true);
    String[] shapeNames = {"Oval", "Rectangle", "Line", "NewShape"};

    public InternalFrame(String string, boolean b,
                    boolean c, boolean d, boolean e) {
        super(string,b,c,d);

        this.setLayout(new BorderLayout());

        redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);
        greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);
        blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);

        redField.setText(" " + redSlider.getValue());
        greenField.setText(" " + greenSlider.getValue());
        blueField.setText(" " + blueSlider.getValue());

        redSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                redField.setText(" " + redSlider.getValue());
                //System.out.println("red value: " + redSlider.getValue());
            }
        });

        greenSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                greenField.setText(" " + greenSlider.getValue());
            }
        });

        blueSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                blueField.setText(" " + blueSlider.getValue());
            }
        });

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(1, 4, 6, 0));

        JComboBox shapesDropDown = new JComboBox(shapeNames);
        shapesDropDown.setSelectedIndex(0);

        shapesDropDown.addActionListener(new ActionListener (){     
            @Override
            public void actionPerformed(ActionEvent arg0) {
                //if(arg0.getSource() == shapeNames[1] ){
                MyShape myShape = new MyRectangle();
                add(myShape, BorderLayout.CENTER);

                System.out.println("HHHH");
                //}
            }
        });

        JPanel empty1 = new JPanel();
        JPanel empty2 = new JPanel();

        topPanel.add(empty1);
        topPanel.add(shapesDropDown);
        topPanel.add(checkBox);
        topPanel.add(empty2);

        JPanel sliderPanel = new JPanel();
        sliderPanel.setLayout(new GridLayout(3, 3));

        sliderPanel.add(new Label("Red:"));
        sliderPanel.add(redSlider);
        sliderPanel.add(redField);
        sliderPanel.add(new Label("Green:"));
        sliderPanel.add(greenSlider);
        sliderPanel.add(greenField);
        sliderPanel.add(new Label("Blue:"));
        sliderPanel.add(blueSlider);
        sliderPanel.add(blueField);

        add(topPanel, BorderLayout.NORTH);
        add(sliderPanel, BorderLayout.SOUTH);
    }
}

The shape object is also getting created, however it is not getting displayed on the InternalFrame.

Was it helpful?

Solution

Instead of extending JInternalFrame, use a factory method to create the internal frame and its content. In this example, createInternalFrame() is called in the constructor, but it can be called from an ActionListener as well. Note that the internal frame's default layout is BorderLayout, and the default placement is CENTER; be sure to pack() the container before adjusting its size and visibility.

image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top