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?
Your problem lies in the code here:
The solution to your problem could be done by passing the instance of the
SortsGuiin the constructor of theMyMenuBarand storing it in an instance variable saysGas you have done in your code like below:Now in the action performed method remove the line:
Now in the initialisation of the variable
mbrinSortsGuiclass use the following code:instead of
I think this will solve your problem.