pulsante.putClientProperty
From: Alessandro Mizzoni (alessandro.mizzoni_at_fastwebnet.it)
Date: 08/29/04
- Next message: Alessandro Mizzoni: "putClientProperty"
- Previous message: ZOCOR: "GUI: Handcode or Automate?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 29 Aug 2004 03:49:40 +0200
HI, i have this two funcion: i want put on the client property of "pulsante"
the array "m". How can I put the array?
pulsante.putClientProperty(PROP_ARRAY, ????????????);<=============
and how can i get in a int[][] the array "m" in the ActionListener?
int[][] array= ((int[][])
pressedButton.getClientProperty(PROP_ARRAY)).intValue(); <=====?????
thx!!!!
--------------------------------------------------------------------------------- private void CreateRamdom (int[][] m){ 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)); pulsante.putClientProperty(PROP_ARRAY, ????????????);<=== // 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); } ---------------------------------------------------------------------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(); int[][] array= ((int[][])pressedButton.getClientProperty(PROP_ARRAY)).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 { // 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, "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(); CreateRamdom2 (); } }
- Next message: Alessandro Mizzoni: "putClientProperty"
- Previous message: ZOCOR: "GUI: Handcode or Automate?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]