Manipulating components in a separate class java

79 views Asked by At

I have three java classes

StitchSorts

import Sorts.*;

public class StitchSorts
{   
    public static void main(String[] args)
    {
        StitchSorts sS = new StitchSorts();
        SortsGui.main(args);
    }
}

SortsGui

package Sorts;

import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
import Sorts.*;
import javax.swing.event.*;
import java.awt.event.*;

public class SortsGui
{
    JFrame myMainWindow = new JFrame("Sorts");

    JPanel sortPanel = new JPanel();

    MyMenuBar mbr = new MyMenuBar();

    JTextField txtField = new JTextField();
    JTextField txtField2 = new JTextField();

    public void runGUI()
    {
        myMainWindow.setBounds(10, 10, 800, 800);
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1,1));

        createSortTestPanel();

        myMainWindow.getContentPane().add(sortPanel);

        myMainWindow.setJMenuBar(mbr);

        myMainWindow.setVisible(true);
    }

    public void createSortTestPanel()
    {
        MigLayout layout = new MigLayout("" , "[grow]");
        sortPanel.setLayout(layout);

        sortPanel.add(txtField,"growx");
        sortPanel.add(txtField2,"growx");
    }

    public void clearTextBoxes()
    {
        for (Component C : sortPanel.getComponents())
        {    
            if (C instanceof JTextField)
            {
                ((JTextField) C).setText("");
                System.out.println("Multiple");
            }
        }
    }

    public static void main(String[] args)
    {
        SortsGui sG = new SortsGui();
        sG.runGUI();
    }
}

MyMenuBar

package Sorts;

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

import java.io.*;
import java.lang.*;

public class MyMenuBar extends JMenuBar
{
    JButton btnClear = new JButton("Clear");
    SortsGui sG;

    public MyMenuBar()
    {
        setBorderPainted(true);
        makePopUpMenu();
    }

    void makePopUpMenu()
    {
        add(Box.createHorizontalGlue());
        clearButton(btnClear);
        add(btnClear);
    }

    public void clearButton(JButton J)
    {
        J.setOpaque(false); //These remove the button filling and border
        J.setContentAreaFilled(false);
        J.setBorder(null);
        J.setMinimumSize(new Dimension(50,25));
        J.setPreferredSize(new Dimension(50,25));
        J.setMaximumSize(new Dimension(50,25));
        J.addActionListener(new buttonPress());
        J.setFocusable(false);
    }

    class buttonPress implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {               
            if(e.getSource() == btnClear)
            {
                System.out.println("Clearing");
                sG = new SortsGui();
                sG.clearTextBoxes();
            }
        }
    }
}

What I am trying to achieve here is when the JButton btnClear is clicked the JTextFields in SortsGui should be cleared. The public void clearTextBoxes I set up to achieve this works when it is called in SortsGui. However when the method is called by the btnClear being clicked, it does not detect any JTextFields on the JPanel sortPanel. Is there a way to correct this? If so what is the best way to accomplish this?

1

There are 1 answers

3
Blip On BEST ANSWER

Your problem lies in the code here:

    public void actionPerformed(ActionEvent e)
    {               
        if(e.getSource() == btnClear)
        {
            System.out.println("Clearing");
            sG = new SortsGui(); // You are creating a new instance of SortsGui
            sG.clearTextBoxes(); //You are calling the method on this new instance
        }
    }

The solution to your problem could be done by passing the instance of the SortsGui in the constructor of the MyMenuBar and storing it in an instance variable say sG as you have done in your code like below:

public MyMenuBar(SortsGui sG)
{
    this.sG = sG;
    setBorderPainted(true);
    makePopUpMenu();
}

Now in the action performed method remove the line:

  sG = new SortsGui();

Now in the initialisation of the variable mbr in SortsGui class use the following code:

MyMenuBar mbr = new MyMenuBar(this);

instead of

MyMenuBar mbr = new MyMenuBar();

I think this will solve your problem.