java - text in textfield is displayed but wont change font when choosing in combobox

This code is about user inserting text in text field and transfer the text to label then user can choose font style in JComboBox where the text being displayed will changed font if the user choose font.

package hw;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class HW {


public static void main(String[] args) {

/*from this code is adding the frame, label, textfield, panels, panel background color and the location of the labels and textfields*/

    String [] cb =  {"Comic Sans MS", "Times New Roman", "Arial Black"};
    JFrame frames = new JFrame();
    frames.setVisible(true);
    frames.setSize(700, 500);
    frames.setResizable(false);
    frames.setLocation(170, 100);
    JPanel panels = new JPanel();
    frames.add(panels);
    panels.setBackground(new Color(40, 136, 168));
    panels.setLayout(null);
    JTextField tf1 = new JTextField();
    panels.add(tf1);
    tf1.setBounds(90, 150, 100, 25);
    JLabel label1 =  new JLabel("ENTER TEXT");
    panels.add(label1);
    label1.setBounds(100, 30, 150, 100);

    JLabel label2 = new JLabel("FONT STYLE");
    panels.add(label2);
    label2.setBounds(400, 30, 150, 100);
    JComboBox combo = new JComboBox(cb);
    panels.add(combo);
    combo.setBounds(400, 150, 150, 25);

    JLabel label3 = new JLabel("");
    panels.add(label3);
    label3.setBounds(310, 250, 150, 100);
    label3.setText("");

 /* this part below is the itemlistener and itemevent, i dont know the if this part below is correct because the font in the inserted text wont change but the text being insert in textfield is showing up in the jlabel*/

    combo.addItemListener(new ItemListener() {

    @Override
    public void itemStateChanged(ItemEvent event){
       String word;

       if (event.getStateChange()==ItemEvent.SELECTED){

       label3.setText(word=tf1.getText());
       label3.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
       }

       else if (event.getStateChange()==ItemEvent.SELECTED) {
       label3.setText(word=tf1.getText());
       label3.setFont(new Font("Times New Roman", Font.PLAIN, 14));
       }

       else if (event.getStateChange()==ItemEvent.SELECTED) {
       label3.setText(word=tf1.getText());
       label3.setFont(new Font("Arial Black", Font.PLAIN, 14));
       }

   /* the else and else if statement is not working, i dont know how to correct this problem*/    
       }
    }
    });
}

}

I have trouble correcting this problem, I dont know where is the main source of the problem why fonts wont change if they being choose in JComboBox.

1 Answer

  1. Kenneth- Reply

    2019-11-13

    This fixes the multiple logic problems in the itemStateChanged method (and works for each of the fonts). I would typically use an ActionListener for combo boxes, but YMMV.

        combo.addItemListener(new ItemListener() {
    
            @Override
            public void itemStateChanged(ItemEvent event) {
                String fontName = combo.getSelectedItem().toString();
    
                if (event.getStateChange() == ItemEvent.SELECTED) {
                    label3.setText(tf1.getText());
                    label3.setFont(new Font(fontName, Font.PLAIN, 14));
                } 
            }
        });
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>