Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:36:41+00:00 2026-05-30T20:36:41+00:00

Edit: Solved, I wasn’t using ‘validate()’ after adding components. I have a GUI class

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:36:42+00:00Added an answer on May 30, 2026 at 8:36 pm

    Calling the method works fine, nothing fancy needed

    You need to add an instance of your HandlerClass to a GUI control, such as a JButton, which will trigger the method when clicked. That is the whole point of ActionListeners (and listeners in general). For example:

    myJButton.addActionListener(new HandlerClass());
    

    A working example based on your code:

    public class Gui extends JFrame
    {
        public Gui()
        {
            super("Physics Helper v0.1");
            JButton b = new JButton("Press me!");
            b.addActionListener(new HandlerClass());
            add(b);
            pack();
            setVisible(true);
        }
    
        public void displayButton(String s)
        {
            System.out.println(s);
        }
    
        private class HandlerClass implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                displayButton("Press me!");
            }
        }
    
        public static void main(String[] args)
        {
            new Gui();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
EDIT: Problem solved. This was (yet another) situation where the problem wasn't really where
-- EDIT -- Solved by using JSON dataType. -- QUESTION -- Hello, I am
EDIT: Solved, my mistake; explained in my answer. I have this: std::vector < boost::shared_ptr
Edit : Solved, there was a trigger with a loop on the table (read
Edit: Solved. Hi, I'm starting with Qt, I try to connect a slot to
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
I have a class with a static method that looks roughly like: class X

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.