How to remove NullPointerExceptions due to JButton array - java

I'm terribly unfamiliar with swing, but here I'm trying to make a "clacker" game where each time two dice are rolled, either you click the two (or 1 if they're the same number) buttons representing the two numbers or just one button representing the sum of the two numbers. If a button is clicked, visible is set to false. I have a JButton array to hold the 12 buttons, but every time a reference is made to the array buttons, it throws a NullPointerException. I've tried putting the code making the JButton array within the constructor and actionPerformed, but it still throws the same exception.
Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Clacker implements ActionListener
{
JPanel panel;
JFrame frame;
JLabel dieFace1, dieFace2, numTurns;
JButton[] buttons;
JButton rollDie, reset;
String turn="Turns: ";
int turns, numCovered;
public Clacker()
{
frame = new JFrame("clacker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel=new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(50,30,50,30));
panel.setBackground(Color.gray);
panel.setLayout(new GridLayout(5,10,1,1));
dieFace1=new JLabel(new ImageIcon("die3.jpg"));
dieFace1.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
dieFace1.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
panel.add(dieFace1);
dieFace2=new JLabel(new ImageIcon("die3.jpg"));
dieFace2.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
dieFace2.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
panel.add(dieFace2);
rollDie=new JButton("Roll Die");
rollDie.setAlignmentX(JButton.RIGHT_ALIGNMENT);
rollDie.addActionListener(this);
panel.add(rollDie);
numTurns=new JLabel(turn+turns);
numTurns.setAlignmentX(JLabel.LEFT_ALIGNMENT);
numTurns.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
panel.add(numTurns);
setArray();
reset=new JButton("Reset");
reset.setAlignmentX(JButton.LEFT_ALIGNMENT);
reset.addActionListener(this);
panel.add(reset);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void setArray()
{
JButton[] buttons = new JButton[12];
for (int i=0; i<12; i++)
{
buttons[i]= new JButton(i+1+"");
buttons[i].setActionCommand(i+"");
buttons[i].addActionListener(this);
panel.add(buttons[i]);
buttons[i].setEnabled(false);
System.out.println(buttons[i]);
}
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("Roll Die"))
{
Random asdf = new Random();
int newRoll= asdf.nextInt(6)+1;
dieFace1.setIcon(new ImageIcon("die"+newRoll+".jpg"));
Random asdf2 = new Random();
int newRoll2= asdf2.nextInt(6)+1;
dieFace2.setIcon(new ImageIcon("die"+newRoll2+".jpg"));
int sum=newRoll+newRoll2;
turns++;
numTurns.setText(turn+turns);
for (int i=0; i<7; i++) ////here
{
if (i==newRoll)
buttons[i].setEnabled(true);
if (i==newRoll2)
buttons[i].setEnabled(true);
}
for (int i=7; i<13; i++)
{
if (sum==i)
buttons[i].setEnabled(true);
}
String eventName = event.getActionCommand();
for (int b=1; b<13; b++)
{
if (eventName.equals(buttons[b].getActionCommand())) ////here
{
if (Integer.parseInt(eventName)>6)
{
buttons[b].setText("");
numCovered++;
buttons[b].setVisible(false);
for (int i=0; i<7; i++)
buttons[i].setEnabled(false);
}
}
}
for (int i=0; i<buttons.length; i++) ////here
buttons[i].setEnabled(false);
if (numCovered==12)
{
rollDie.setEnabled(false);
numTurns.setText("Win! "+turns+" turns");
}
}
else if (event.getActionCommand().equals("Reset"))
{
turns=0;
numTurns.setText(turn+turns);
rollDie.setEnabled(true);
numCovered=0;
for (int i=0; i<buttons.length; i++) ////here
{
buttons[i].setVisible(true);
buttons[i].setEnabled(false);
}
}
}
private static void runGUI()
{ JFrame.setDefaultLookAndFeelDecorated(true); Clacker game = new Clacker();}
public static void main(String[] args)
{ javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() { runGUI();}});}
}

In setArray you're declaring a local variable instead of referencing the class member variable. Just change
JButton[] buttons = new JButton[12];
to
this.buttons = new JButton[12];

Related

How can I enable/disable a JTextField with a JCheckBox? or what's wrong with my code?

I'm newbie in programming java, I have an array of JCheckBox next to an array of JTextfield.
I need to make a CheckBox Deactivate a JTextField when it's checked, but I don't have success in this
How can I make it works with actionlisteners?
This is my code:
public class Checklist_Complete extends JFrame {
private JLabel description;
private JButton send;
private JTextField text[]=new JTextField[10];
private JCheckBox cb[]=new JCheckBox[10];
public Checklist_Complete() {
setTitle("Activities");
setSize(500,300);
setupWidgets();
setVisible(true);
}
private void setupWidgets() {
JPanel pn_center = new JPanel(new GridLayout(10,1));
JPanel pn_west = new JPanel(new GridLayout(10,1));
description = new JLabel("List your activities and uncheck the irrelevant ones");
send = new JButton("Send Checklist");
for (int i=0; i<10; i++) {
text[i] = new JTextField();
cb[i] = new JCheckBox("", true);
}
add(description, BorderLayout.NORTH);
add(pn_center, BorderLayout.CENTER);
add(pn_west, BorderLayout.WEST);
add(send, BorderLayout.SOUTH);
for (int i=0; i<10; i++){
pn_center.add(text[i]);
pn_west.add(cb[i]);
}
}
private void setupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0; i<10; i++) {
cb[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if(cb[i].isSelected()){
text[i].setEnabled(false);
} else{
text[i].setEnabled(true);
}
}
});
}
}
public static void main(String[] args) {
new Checklist_Complete();
}
}
Here is a quick solution with an ItemListener.
private void setupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0; i<10; i++) {
final int finalI = i;
cb[i].addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
text[finalI].setEnabled(e.getStateChange() == ItemEvent.SELECTED);
}
});
}
}
You can also do it with an ActionListener but the solution is a bit of a hack, and it is not as elegant. I am posting this because you can solve your issue like this also:
private void setupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0; i<10; i++) {
final int finalI = i;
cb[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
text[finalI].setEnabled(!text[finalI].isEnabled() && e.getID() == ActionEvent.ACTION_PERFORMED);
}
});
}
}
Problems:
You never call setupEvents() and so the code in this method is never called
You will need to make local fields final if you want to use them within an inner anonymous class.
So:
private void setupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0; i<10; i++) {
final int finalIndex = i;
cb[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if(cb[finalIndex].isSelected()){
text[finalIndex].setEnabled(false);
} else{
text[finalIndex].setEnabled(true);
}
}
});
}
}
I would do things a little differently, and to my eye cleaner. e.g.,
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
#SuppressWarnings("serial")
public class CheckList2 extends JPanel {
public static final int TEXT_FIELD_COUNT = 10;
private List<JTextField> fields = new ArrayList<>();
private JButton sendBtn = new JButton(new SendAction("Send Checklist"));
public CheckList2() {
JPanel checkPanel = new JPanel();
checkPanel.setLayout(new GridLayout(0, 1, 1, 1));
for (int i = 0; i < TEXT_FIELD_COUNT; i++) {
JCheckBox checkBox = new JCheckBox("", true);
// final so can use within item listener
final JTextField textField = new JTextField(20);
textField.setEnabled(false);
fields.add(textField);
checkBox.addItemListener(itemEvent -> {
// set textfield enabled based on checkbox state
textField.setEnabled(itemEvent.getStateChange() == ItemEvent.DESELECTED);
});
JPanel rowPanel = new JPanel();
rowPanel.add(checkBox);
rowPanel.add(Box.createHorizontalStrut(3));
rowPanel.add(textField);
checkPanel.add(rowPanel);
}
setLayout(new BorderLayout());
add(checkPanel, BorderLayout.CENTER);
add(sendBtn, BorderLayout.PAGE_END);
}
private class SendAction extends AbstractAction {
public SendAction(String name) {
super(name);
int mnemonic = name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
for (JTextField jTextField : fields) {
System.out.println(jTextField.getText());
}
System.out.println();
}
}
private static void createAndShowGui() {
CheckList2 mainPanel = new CheckList2();
JFrame frame = new JFrame("CheckList2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

how to remove two jbuttons that have the same values and same colors by clicking them?

I have a matrix of colorful buttons and I need to compare their colors and their values after clicking them. All my code does is removing every button without searching their values or colors.Also i need to add the jlabel without rearranging jbuttons. How can i fix the problem?
public class Legos2 extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public JPanel jp = (JPanel)this.getContentPane();
public JButton[][] jb = new JButton[12][24];//my matrix
public static JLabel jl = new JLabel("score",SwingConstants.CENTER);//a jlabel to count the scores every time two buttons are removed
public Legos2() {
super();
this.setSize(2000,2000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
jp.setLayout(new GridLayout(12, 24));//size of my jpanel
Random rnd = new Random();
Color[] c = {Color.lightGray, Color.gray, Color.white, Color.cyan};//colored jbuttons
final int MAXCOLOR = c.length;
JButton jb1;
for(int i = 0;i <jb.length;i++) {
for(int j = 0; j<jb[i].length; j++) {
int k = (int) (Math.random()*9+1);//k is a random integer from 1 to 9
jb1 = new JButton(Integer.toString(k));
add(jb1);
jb[i][j]=jb1;
jb[i][j].addActionListener(this);
}
}
for(int i=0; i<jb.length; i++){
for(int j = 0; j<jb[i].length; j++) {
jb[i][j].setBackground(c[rnd.nextInt(MAXCOLOR)]);//i add colors in here
}
}
for (int row = 0; row < jb.length; row++) {
for (int column = 0; column < jb[row].length; column++) {
jb[row][column].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String buttonText = button.getText();
// now iterate over all the jbuttons you have
for(int i=0;i<jb.length;i++){
for(int j=0;j<jb[0].length;j++){
JButton b = jb[i][j];
String bText = b.getText();
if(e.getSource()==b)
if(buttonText.equals(bText)){
b.setEnabled(false);
jl.setText(Integer.toString(i));
}
}
}
}
}
);
jp.add(jb[row][column]);
}
}
}
}
The following is mcve of the required functionality. Please review and ask for clarifications as needed:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Legos2 extends JFrame{
private static final long serialVersionUID = 1L;
private static final int ROWS = 12, COLS =24;
public JButton[][] jb = new JButton[ROWS][COLS];
private int score = 0;
public static JLabel jl;
public Legos2() {
super();
this.setSize(2000,2000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp= new JPanel();
jp.setLayout(new GridLayout(ROWS, COLS));
Random rnd = new Random();
Color[] c = {Color.lightGray, Color.gray, Color.white, Color.cyan};
final int MAXCOLOR = c.length;
//all can be done in one loop
for(int i = 0;i <jb.length;i++) {
for(int j = 0; j<jb[i].length; j++) {
int k = (int) (Math.random()*9+1);//k is a random integer from 1 to 9
JButton button = new JButton(Integer.toString(k));
button.setBackground(c[rnd.nextInt(MAXCOLOR)]);
button.addActionListener(e -> {
disableTwoMatchingButtons(button);
});
jb[i][j]=button;
jp.add(button);
}
}
add(jp); //add panel at content pane center
jl= new JLabel("",SwingConstants.CENTER);
updateScore(0);
add(jl, BorderLayout.PAGE_END); //add label at content pane bottom
pack();
setVisible(true);
}
private void disableTwoMatchingButtons(JButton button) {
for(int i=0;i<jb.length;i++){
for(int j=0;j<jb[0].length;j++){
JButton b = jb[i][j];
if(b.isEnabled() && button.getText().equals(b.getText()) &&
button.getBackground().equals(b.getBackground())){
b.setEnabled(false);
button.setEnabled(false);
updateScore(2* Integer.valueOf(b.getText()));
return;
}
}
}
}
private void updateScore(int value) {
score += value;
jl.setText("Score: "+ score );
}
public static void main(String[] args) {
new Legos2();
}
}
If you need to disable all matching button, and not just two, use :
private void disableAllMatchingButtons(JButton button) {
for(int i=0;i<jb.length;i++){
for(int j=0;j<jb[0].length;j++){
JButton b = jb[i][j];
if(b.isEnabled() && button.getText().equals(b.getText())
&& button.getBackground().equals(b.getBackground())){
b.setEnabled(false);
updateScore(Integer.valueOf(b.getText()));
}
}
}
}
(instead of disableTwoMatchingButtons)
For future post always consider posting an mcve, and stick to one question per post.

need help accessing variable inside inner class for loop

I am having problem accessing the variable int i inside the inner class
Code:
public class OnColumnSelectPanel {
JFrame jFrame;
JPanel mainJPanel;
//JPanel jPanel1;
//JTextField jTextField;
//JButton jButton;
//JComboBox jComboBox [];
MigLayout layout;
MigLayout frameLayout;
//column names
ColumnNames cn = new ColumnNames();
List<String> listedColumnNames = cn.getColumnNames();
String columnNames[] = listedColumnNames.toArray(new String[0]);
public OnColumnSelectPanel(int n) {
//jPanel1 = new JPanel();
jFrame = new JFrame("Create Structure of Columns");
// mainJPanel = new JPanel();
layout = new MigLayout("flowy", "[center]rel[grow]", "[]10[]");
frameLayout = new MigLayout("flowx", "[center]rel[grow]", "[]10[]");
//mainJPanel.setLayout(new BoxLayout(mainJPanel, BoxLayout.X_AXIS));
//MigLayout mainJPanelLayout = new MigLayout("flowy", "[]rel[grow]", "[]5[]");
// declare & initialize array
JPanel jPanel[] = new JPanel[n];
JComboBox jComboBox[] = new JComboBox[n];
JButton jButton[] = new JButton[n];
final int num = 0;
for (int i = 0; i < n; i++) {
//assign array
jComboBox[i] = new JComboBox(columnNames);
jButton[i] = new JButton("add Sub Heading");
jPanel[i] = new JPanel();
System.out.println("Loop number: " + i);
jButton[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
for (int j = 0; j < n; j++) {
if (j <= n) {
jComboBox[j] = new JComboBox(columnNames);
jPanel[j].add(jComboBox[j]);
jFrame.revalidate();
} else {
JOptionPane.showMessageDialog(null, "You have exceeded your limit");
}
}
}
});
//set layout
jPanel[i].setLayout(layout);
//jPanel[i].setPreferredSize(new Dimension(50, 400));
//mainJPanel.setLayout(mainJPanelLayout);
jFrame.setLayout(frameLayout);
System.out.println(" height " + jPanel[i].getHeight());
jPanel[i].add(jComboBox[i], new CC().newline());
//jPanel.add(jTextField);
jPanel[i].add(jButton[i]);
//mainJPanel.add(jPanel[i], "spany");
jPanel[i].repaint();
jFrame.add(jPanel[i]);
jFrame.pack();
jPanel[i].revalidate();
jFrame.revalidate();
//mainJPanel.revalidate();
//jFrame.revalidate();
}
//mainJPanel.setSize(600, 400);
//jFrame.add(jPanel1);
jFrame.setVisible(true);
jFrame.setSize(600, 400);
jFrame.setAlwaysOnTop(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output Frame
As you can see the output image. The problem here is that when i click on the add subheading button, combobox is added to every jpanel. this is because i can;t pass the value i to the inner class.
It would be interesting to know possible solutions for this.
FYI I am using Mig Layout.
Thanks in advance.
Only effectively final variables can be accessed from anonymous classes.
i is modified by the loop, so it is not effectively final.
A workaround would look like this:
for (int i = 0; i < n; i++) {
...
final int effectivelyFinal = i;
jButton[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
...
// use effectivelyFinal instead of i
});
}
But as other suggested, it would be way better to extract the anonymous class into a real class and pass all required parameters using the constructor. This can look like this:
class MyListener implements ActionListener {
private final int index;
// add more fields for other required parameters
public MyListener(int index) {
this.index = index;
}
#Override
public void actionPerformed(ActionEvent e) {
// use index
}
}
Usage:
jButton[i].addActionListener(new MyListener(i));

Action Listeners

I have rows and columns of jbuttons, and when each button is clicked they should turn red, and when clicked a second time they should return to their original color. So far, the code I have written is as follows:
public class MainPanel extends JPanel
{
private JButton[][] btn1 = new JButton[3][5];
public MainPanel()
{
JPanel MainPanel= new JPanel();
MainPanel.setPreferredSize(new Dimension(700,700));
JPanel p1 = new JPanel();
{
p1.setLayout(new GridLayout(3,5,10,10));
p1.setBackground(Color.WHITE);
for(int i = 0; i < 3; i++)
for(int j = 0; j < 5; j++)
{
btn1[i][j] = new JButton();
btn1[i][j].setBackground(Color.YELLOW);
p1.add(btn1[i][j]);
btn1[i][j].addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btn1[i][j].setBackground(Color.RED);
}
}
);
}
}
}
}
The line btn1[i][j].setBackground(Color.RED); is stating 'local variables referenced from an inner class must be final or effectively final'. Does anyone know how to fix this?
Add listeners in loop:
for(int i = 0; i < 3; i++)
for(int j = 0; j < 5; j++) {
final JButton btn = btn1[i][j];
btn1[i][j].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn.setBackground(Color.RED);
}
}
);
}
You added listener only to the first button.

ActionListener on array of JButtons

I've created a an array of JButtons that have a random color assigned to them when created as opposed to manually creating each button and assigning random colors to them. I'm now at a point where I want to use change the color, at random, of whichever button is clicked. I'd like to do it the same way as I've created and added the buttons so far(through the use of loops).
Though doing it the way I thought would work has failed. I'm given "local variable is accessed from within inner class; needs to be declared final". I'm that if I use final it can't be changed and now I'm at a loss.
Is there a possible workaround?
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.EventHandler;
import java.lang.String;
import java.util.Random;
public class TEST
{
/**
* #param args the command line arguments
*/
public static Random rand = new Random();
public static int oh;
public void btnPress(ActionEvent e, JButton[] jButts, float r, float g, float b) {
for (int y = 0; y < jButts.length; y++) {
if (e.getSource() == jButts[y]) {
jButts[y].setBackground(Color.getHSBColor(r, g, b));
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Suhp, Brah?");
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setBackground(Color.magenta);
frame.setSize(400, 400);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(4, 4));
String[] numbs = {"0", "1", "2", "3", "4", "5", "6", "7"};
final JButton[] jButts = new JButton[numbs.length];//0-7
for (int i = 0; i < 8; i++) {
jButts[i] = new JButton(numbs[i].toString());
//String leString = rand.nextInt(255).toString;
jButts[i].setBackground(Color.getHSBColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
}
for (int x = 0; x < 8; x++) {
frame.add(jButts[x]);
}
//ActionListener
for (int i =0; i < 8; i++) {
jButts[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
i++;
jButts[i].setBackground(Color.getHSBColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
}
});
}
}
}
There's no need to use i in the ActionListener. You can get the button using ActionEvent#getSource:
jButts[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
button.setBackground(Color.getHSBColor(rand.nextFloat(),
rand.nextFloat(), rand.nextFloat()));
}
});
here's a workaround,
//ActionListener
for (int i =0; i < 8; i++)
{
final int temp = i; // assign to temporary variable
jButts[temp].addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// i++; Not sure what you're trying to do here..
jButts[temp].setBackground(Color.getHSBColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
}
});
}
but I highly recommend rethinking your approach.
// ActionListener
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
button.setBackground(Color.getHSBColor(rand.nextFloat(),
rand.nextFloat(), rand.nextFloat()));
}
};
for (int i = 0; i < 8; i++)
jButts[i].addActionListener(listener);

Categories