JOptionPane.showInputDialog on JButton[][] array  
Author Message
Alessandro Mizzoni





PostPosted: 2004-8-25 5:04:00 Top

java-programmer, JOptionPane.showInputDialog on JButton[][] array Sorry i have a problem.
I don't know the java language but i must create a grid Of button witch
random Values, and when i clicked on one button i must change the internal
values.
I try ti make a code but i don't know how to pass the value of botton to
mouse event, and change it with
String input = JOptionPane.showInputDialog("Insert value:");

for a single button there aren't any problems, but how can I change the
value of an array of button witch MouseEvent?

Thx!!!! please help me!!!!


QUADRATOFRAME.CLASS

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class QuadratoFrame extends JFrame {

JButton pulsante = new JButton();
int N=6;
int MAXELEM = 50;
int[][] mat=new int[N][N];
JButton[][] jb = new JButton[N][N];
private JPanel pannello2= new JPanel();
LeftButtonListener leftListener = new LeftButtonListener();

public QuadratoFrame() {

Random generator=new Random();
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
mat[i][j]=generator.nextInt(MAXELEM);

for(int i = 0; i < N; i++)
for (int j=0; j<N; j++)
{
jb[i][j] = new JButton(Integer.toString(mat[i][j]));
pulsante=jb[i][j];
pulsante.addMouseListener(leftListener);
}


pannello2.setLayout(new GridLayout(N,N));
getContentPane().add(pannello2);
pack();

for(int i = 0; i < jb.length; i++)
for (int j=0; j<N; j++)
pannello2.add(jb[i][j]);

}

private class LeftButtonListener implements MouseListener {
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mousePressed(MouseEvent event) {
String input = JOptionPane.showInputDialog("Insert new value:");
}

public void mouseReleased(MouseEvent event) { }

}


}



QUADRATOTEST.CLASS

import javax.swing.*;

/**Una classe che rende attivo il programma Frattali
*tramite la costruzione e la visualizzazione della finestra Frattali nel
main.
*/

public class QuadratoTest {

public static void main(String[] args) {
JFrame myFrame=new QuadratoFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(800,600);
myFrame.show();

}

}


 
zoopy





PostPosted: 2004-8-25 19:08:00 Top

java-programmer >> JOptionPane.showInputDialog on JButton[][] array On 24-8-2004 23:04, Alessandro Mizzoni wrote:

> Sorry i have a problem.
> I don't know the java language but i must create a grid Of button witch
> random Values, and when i clicked on one button i must change the internal
> values.
> I try ti make a code but i don't know how to pass the value of botton to
> mouse event, and change it with
> String input = JOptionPane.showInputDialog("Insert value:");
>
> for a single button there aren't any problems, but how can I change the
> value of an array of button witch MouseEvent?
>
> Thx!!!! please help me!!!!

Use an ActionListener instead of MouseListener to react to a button press.

I've added the modified class below. Basically there's one ActionListener ('buttonListener') for all
the buttons. Every button "knows" which element it represents in the matrix: this is done by storing
the row and column index as a clientProperty of the button. When the buttonListener is activated, it
retrieves these indexes and reads and changes the right element of the matrix.

--------
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class QuadratoFrame extends JFrame
{
// keys for putClientProperty
private final static String PROP_MAT_ROW = "MAT_ROW";
private final static String PROP_MAT_COL = "MAT_COL";

private final static int N = 6;
private final static int MAXELEM = 50;

private int[][] mat = new int[N][N];
private JButton[][] jb = new JButton[N][N];
private JPanel pannello2 = new JPanel();

// XXX LeftButtonListener leftListener = new LeftButtonListener();

/*
* buttonListener reacts to a button press.
* buttonListener is created as an instance of an anonymous
* subclass of ActionListener:
* ActionListener buttonListener = new ActionListener()
* {
* ...
* }
* buttonListener doesn't necessarily have to be a member
* variable: it would be sufficient if it had been declared as
* a local variable in the QuadratoFrame constructor.
*/
private ActionListener buttonListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton pressedButton = (JButton) e.getSource();

// Get row and column index from the button
int row =
((Integer) pressedButton.getClientProperty(PROP_MAT_ROW)).intValue();
int col =
((Integer) pressedButton.getClientProperty(PROP_MAT_COL)).intValue();

// matrix value
int value = mat[row][col];

String input =
JOptionPane.showInputDialog(
QuadratoFrame.this,
"Insert new value:",
Integer.toString(value));
if (input != null)
{
// input != null: user did not cancel the dialog
try
{
// Parse input to see if it contains a valid integer;
// if it doesn't, parseInt throws a NumberFormatException.
// Trim input to remove leading and trailing spaces
value = Integer.parseInt(input.trim());

// Test if value is in the appropriate range
if (value >= 0 && value < MAXELEM)
{
// Yep: change matrix...
mat[row][col] = value;
// ...and the text of the button
pressedButton.setText(Integer.toString(value));
}
else
{
// Value was not in the range
throw new NumberFormatException(
"value must be >= 0 and < " + MAXELEM);
}
}
catch (NumberFormatException ex)
{
// Input was invalid: show error dialog
JOptionPane.showMessageDialog(
QuadratoFrame.this,
"Invalid number: " + input + "\n" + ex.getMessage(),
"Invalid input",
JOptionPane.ERROR_MESSAGE);
}
}
}
};

public QuadratoFrame()
{
Random generator = new Random();
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
mat[i][j] = generator.nextInt(MAXELEM);

for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
JButton pulsante = new JButton(Integer.toString(mat[i][j]));
jb[i][j] = pulsante;
// XXX pulsante.addMouseListener(leftListener);

// Set row and col index of the button; used by buttonListener
pulsante.putClientProperty(PROP_MAT_ROW, new Integer(i));
pulsante.putClientProperty(PROP_MAT_COL, new Integer(j));

// Add buttonListener as ActionListener
pulsante.addActionListener(buttonListener);
}

pannello2.setLayout(new GridLayout(N, N));
getContentPane().add(pannello2);

for (int i = 0; i < jb.length; i++)
for (int j = 0; j < N; j++)
pannello2.add(jb[i][j]);

pack();
}

/* XXX
private class LeftButtonListener implements MouseListener
{
public void mouseClicked(MouseEvent event)
{
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
public void mousePressed(MouseEvent event)
{
String input = JOptionPane.showInputDialog("Insert new value:");
}

public void mouseReleased(MouseEvent event)
{
}
}
*/
}

--
Regards,
Z.
 
Alessandro Mizzoni





PostPosted: 2004-8-26 5:20:00 Top

java-programmer >> JOptionPane.showInputDialog on JButton[][] array Sorry i have a problem.
I don't know the java language but i must create a grid Of button witch
random Values, and when i clicked on one button i must change the internal
values.
I try ti make a code but i don't know how to pass the value of botton to
mouse event, and change it with
String input = JOptionPane.showInputDialog("Insert value:");

for a single button there aren't any problems, but how can I change the
value of an array of button witch MouseEvent?

Thx!!!! please help me!!!!


QUADRATOFRAME.CLASS

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class QuadratoFrame extends JFrame {

JButton pulsante = new JButton();
int N=6;
int MAXELEM = 50;
int[][] mat=new int[N][N];
JButton[][] jb = new JButton[N][N];
private JPanel pannello2= new JPanel();
LeftButtonListener leftListener = new LeftButtonListener();

public QuadratoFrame() {

Random generator=new Random();
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
mat[i][j]=generator.nextInt(MAXELEM);

for(int i = 0; i < N; i++)
for (int j=0; j<N; j++)
{
jb[i][j] = new JButton(Integer.toString(mat[i][j]));
pulsante=jb[i][j];
pulsante.addMouseListener(leftListener);
}


pannello2.setLayout(new GridLayout(N,N));
getContentPane().add(pannello2);
pack();

for(int i = 0; i < jb.length; i++)
for (int j=0; j<N; j++)
pannello2.add(jb[i][j]);

}

private class LeftButtonListener implements MouseListener {
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mousePressed(MouseEvent event) {
String input = JOptionPane.showInputDialog("Insert new value:");
}

public void mouseReleased(MouseEvent event) { }

}


}



QUADRATOTEST.CLASS

import javax.swing.*;

/**Una classe che rende attivo il programma Frattali
*tramite la costruzione e la visualizzazione della finestra Frattali nel
main.
*/

public class QuadratoTest {

public static void main(String[] args) {
JFrame myFrame=new QuadratoFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(800,600);
myFrame.show();

}

}



 
 
Alessandro Mizzoni





PostPosted: 2004-8-26 5:21:00 Top

java-programmer >> JOptionPane.showInputDialog on JButton[][] array Sorry i have a problem.
I don't know the java language but i must create a grid Of button witch
random Values, and when i clicked on one button i must change the internal
values.
I try ti make a code but i don't know how to pass the value of botton to
mouse event, and change it with
String input = JOptionPane.showInputDialog("Insert value:");

for a single button there aren't any problems, but how can I change the
value of an array of button witch MouseEvent?

Thx!!!! please help me!!!!


QUADRATOFRAME.CLASS

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class QuadratoFrame extends JFrame {

JButton pulsante = new JButton();
int N=6;
int MAXELEM = 50;
int[][] mat=new int[N][N];
JButton[][] jb = new JButton[N][N];
private JPanel pannello2= new JPanel();
LeftButtonListener leftListener = new LeftButtonListener();

public QuadratoFrame() {

Random generator=new Random();
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
mat[i][j]=generator.nextInt(MAXELEM);

for(int i = 0; i < N; i++)
for (int j=0; j<N; j++)
{
jb[i][j] = new JButton(Integer.toString(mat[i][j]));
pulsante=jb[i][j];
pulsante.addMouseListener(leftListener);
}


pannello2.setLayout(new GridLayout(N,N));
getContentPane().add(pannello2);
pack();

for(int i = 0; i < jb.length; i++)
for (int j=0; j<N; j++)
pannello2.add(jb[i][j]);

}

private class LeftButtonListener implements MouseListener {
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mousePressed(MouseEvent event) {
String input = JOptionPane.showInputDialog("Insert new value:");
}

public void mouseReleased(MouseEvent event) { }

}


}



QUADRATOTEST.CLASS

import javax.swing.*;

/**Una classe che rende attivo il programma Frattali
*tramite la costruzione e la visualizzazione della finestra Frattali nel
main.
*/

public class QuadratoTest {

public static void main(String[] args) {
JFrame myFrame=new QuadratoFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(800,600);
myFrame.show();

}

}



 
 
Alessandro Mizzoni





PostPosted: 2004-8-26 5:23:00 Top

java-programmer >> JOptionPane.showInputDialog on JButton[][] array Sorry i have a problem.
I don't know the java language but i must create a grid Of button witch
random Values, and when i clicked on one button i must change the internal
values.
I try ti make a code but i don't know how to pass the value of botton to
mouse event, and change it with
String input = JOptionPane.showInputDialog("Insert value:");

for a single button there aren't any problems, but how can I change the
value of an array of button witch MouseEvent?

Thx!!!! please help me!!!!


QUADRATOFRAME.CLASS

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class QuadratoFrame extends JFrame {

JButton pulsante = new JButton();
int N=6;
int MAXELEM = 50;
int[][] mat=new int[N][N];
JButton[][] jb = new JButton[N][N];
private JPanel pannello2= new JPanel();
LeftButtonListener leftListener = new LeftButtonListener();

public QuadratoFrame() {

Random generator=new Random();
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
mat[i][j]=generator.nextInt(MAXELEM);

for(int i = 0; i < N; i++)
for (int j=0; j<N; j++)
{
jb[i][j] = new JButton(Integer.toString(mat[i][j]));
pulsante=jb[i][j];
pulsante.addMouseListener(leftListener);
}


pannello2.setLayout(new GridLayout(N,N));
getContentPane().add(pannello2);
pack();

for(int i = 0; i < jb.length; i++)
for (int j=0; j<N; j++)
pannello2.add(jb[i][j]);

}

private class LeftButtonListener implements MouseListener {
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mousePressed(MouseEvent event) {
String input = JOptionPane.showInputDialog("Insert new value:");
}

public void mouseReleased(MouseEvent event) { }

}


}



QUADRATOTEST.CLASS

import javax.swing.*;

/**Una classe che rende attivo il programma Frattali
*tramite la costruzione e la visualizzazione della finestra Frattali nel
main.
*/

public class QuadratoTest {

public static void main(String[] args) {
JFrame myFrame=new QuadratoFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(800,600);
myFrame.show();

}

}