Forum - Java
- Creazione dinamica componenti java
rosario.damore (Normal User)
Newbie
Messaggi: 6
Iscritto: 03/10/2010
Ciao a tutti,
io ho un problema simile. Ho Bisogno di creare dei combo box dinamicamente in base ad un numero di attrubuti che gli passo dalla classe main. Vorrei una cosa così
http://oi55.tinypic.com/kaqngn.jpg
[img]http://oi55.tinypic.com/kaqngn.jpg [/img]
Oppure una cosa con i radio button così
http://i52.tinypic.com/33p5wck.jpg
[img]http://i52.tinypic.com/33p5wck.jpg [/img]
I valori da selezionare sono numeri interi da 1 a 5. Quindi l'utente dovrà valutare gli attribut1 x,y,z ecc,.. con un valore da 1 a 5. Quando preme OK mi deve restituire un arrey di numeri interi con le sue valutazioni.
Possiamo pensare in alternativa anche un componente jslider da 1 a 5. non è un problema. Mi puoi aiutare. Le variabili in entrata sarebbero i numero degli attributi da valutare ( che coinciderà con numero di label e combox) e il numero nome degli stessi che andrà inserito nelle label.
Mi date una mano? Grazie
Bonny (Member )
Expert
Messaggi: 437
Iscritto: 24/04/2009
Codice sorgente - presumibilmente Java
import java.awt.GridLayout ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.* ;
public class Esempio
extends JFrame {
public Esempio( ) {
super ( "Esempio" ) ;
p.
setLayout ( new GridLayout ( 7, 1, 10, 10
) ) ; //Layout Manager griglia p.
add ( new JLabel ( "Confronto 1" ) ) ; //7 righe 1 colonna p.add ( combo1) ;
p.
add ( new JLabel ( "Confronto 2" ) ) ; p.add ( combo2) ;
p.
add ( new JLabel ( "Confronto 3" ) ) ; p.add ( combo3) ;
p.add ( conferma) ;
initCombo( ) ; //chimata inizializza Combobox
this .getContentPane ( ) .add ( p, "Center" ) ;
this .setVisible ( true ) ;
this .setBounds ( 200, 200, 500, 300) ;
this .pack ( ) ;
//ascoltatore del JButton conferma
conferma.addActionListener (
/*
qui dovrai implementare il codice per creare l'array di risposte per poi fare ulteriori istruzioni..
Mi sono limitato a prepararti la GUI di esempio adesso tocca a te.
* Con il metodo "getSelectedItem()" la combobox ti restituisce l'elemento selezionato (tipo Object)
* a questo punto.................
*/
}
} ) ;
/////////////////////////////////////
}
public void initCombo( ) {
//inizializzo le comboBox
for ( int i = 1 ; i < 6 ; i++ ) {
combo1.
addItem ( String .
valueOf ( i
) ) ; combo2.
addItem ( String .
valueOf ( i
) ) ; combo3.
addItem ( String .
valueOf ( i
) ) ; }
}
}
spero di esserti stati d'aiuto
ps. da non confondere "
"getSelectedItem()" " con
"getSelectionIndex" qst'ultimo restituisce un intero cioè l'indice dell'elemento selezionato nella combobox.
Ultima modifica effettuata da Bonny il 03/10/2010 alle 11:44
rosario.damore (Normal User)
Newbie
Messaggi: 6
Iscritto: 03/10/2010
Ciao Bonny,
grazie per il tuo aiuto. Vedo nel tuo codice che hai dichiarato 3 ComboBox
Codice sorgente - presumibilmente Java
Io invece volevo che il programma creasse tante combobox quanti sono gli attributi. Magari gli passo una varibile nel costruttore.
Poi volevo che nelle combobox si potessero selezionare uno dei valori {1,2,3,4,5}
Mi dai una mano? Grazie
Bonny (Member )
Expert
Messaggi: 437
Iscritto: 24/04/2009
Codice sorgente - presumibilmente Java
package prova ;
import java.awt.GridLayout ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.* ;
public class Esempio
extends JFrame {
private Extendscombo vetCombo[ ] ;
public Esempio( int numeroCombo) {
super ( "Esempio" ) ;
vetCombo = new Extendscombo[ numeroCombo] ;
//ad ogni combo va allegata una JLabel il +1 è per il JButton
int n = ( numeroCombo * 2) + 1 ;
for ( int i = 0 ; i < numeroCombo; i++ ) {
p.
add ( new JLabel ( "Confronto " + ( i
+ 1
) ) ) ; vetCombo[ i] = new Extendscombo( ) ;
p.add ( vetCombo[ i] ) ;
}
p.add ( conferma) ;
this .getContentPane ( ) .add ( p, "Center" ) ;
this .setVisible ( true ) ;
this .setBounds ( 200, 200, 500, 300) ;
this .pack ( ) ;
conferma.addActionListener (
}
} ) ;
}
} //fine class esempio
public Extendscombo( ) {
super ( ) ;
for ( int i = 1 ; i < 6 ; i++ ) {
this .
addItem ( String .
valueOf ( i
) ) ; }
}
}
Ho esteso la classe JComboBox cosi nel costruttore le inizializzo, da quello che ho capito sono tutte uguali.
poi ho creato un vettore di qst e con un ciclo le ho inserite nel pannello.
Bonny (Member )
Expert
Messaggi: 437
Iscritto: 24/04/2009
mmmm:/ nella classe Esempio ti conviene dichiarare una var intera che conterrà il valore di numeroCombo e nel costruttore fai this.nuova_var = numeroCombo;
altrimenti il Listener del bottone conferma non la riconosce!
Codice sorgente - presumibilmente Java
package prova ;
import java.awt.GridLayout ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.* ;
public class Esempio
extends JFrame {
private Extendscombo vetCombo[ ] ;
private int numeroC = 0 ;
public Esempio( int numeroCombo) {
super ( "Esempio" ) ;
this .numeroC = numeroCombo;
vetCombo = new Extendscombo[ this .numeroC ] ;
//ad ogni combo va allegata una JLabel il +1 è per il JButton
int n = ( this .numeroC * 2) + 1 ;
for ( int i = 0 ; i < this .numeroC ; i++ ) {
p.
add ( new JLabel ( "Confronto " + ( i
+ 1
) ) ) ; vetCombo[ i] = new Extendscombo( ) ;
p.add ( vetCombo[ i] ) ;
}
p.add ( conferma) ;
this .getContentPane ( ) .add ( p, "Center" ) ;
this .setVisible ( true ) ;
this .setBounds ( 200, 200, 500, 300) ;
this .pack ( ) ;
conferma.addActionListener (
//qui crei un array di interi , per esempio, di dimensione numeroC
// e con un ciclo che va da 0 a numeroC recupero tutte le risposte
//selezionate sulle combobox,ovviamente il metodo getSelectionItem
// restituisce un oggetto di classo Object quindi devi fare un casting
//o usi il metodo getSelectionIndex xò stai attento l'indice parte da 0
//quindi basta fare 'combo.getSelectionIndex() + 1' (risparmi casting)
}
} ) ;
}
} //fine class esempio
public Extendscombo( ) {
super ( ) ;
for ( int i = 1 ; i < 6 ; i++ ) {
this .
addItem ( String .
valueOf ( i
) ) ; }
}
}
rosario.damore (Normal User)
Newbie
Messaggi: 6
Iscritto: 03/10/2010
Ciao Bonny,
ho implementato il codice con lo JSlider:
Codice sorgente - presumibilmente Java
public class Matrice
extends javax.
swing .
JFrame {
private boolean done = false ;
public Matrice( ) {
initComponents( ) ;
}
public Matrice
( String name,
int max,
int attributi
) { initComponents( name, max, attributi) ;
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents
( String name,
int max,
int attributi
) { jLabel1
[ attributi
] = new javax.
swing .
JLabel ( ) ; jSlider1
= new javax.
swing .
JSlider ( ) ; jButton1
= new javax.
swing .
JButton ( ) ;
setTitle( "Valutazione Attributi" ) ;
jLabel1
[ attributi
] .
setFont ( new java.
awt .
Font ( "Arial" , 0, 14
) ) ; // NOI18N jLabel1[ attributi] .setText ( "Valuta l'attributo " + name+ " in confronto all'attributo Y" ) ;
jSlider1.
setFont ( new java.
awt .
Font ( "Arial" , 0, 18
) ) ; // NOI18N jSlider1.setMajorTickSpacing ( 1) ;
jSlider1.setMaximum ( 5) ;
jSlider1.setMinimum ( 1) ;
jSlider1.setMinorTickSpacing ( 1) ;
jSlider1.setPaintLabels ( true ) ;
jSlider1.setPaintTicks ( true ) ;
jSlider1.setSnapToTicks ( true ) ;
jSlider1.setValue ( 1) ;
jButton1.
setFont ( new java.
awt .
Font ( "Arial" , 0, 18
) ) ; // NOI18N jButton1.setText ( "OK" ) ;
public void actionPerformed
( java.
awt .
event .
ActionEvent evt
) { jButton1ActionPerformed( evt) ;
}
} ) ;
javax.swing .GroupLayout layout = new javax.swing .GroupLayout ( getContentPane( ) ) ;
getContentPane( ) .setLayout ( layout) ;
layout.setHorizontalGroup (
layout.createParallelGroup ( javax.swing .GroupLayout .Alignment .LEADING )
.addGroup ( layout.createSequentialGroup ( )
.addGroup ( layout.createParallelGroup ( javax.swing .GroupLayout .Alignment .LEADING )
.addGroup ( layout.createSequentialGroup ( )
.addGap ( 65, 65, 65)
.addComponent ( jLabel1[ attributi] ) )
.addGroup ( layout.createSequentialGroup ( )
.addGap ( 166, 166, 166)
.addComponent ( jButton1) )
.addGroup ( layout.createSequentialGroup ( )
.addContainerGap ( )
.
addComponent ( jSlider1, javax.
swing .
GroupLayout .
DEFAULT_SIZE , 391,
Short .
MAX_VALUE ) ) ) .addContainerGap ( ) )
) ;
layout.setVerticalGroup (
layout.createParallelGroup ( javax.swing .GroupLayout .Alignment .LEADING )
.addGroup ( layout.createSequentialGroup ( )
.addContainerGap ( )
.addComponent ( jLabel1[ attributi] )
.addPreferredGap ( javax.swing .LayoutStyle .ComponentPlacement .RELATED )
.addComponent ( jSlider1, javax.swing .GroupLayout .PREFERRED_SIZE , 55, javax.swing .GroupLayout .PREFERRED_SIZE )
.
addPreferredGap ( javax.
swing .
LayoutStyle .
ComponentPlacement .
RELATED , 169,
Short .
MAX_VALUE ) .addComponent ( jButton1)
.addContainerGap ( ) )
) ;
java.
awt .
Dimension screenSize
= java.
awt .
Toolkit .
getDefaultToolkit ( ) .
getScreenSize ( ) ; setBounds( ( screenSize.width - 300) / 2, ( screenSize.height - 150) / 2, 300, 150) ;
} // </editor-fold>//GEN-END:initComponents
private void initComponents( ) {
for ( int i= 0 ; i< 8 ; i++ ) {
jLabel1
[ i
] = new javax.
swing .
JLabel ( ) ; jSlider1
= new javax.
swing .
JSlider ( ) ; jButton1
= new javax.
swing .
JButton ( ) ;
setTitle( "Valutazione Attributi" ) ;
jLabel1
[ i
] .
setFont ( new java.
awt .
Font ( "Arial" , 0, 14
) ) ; // NOI18N jLabel1[ i] .setText ( "Valuta l'attributo X in confronto all'attributo Y" ) ;
jSlider1.
setFont ( new java.
awt .
Font ( "Arial" , 0, 18
) ) ; // NOI18N jSlider1.setMajorTickSpacing ( 1) ;
jSlider1.setMaximum ( 5) ;
jSlider1.setMinimum ( 1) ;
jSlider1.setMinorTickSpacing ( 1) ;
jSlider1.setPaintLabels ( true ) ;
jSlider1.setPaintTicks ( true ) ;
jSlider1.setSnapToTicks ( true ) ;
jSlider1.setValue ( 1) ;
jButton1.
setFont ( new java.
awt .
Font ( "Arial" , 0, 18
) ) ; // NOI18N jButton1.setText ( "OK" ) ;
public void actionPerformed
( java.
awt .
event .
ActionEvent evt
) { jButton1ActionPerformed( evt) ;
}
} ) ;
javax.swing .GroupLayout layout = new javax.swing .GroupLayout ( getContentPane( ) ) ;
getContentPane( ) .setLayout ( layout) ;
layout.setHorizontalGroup (
layout.createParallelGroup ( javax.swing .GroupLayout .Alignment .LEADING )
.addGroup ( layout.createSequentialGroup ( )
.addGroup ( layout.createParallelGroup ( javax.swing .GroupLayout .Alignment .LEADING )
.addGroup ( layout.createSequentialGroup ( )
.addGap ( 65, 65, 65)
.addComponent ( jLabel1[ i] ) )
.addGroup ( layout.createSequentialGroup ( )
.addGap ( 166, 166, 166)
.addComponent ( jButton1) )
.addGroup ( layout.createSequentialGroup ( )
.addContainerGap ( )
.
addComponent ( jSlider1, javax.
swing .
GroupLayout .
DEFAULT_SIZE , 391,
Short .
MAX_VALUE ) ) ) .addContainerGap ( ) )
) ;
layout.setVerticalGroup (
layout.createParallelGroup ( javax.swing .GroupLayout .Alignment .LEADING )
.addGroup ( layout.createSequentialGroup ( )
.addContainerGap ( )
.addComponent ( jLabel1[ i] )
.addPreferredGap ( javax.swing .LayoutStyle .ComponentPlacement .RELATED )
.addComponent ( jSlider1, javax.swing .GroupLayout .PREFERRED_SIZE , 55, javax.swing .GroupLayout .PREFERRED_SIZE )
.
addPreferredGap ( javax.
swing .
LayoutStyle .
ComponentPlacement .
RELATED , 169,
Short .
MAX_VALUE ) .addComponent ( jButton1)
.addContainerGap ( ) )
) ;
}
java.
awt .
Dimension screenSize
= java.
awt .
Toolkit .
getDefaultToolkit ( ) .
getScreenSize ( ) ; setBounds( ( screenSize.width - 300) / 2, ( screenSize.height - 150) / 2, 400, 250) ;
} // </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed
( java.
awt .
event .
ActionEvent evt
) { //GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here:
setDone( true ) ;
} //GEN-LAST:event_jButton1ActionPerformed
public static void main
( String args
[ ] ) { public void run( ) {
new Matrice( ) .setVisible ( true ) ;
}
} ) ;
}
public double visualizza( ) {
int rating;
this .setVisible ( true ) ;
while ( ! isDone( ) ) { }
rating = jSlider1.getValue ( ) ;
this .dispose ( ) ;
return rating;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.
swing .
JButton jButton1
; private javax.
swing .
JSlider jSlider1
; private javax.
swing .
JLabel jLabel1
[ ] ; // End of variables declaration//GEN-END:variables
public boolean isDone( ) {
return done;
}
public void setDone( boolean done) {
this .done = done;
}
}
Adesso il problema è che quando lo compilo mi dice
Codice sorgente - presumibilmente Delphi
Exception in thread "AWT- EventQueue- 0" java. lang . NullPointerException
at Matrice. initComponents ( Matrice. java : 80)
at Matrice. <init>( Matrice. java : 6)
at Matrice$3 . run ( Matrice. java : 151 )
perchè non mi crea le 8 JLabels in automatico?