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.setSize(700, 500);
    frames.setLocation(170, 100);
    JPanel panels = new JPanel();
    panels.setBackground(new Color(40, 136, 168));
    JTextField tf1 = new JTextField();
    tf1.setBounds(90, 150, 100, 25);
    JLabel label1 =  new JLabel("ENTER TEXT");
    label1.setBounds(100, 30, 150, 100);

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

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

 /* 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() {

    public void itemStateChanged(ItemEvent event){
       String word;

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

       label3.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));

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

       else if (event.getStateChange()==ItemEvent.SELECTED) {
       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


    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() {
            public void itemStateChanged(ItemEvent event) {
                String fontName = combo.getSelectedItem().toString();
                if (event.getStateChange() == ItemEvent.SELECTED) {
                    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>