Edit: Solved, I wasn’t using ‘validate()’ after adding components.
I have a GUI class structured something like this (this is a very basic representation of my code):
Edit: here’s my full code
package source;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Gui extends JFrame
{
public String[] list1 = {"equation1","equation2","equation3","equation4", "equation5"};
public JOptionPane opt1;
private JButton custom;
private JTextField[] tf;
public HandlerClass2 itemhandler = new HandlerClass2();
private JList list;
private static int index = 0;
private static int lastlistindex = 0;
private JPanel buttonpanel;
private JPanel buttonpanel2[] = new JPanel[3];
private JPanel listpanel[] = new JPanel[4];
private JPanel checkpanel;
private JCheckBox checkboxes[];
private SpringLayout layout;
public Container contentPane;
private JButton but;
public Gui()
{
super("Physics Helper v0.1");
setBackground(Color.DARK_GRAY);
layout = new SpringLayout();
contentPane = getContentPane();
contentPane.setLayout(layout);
displayPortal();
}
public void displayPortal()
{
Icon a = new ImageIcon(getClass().getResource("button.png"));
Icon b = new ImageIcon(getClass().getResource("button2.png"));
custom = new JButton("", a);
custom.setRolloverIcon(b);
buttonpanel = new JPanel();
buttonpanel.setBackground(Color.GRAY);
buttonpanel.add(custom);
contentPane.add(buttonpanel);
layout.putConstraint(SpringLayout.WEST, buttonpanel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.EAST, buttonpanel, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, buttonpanel, 5, SpringLayout.NORTH, contentPane);
custom.addActionListener(new HandlerClass());
}
public void displayButton(String s)
{
but = new JButton(s);
buttonpanel2[index] = new JPanel();
buttonpanel2[index].setBackground(Color.GRAY);
buttonpanel2[index].add(but);
contentPane.add(buttonpanel2[index]);
layout.putConstraint(SpringLayout.SOUTH, buttonpanel2[index], -5, SpringLayout.SOUTH, contentPane);
if (index < 1)
{
layout.putConstraint(SpringLayout.WEST, buttonpanel2[index], 5, SpringLayout.WEST, contentPane);
}
else
{
layout.putConstraint(SpringLayout.WEST, buttonpanel2[index], 5, SpringLayout.EAST, buttonpanel2[index - 1]);
}
index++;
}
public void displayList(String[] t)
{
list = new JList(t);
list.setVisibleRowCount(8);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(list));
listpanel[lastlistindex] = new JPanel();
listpanel[lastlistindex].setBackground(Color.GRAY);
listpanel[lastlistindex].add(list);
contentPane.add(listpanel[lastlistindex]);
layout.putConstraint(SpringLayout.NORTH, listpanel[lastlistindex], 5, SpringLayout.SOUTH, buttonpanel);
if (lastlistindex < 1)
{
layout.putConstraint(SpringLayout.WEST, listpanel[lastlistindex], 5, SpringLayout.WEST, contentPane);
}
else
{
layout.putConstraint(SpringLayout.WEST, listpanel[lastlistindex], 5, SpringLayout.EAST, listpanel[lastlistindex - 1]);
}
lastlistindex++;
}
public void displayInputValues(String[] p)
{
checkboxes = new JCheckBox[p.length];
GridLayout gridlayout = new GridLayout(p.length, 2);
tf = new JTextField[p.length];
checkpanel = new JPanel();
checkpanel.setBackground(Color.GRAY);
checkpanel.setLayout(gridlayout);
for (int b = 0; b < p.length; b++)
{
checkboxes[b] = new JCheckBox(p[b]);
checkpanel.add(checkboxes[b]);
tf[b] = new JTextField("", 9);
checkpanel.add(tf[b]);
tf[b].setFont(new Font("Serif", Font.PLAIN, 14));
}
contentPane.add(checkpanel);
layout.putConstraint(SpringLayout.EAST, checkpanel, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, checkpanel, -5, SpringLayout.SOUTH, contentPane);
}
private class HandlerClass implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
displayButton("Back");
displayButton("Next");
displayList(list1);
}
}
My main method is contained within another class, and works fine.
My question is how can I call the “displayButton” method in the actionPerformed method? I’ve tried a few tips already, such as calling it with “Gui.this.displayButton(“Press me!”).
I have tested every other aspect of my code, and this seems to be the only problem.
I get no errors when I run the code.
If needs be I can post the full class, but I think this problem lies in trying to call these methods.
What’s your opinion?
Calling the method works fine, nothing fancy needed
You need to add an instance of your
HandlerClassto a GUI control, such as aJButton, which will trigger the method when clicked. That is the whole point ofActionListeners(and listeners in general). For example:A working example based on your code: