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

  • Home
  • SEARCH
  • 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 8723835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:42:39+00:00 2026-06-13T07:42:39+00:00

im trying to create a custom JPanel which i add into a card layout

  • 0

im trying to create a custom JPanel which i add into a card layout in my GUI set up. in this custom layout i use a gridlayout and add buttons directly into a container. i now have a need to add actionlisteners to these buttons and dont know how. any help would be appreciated and please find the custom class below.

import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class PuzzleSudokuPanel extends JPanel{
  int[][] grid =     
{{0, 0, 0, 5, 0, 0, 8, 3, 1},
 {0, 5, 3, 0, 0, 8, 7, 0, 0},      
 {7, 0, 0, 0, 4, 2, 0, 6, 0},
 {0, 0, 9, 0, 3, 0, 0, 7, 8},
 {0, 0, 2, 4, 0, 6, 1, 0, 0},
 {6, 3, 0, 0, 9, 0, 2, 0, 0},
 {0, 1, 0, 7, 6, 0, 0, 0, 9},
 {0, 0, 4, 9, 0, 0, 5, 8, 0},
 {2, 9, 7, 0, 0, 3, 0, 0, 0}};
Container myGrid = new Container();

public PuzzleSudokuPanel ()throws NullPointerException{
  try{
    for(int i = 0; i<9; i++){
      for(int j = 0; j<9; j++){
        String appropriateNumber = convertSimple(grid[i][j]);
        myGrid.add(new JButton(appropriateNumber));
      }
    }
  }
  catch(NullPointerException e){

  }
  myGrid.setLayout(new GridLayout(9, 9));
  myGrid.setPreferredSize (new Dimension(400, 400)); 

  add(myGrid);
  }
    public static String convertSimple(int a){
    return ("" + a);
  }
}

[edit]

calkuro code:

import java.awt.*; import java.awt.event.*; import javax.swing.*;

 public class PuzzleCalkuroPanel extends JPanel{
  String[][] grid =     
  {{"3/", "3/", "3-", "8x"},
    {"9+", "9+", "3-", "8x"},      
    {"9+", "9+2", "9+2", "8x"},
    {"1", "9+2", "1-", "1-"}};
  Container myGrid = new Container();

  public PuzzleCalkuroPanel ()throws NullPointerException{
    try{
      for(int i = 0; i<4; i++){
        for(int j = 0; j<4; j++){
          JButton button = new JButton(grid[i][j]);
          button.addActionListener(new calkuroListener());
          myGrid.add(new JButton(grid[i][j]));
        }
      }
    }
    catch(NullPointerException e){

    }
    myGrid.setLayout(new GridLayout(4, 4));
    myGrid.setPreferredSize (new Dimension(400, 400)); 

    add(myGrid);
  }
  private class calkuroListener implements ActionListener{
    public void actionPerformed (ActionEvent event){
      String numStr = JOptionPane.showInputDialog("Enter a number to change to: ");
      JOptionPane.showMessageDialog(null, numStr);
    }
  }
}
  • 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-13T07:42:40+00:00Added an answer on June 13, 2026 at 7:42 am

    Instead of…

    myGrid.add(new JButton(appropriateNumber));
    

    Do…

    JButton button = new JButton(appropriateNumber);
    button.addActionListener(...);
    myGrid.add(button);
    

    UPDATED

    This is what you’re doing…

    JButton button = new JButton(grid[i][j]); // Create button here, good...
    button.addActionListener(new calkuroListener()); // Add listener here, good...
    myGrid.add(new JButton(grid[i][j])); // Create a new button here, bad...
    

    This is what you should be doing

    JButton button = new JButton(grid[i][j]); // Create button here, good...
    button.addActionListener(new calkuroListener()); // Add listener here, good...
    myGrid.add(button); // Add button here, good...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a custom User library in CodeIgniter. Inside this library
I'm trying to create a custom ArrayAdapter to use for my ListFragment . Right
I am trying to create a JPanel which has 3 components arranged in a
I am trying to create a custom event which listens and responses to the
I'm trying to create custom attributes to my button but I dont know which
I'm trying to create a custom DQL function to allow me to use the
Trying to create a custom component that gets it's layout from an XML file
I am trying to create a Custom Attribute for a ServiceStack Service with which
I am trying to create custom bullet points and it is not working. This
I'm trying to create custom layout for android. It normally draws on screen, but

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.