Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Calling methods in a nested class, specifically an ActionListener
    primarykey
    data
    text
    <p>Edit: Solved, I wasn't using 'validate()' after adding components. </p> <p>I have a GUI class structured something like this (this is a very basic representation of my code):</p> <p>Edit: here's my full code</p> <pre><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 &lt; 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 &lt; 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 &lt; 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); } } </code></pre> <p>My main method is contained within another class, and works fine.</p> <p>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!").</p> <p>I have tested every other aspect of my code, and this seems to be the only problem.</p> <p>I get no errors when I run the code.</p> <p>If needs be I can post the full class, but I think this problem lies in trying to call these methods.</p> <p>What's your opinion?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload