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 7755929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:41:20+00:00 2026-06-01T12:41:20+00:00

I am trying to have my interface dynamically generate a customized button when I

  • 0

I am trying to have my interface dynamically generate a customized button when I click a button. I searched several answers like this, but somehow it does not work. Is there any mistake with my current code below?

  public class MainWindow {

private JFrame frame;
private JPanel panel;
private JPanel panel_1;
private JPanel panel_2;
private JSplitPane splitPane;
private JButton btnSearch;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 645, 438);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    splitPane = new JSplitPane();
    panel.add(splitPane);

    panel_1 = new JPanel();
    splitPane.setLeftComponent(panel_1);

    btnSearch = new JButton("Search");

    GridBagConstraints gbc_btnSearch = new GridBagConstraints();
    gbc_btnSearch.gridx = 0;
    gbc_btnSearch.gridy = 10;
    panel_1.add(btnSearch, gbc_btnSearch);

    panel_2 = new JPanel();
    splitPane.setRightComponent(panel_2);

    btnSearch.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            addButton();
        }
    });
}
protected void addButton() {
    MyButton hahaButton = new MyButton("haha");
    panel_2.add(hahaButton);
    panel_2.add(new JButton());
    panel_2.revalidate();
    panel_2.repaint();
}

And this is the definition of the MyButton:

    public class MyButton extends JButton {

private static final long serialVersionUID = 1L;

private Color circleColor = Color.BLACK;

public MyButton(String label) {
    super(label);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension originalSize = super.getPreferredSize();
    int gap = (int) (originalSize.height * 0.2);
    int x = originalSize.width + gap;
    int y = gap;
    int diameter = originalSize.height - (gap * 2);

    g.setColor(circleColor);
    g.fillOval(x, y, diameter, diameter);
}

@Override
public Dimension getPreferredSize() {
    Dimension size = super.getPreferredSize();
    size.width += size.height;
    return size;
}

}

  • 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-06-01T12:41:21+00:00Added an answer on June 1, 2026 at 12:41 pm

    I just tried your sourcecode and it works as expected: everytime I klick the search button on the left side of the split-pane, 2 buttons are added to the panel on the right side of the panel (one with the black filled circle and a button without a label).

    What doesn’t work for you? I’m using java 1.6 on Mac OSX, but this should work with earlier versions on other platforms as well……

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface like this: @interface AView : UIScrollView { UIView* m_view1; UIView*
I have been trying the following interface IUIntegral : IEquatable<Byte>, IEquatable<UInt16>, IEquatable<UInt32>, IEquatable<UInt64> {
I have the following interface which I'm trying to make COM-visible. When I try
I'm trying to implement a generic java interface in scala. I have looked at:
I am trying to create a class that implements the IUnknown interface. I have
I have a C++ dll which implements several COM interfaces, that I'm trying to
I am trying to call a class method dynamically depending on a condition. This
I'm trying to work on a user interface for adding events. These events have
I am trying to implement an interface where users can dynamically enter text and
I am currently writing a Python script and trying to dynamically generate some arguments.

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.