MagicSquare
From: Alessandro Mizzoni (alessandro.mizzoni_at_fastwebnet.it)
Date: 08/27/04
- Next message: ak: "Re: Rendering performance while dragging"
- Previous message: Larry Barowski: "Re: Rendering performance while dragging"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 27 Aug 2004 01:34:04 +0200
last question!!!
this program create a jpannel "pannello" with a gridlayout(N*N) and a
jpannel"pannello3" with JComboBox ArrayChoice.
how can i change the content of pannello with a new random gridlayout(N*N)
when i clicked on a item of the JComboBox ArrayChoice?
thx!!!!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
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();
private JPanel pannello3= new JPanel();
//private JButton Verifica= new JButton("verifica");
private JComboBox ArrayChoice;
public QuadratoFrame()
{
CreateRamdom ();
ArrayChoice=new JComboBox();
ArrayChoice.addItem("< selezionare matrice>");
ArrayChoice.addItem("Array Random");
ArrayChoice.addItem("Array Random2");
//ArrayChoice.setEditable(true);
ArrayChoice.addActionListener(new ChoiceListener());
//pannello3.add(Verifica);
pannello2.add(ArrayChoice);
//pannello3.setPreferredSize(new Dimension(120, 100));
pannello2.setPreferredSize(new Dimension(120, 100));
getContentPane().add(pannello2, BorderLayout.NORTH);
//getContentPane().add(pannello, BorderLayout.CENTER);
//getContentPane().add(pannello3, BorderLayout.SOUTH);
//pack();
}
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,
"Inserisci un nuovo valore:",
Integer.toString(value));
if (input != null)
{
// input != null: user did not cancel the dialog
try
{
value = Integer.parseInt(input.trim());
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,
"Valore inserito: " + input + "\n" + ex.getMessage(),
"hai inserito un valore non valido, inserire un intero",
JOptionPane.ERROR_MESSAGE);
}
}
}
};
private class ChoiceListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
//String currentChoiceName=(String)ArrayChoice.getSelectedItem();
CreateRamdom ();
}
private void CreateRamdom (){
JPanel pannello = new JPanel();
pannello.setLayout(new GridLayout(N, N));
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);
}
for (int i = 0; i < jb.length; i++)
for (int j = 0; j < N; j++)
pannello.add(jb[i][j]);
getContentPane().add(pannello, BorderLayout.CENTER);
}
}
- Next message: ak: "Re: Rendering performance while dragging"
- Previous message: Larry Barowski: "Re: Rendering performance while dragging"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]