Converting JFormattedTextField.getText() return result into a custom type

66 Views Asked by At

So I'm fairly new to Java Swing and graphical interface framework and I'm trying to get user input and use my custom constructor to instantiate new objects referred to here as 'formation' (let's call it a mother class). (C is an instance of a 'Grand mother' Class) and (Matiére Matiére or M is an instance of 'daughter 'class this notation might be confusing but these classes DO NOT extend each other but require an arguement of the "descendant" class to construct a new instance) so (C<-F<-M) and my JFrame takes into it's constructor parameter an instance C of type 'Centre de formation'.

I've Float parsed and cast most of the needed arguments but the final argument is in itself a class 'Matiére" and obviously it's not possible to cast from the JFormattedTextField.getText() return type String directly into Matiére (which in itself has a rather detailed constructor) .

My question is (albeit might be of a beginner) how do convert JFormattedTextField.getText() return result into a custom type of my choice? Since casting is off the table, is there another method to construct this 'formation'?

Is there a way to trace everything back to ObjectType then I can do the down boxing that I desire?

Here's the code:

 bouton_f0.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         if (!bouton_f0.getModel().isPressed()) {
             {  /*ajouter formation*/
                 JFrame f = new JFrame("Ajout d'une formation");
                    f.setSize(500, 200);
                    JPanel pannel = new JPanel();
                    JTextField  testField1 = new JTextField ("nom formation");
                    String nom = testField1.getText();
                    JTextField  testField2 = new JTextField ("duree formation");
                    float duree =Float.parseFloat(testField2.getText()) ;
                    JTextField  testField3 = new JTextField ("prix formation");
                    float prix =Float.parseFloat(testField2.getText()) ;
                    JTextField  testField4 = new JTextField ("Matiére initiale");
                    String Matiére = testField1.getText();
                    /*Cannot cast String  to Matiére to put in the following constructor */ 
                    formation R = new formation(nom,duree,prix,Matiére);
                    
                    C.ajouter_forma(R);
                    pannel.add(testField1);pannel.add(testField2);
                    pannel.add(testField3);pannel.add(testField4);
                    f.getContentPane().add(pannel);
                    f.setVisible(true);
        
                 /*textArea = new JTextArea("nom formation",5, 20);
                 JScrollPane scrollPane = new JScrollPane(textArea); 
                 textArea.setEditable(false); */

                 panneau.removeAll();
                 panneau.revalidate();
                 panneau.repaint();
                 
                 setContentPane(p_forma);
                 setSize(600,150);
                 setVisible(true); 
             }
         }
     }
 });
0

There are 0 best solutions below