Question

Whenever I press "clear" in my Java GUI, it never works, please help me finish this. If i replace "textPanel" with another button, it works, otherwise with "textpanel" it doesn't.

Here is a lightweight version of my code demonstrating the problem:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainFrame extends JFrame {
    private TextPanel textPanel;
    private FormPanel formpanel;

    public MainFrame(){
        super("My Frame");
        createLayout();
        createFrame();
    }

    public void createFrame(){
        setSize(600, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);       
    }

    public void createLayout(){
        BorderLayout myLayout = new BorderLayout();     
        setLayout(myLayout);

        textPanel = new TextPanel();
        formpanel = new FormPanel();

        // adding components
        add(textPanel, BorderLayout.CENTER);
        add(formpanel, BorderLayout.WEST);  
    }

    public static void main(String[] args){
        new MainFrame();
    }

    public static class FormPanel extends JPanel {

        private JButton clear;
        private TextPanel textPanel;

        public FormPanel(){
            clear = new JButton("Clear Cart!");
            textPanel=new TextPanel();
            add(clear);

            clear.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent aev){
                    System.out.println("Test");
                    textPanel.setText("");
                }       
            });
            createGrid();               
        }

        /* this methods simply creates the layout  */ 
        void createGrid(){
            //creating layout
            setLayout(new GridBagLayout());     
            GridBagConstraints gc = new GridBagConstraints();

            gc.gridy++;
            gc.weightx = .1;
            gc.weighty = .1;
            gc.gridx = 2;
            gc.gridy=5;
            gc.anchor = GridBagConstraints.LINE_START; 
            gc.insets = new Insets(0,0,0,0);

            add(clear, gc);

        }
    }

    public static class TextPanel extends JPanel {
        private JTextArea textArea;

        TextPanel (){
            textArea = new JTextArea();
            setLayout(new BorderLayout());

            JScrollPane p = new JScrollPane(textArea);
            add(p, BorderLayout.CENTER);
        }

        public void appendSomeText(String t){
            textArea.append(t);     
        }

        public void setText(String s){
            textArea.setText(s);
        }
    }
}
Was it helpful?

Solution

You have two instances of TextPanel, one in MainFrame and the other one in FormPanel. TextPanel that is defined in FormPanel is actually not added to the panel, so textPanel.setText(""); has no effect on it as it is not visible.

When the text appended with Add To Cart! button it actually goes through a method in MainFrame - formEventOccurred() that executes textPanel.appendSomeText(). This is the other instance of TextPanel that is part of MainFrame and that is actually visible.

Looks like you need to move the duplicated logic from the main frame to the panels. Usually you should not extend JFrame as you are not adding any new functionality.

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