1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Did you add any debug code to your listener? Add a simple System.out.println(...) statement to each if condition to see which block of code is being executed. I have no idea why you have 3 if statements with the same if condition. – camickr Oct 06 '17 at 14:57
  • i put joptionpane at the end of the each if else statement,. only if statement execute, the others dont.. – Dominic Agbada Cuizon Oct 07 '17 at 02:02
  • `panels.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Oct 07 '17 at 02:20

1 Answers1

1

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));
            } 
        }
    });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433