Buongiorno a tutti.Nel codice che riporto sotto ho creato un interfaccia grafica con diverse TextField e diversi JRadioButton.
Ora il passo che volevo fare è quello di poter far sì che il programma "clicchi" automaticamente o il JRadioButton A o il JRadioButton B a seconda di un numero randomizzato prodotto dalla funzione Random di java.
Questo però deve avvenire solo se i bottoni e i campi precedenti hanno determinati valori.
Ad esempio vorrei che l'assegnazione venisse fatta se i primi 2 JradioButton sono sì e gli altri 4 no, se l'età è in un certo range di valori...
Quello che vi chiedo è: in questo caso quello che mi serve è implementare un nuovo metodo all'interno della classe con una serie di "if-else" in cui controllo gli stati dei bottoni e dei TextField oppure serve una ActionListener da associare al bottone A e al bottone B?
codice:public class Quest extends JPanel implements ActionListener { private JTextField codicepaziente; private JTextField dataField; private JTextField eta; private JTextField Data_attuale; private JTextField Mobility_scale; private JTextField Tinetti_Scale; public String Datanascita; public Quest (JFrame f) throws ParseException { codicepaziente = new JTextField(20); dataField = new JTextField(10); eta=new JTextField(3); Data_attuale = new JTextField(10); Mobility_scale = new JTextField(10); Tinetti_Scale = new JTextField(10); JLabel inclusion = new JLabel("CRITERI DI INCLUSIONE"); JLabel label1 = new JLabel("Codice paziente"); label1.setLabelFor(codicepaziente); JLabel label3 = new JLabel("Data di nascita"); label3.setLabelFor(dataField); JLabel label4 = new JLabel("Eta"); label4.setLabelFor(eta); JLabel label5 = new JLabel("Data attuale"); label5.setLabelFor(Data_attuale); JLabel label2 = new JLabel("Diagnosi clinica di CMT1A famigliare?"); JRadioButton sì_CMT1A = new JRadioButton("Si"); JRadioButton no_CMT1A = new JRadioButton("No"); ButtonGroup CMT1A = new ButtonGroup(); CMT1A.add(sì_CMT1A); CMT1A.add(no_CMT1A); sì_CMT1A.setAlignmentX(Component.RIGHT_ALIGNMENT); no_CMT1A.setAlignmentX(Component.RIGHT_ALIGNMENT); JLabel label6 = new JLabel("Score Mobility Scale"); label6.setLabelFor(Mobility_scale); JLabel label7 = new JLabel("Tinetti Scale"); label7.setLabelFor(Tinetti_Scale); JLabel label8 = new JLabel("Consenso informato ottenuto:"); JRadioButton sì_Consenso = new JRadioButton("Si"); JRadioButton no_Consenso = new JRadioButton("No"); ButtonGroup Consenso = new ButtonGroup(); Consenso.add(sì_Consenso); Consenso.add(no_Consenso); JLabel exclusion = new JLabel("CRITERI DI ESCLUSIONE"); JLabel cmt = new JLabel("Pt. affetto da HNPP o altro tipo di CMT"); JRadioButton sì_cmt = new JRadioButton("Si"); JRadioButton no_cmt = new JRadioButton("No"); ButtonGroup Cmt = new ButtonGroup(); Cmt.add(sì_cmt); Cmt.add(no_cmt); JLabel affezioni = new JLabel("<html>Affezioni vestibolari,disordini polmonari, " + "cardiovascolari e psichiatrici o severi cambiamenti artropatici " + "negli arti inferiori"); JRadioButton sì_affezioni = new JRadioButton("Si"); JRadioButton no_affezioni = new JRadioButton("No"); ButtonGroup Affezioni = new ButtonGroup(); Affezioni.add(sì_affezioni); Affezioni.add(no_affezioni); JLabel cause_associate = new JLabel("<html>Cause associate di neuropatia " + "(diabete,disordini endocrini,vasculiti, ernia del disco, uso di molecole " + "potenzialmente neurotossiche come farmaci antineoplastici o consumo di alcol"); JRadioButton sì_cause = new JRadioButton("Si"); JRadioButton no_cause = new JRadioButton("No"); ButtonGroup Cause = new ButtonGroup(); Affezioni.add(sì_cause); Affezioni.add(no_cause); JLabel pazienti_nonambulanti = new JLabel("<html>Paziente non ambulante o che " + "richiede supporto monolaterale per camminare"); JRadioButton sì_nonambulanti = new JRadioButton("Si"); JRadioButton no_nonambulanti = new JRadioButton("No"); ButtonGroup Nonambulanti = new ButtonGroup(); Nonambulanti.add(sì_nonambulanti); Nonambulanti.add(no_nonambulanti); JPanel Inclusion = new JPanel(new FlowLayout()); Inclusion.add(inclusion); JPanel nulla = new JPanel(new FlowLayout()); JPanel labels = new JPanel(new GridLayout(1,0)); labels.add(label1); labels.add(codicepaziente); JPanel labels1 = new JPanel(new GridLayout(1,0)); labels1.add(label3); labels1.add(dataField); JPanel labels2 = new JPanel(new GridLayout(1,0)); labels2.add(label4); labels2.add(eta); JPanel labels3 = new JPanel(new GridLayout(1,0)); labels3.add(label2); labels3.add(sì_CMT1A); labels3.add(no_CMT1A); JPanel labels4 = new JPanel(new GridLayout(1,0)); labels4.add(label6); labels4.add(Mobility_scale); JPanel labels5 = new JPanel(new GridLayout(1,0)); labels5.add(label7); labels5.add(Tinetti_Scale); JPanel consenso = new JPanel(new GridLayout(1,0)); consenso.add(label8); consenso.add(sì_Consenso); consenso.add(no_Consenso); JPanel nulla1 = new JPanel(new FlowLayout()); JPanel nulla2 = new JPanel(new FlowLayout()); JPanel nulla3 = new JPanel(new FlowLayout()); JPanel nulla4 = new JPanel(new FlowLayout()); JPanel nulla5 = new JPanel(new FlowLayout()); JPanel nulla6 = new JPanel(new FlowLayout()); JPanel Exclusion = new JPanel(new FlowLayout()); Exclusion.add(exclusion); JPanel CMT = new JPanel(new GridLayout(1,0)); CMT.add(cmt); CMT.add(sì_cmt); CMT.add(no_cmt); JPanel AFFEZIONI = new JPanel(new GridLayout(1,0)); AFFEZIONI.add(affezioni); AFFEZIONI.add(sì_affezioni); AFFEZIONI.add(no_affezioni); JPanel CAUSE = new JPanel(new GridLayout(1,0)); CAUSE.add(cause_associate); CAUSE.add(sì_cause); CAUSE.add(no_cause); JPanel NONAMBULANTI = new JPanel(new GridLayout(1,0)); NONAMBULANTI.add(pazienti_nonambulanti); NONAMBULANTI.add(sì_nonambulanti); NONAMBULANTI.add(no_nonambulanti); Datanascita= dataField.getText(); SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Data_attuale.setText(formatter.format(new Date())); Data_attuale.setEditable(false); eta.setEditable(false); dataField.addFocusListener(new MyFocusListener(Data_attuale.getText(), formatter)); Box group = Box.createVerticalBox(); group.add(Inclusion); group.add(nulla); group.add(labels); group.add(labels1); group.add(labels2); group.add(labels3); group.add(labels4); group.add(labels5); group.add(consenso); group.add(nulla1); group.add(nulla2); group.add(Exclusion); group.add(nulla3); group.add(CMT); group.add(nulla4); group.add(AFFEZIONI); group.add(nulla5); group.add(CAUSE); group.add(nulla6); group.add(NONAMBULANTI); JPanel container = new JPanel(new FlowLayout(FlowLayout.RIGHT)); container.add(group); JLabel Randomization = new JLabel("Treatment arm"); JPanel Fondo = new JPanel(new GridLayout(1,0)); Fondo.add(Randomization); JRadioButton A = new JRadioButton("A"); JRadioButton B = new JRadioButton("B"); ButtonGroup Trattamento = new ButtonGroup(); Trattamento.add(A); Trattamento.add(B); Fondo.add(A); Fondo.add(B); JLabel Randomization_date = new JLabel("Data randomizzazione"); Randomization_date.setLabelFor(Data_attuale); JPanel Randomization1 = new JPanel(new GridLayout(0,1)); Randomization1.add(Randomization_date); Randomization1.add(Data_attuale); JPanel Fondo1 = new JPanel(new FlowLayout(FlowLayout.CENTER,30,10)); Fondo1.add(Fondo); Fondo1.add(Randomization1); JPanel layout = new JPanel(new BorderLayout()); layout.add(container,BorderLayout.CENTER); layout.add(Fondo1,BorderLayout.SOUTH); add(layout); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); } protected void resetFocus() { } private static void createAndShowGUI() throws ParseException { JFrame frame = new JFrame("Questionario"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Quest newContentPane = new Quest(frame); newContentPane.setOpaque(false); frame.setContentPane(newContentPane); frame.addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent e) { newContentPane.resetFocus(); } }); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { UIManager.put("swing.boldMetal", Boolean.TRUE); createAndShowGUI(); } catch (ParseException ex) { Logger.getLogger(Quest.class.getName()).log(Level.SEVERE, null, ex); } } }); }; private class MyFocusListener extends FocusAdapter { String datanascita, dataattuale; SimpleDateFormat formatter; public MyFocusListener(String dataattuale, SimpleDateFormat formatter) { this.formatter = formatter; this.dataattuale = dataattuale; } public void focusLost(FocusEvent fe) { try { datanascita = dataField.getText(); Date dn = formatter.parse(datanascita); Date da = formatter.parse(dataattuale); if (da.after(dn)) { //valida int e = (int)((da.getTime()-dn.getTime())/(365.25d*3600*24*1000)); eta.setText(Integer.toString(e)); } else { dataField.setText(""); dataField.transferFocus(); } } catch (Exception e) { dataField.setText("Wrong date format"); dataField.transferFocus(); e.printStackTrace(); } } } }

Rispondi quotando