Search code examples
javaswingnetbeansfontsjoptionpane

How to Change the font of Textfield using a variable


Can any one tell me how to change the font of a1,a2,a3,a4,a5 after getting the value of the jTextFields so that if i show it on my JOptionPane the assign value of each variable has colors

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    String a1 = jTextField1.getText();
    String a2 = jTextField2.getText();
    String a3 = jTextField3.getText();
    String a4 = jTextField4.getText();
    String a5 = jTextField5.getText();

    String m1 = "You will never forget " + a1 + "\n\n";
    String m2 = "You can consider " + a2 + " as your true friend \n\n";
    String m3 = "You really Love " + a3 + "\n\n";
    String m4 = a4 + " is your twin soul \n\n" ; 
    String m5 = "you will remember " + a5 + " for the rest of your life (due to past –good or bad- experiences, lessons etc) \n\n";
    String m6 = m1 + m2 + m3 + m4 + m5;
    JOptionPane.showMessageDialog(null,m6);
} 

Solution

  • You should create Label, set Font, and then use it to create messageDialog. Try something like this:

    String a1 = jTextField1.getText();
    String a2 = jTextField2.getText();
    String a3 = jTextField3.getText();
    String a4 = jTextField4.getText();
    String a5 = jTextField5.getText();
    
    String m1 = "You will never forget " + a1 + "\n\n";
    String m2 = "You can consider " + a2 + " as your true friend \n\n";
    String m3 = "You really Love " + a3 + "\n\n";
    String m4 = a4 + " is your twin soul \n\n" ; 
    String m5 = "you will remember " + a5 + " for the rest of your life (due to past –good or bad- experiences, lessons etc) \n\n";
    String m6 = m1 + m2 + m3 + m4 + m5;
    
    JLabel label = new JLabel(m6);
    label.setFont(new Font("serif", Font.BOLD, 14));
    JOptionPane.showMessageDialog(null,label);