Search code examples
javaswingactionlistenerjcomboboxitemlistener

how to make the fonts list works in the JComboBox


I tried many things and i only been able to put the fonts on the list but how can i make them works when they are selected?

this is my code

    package textModifier;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.logging.Handler;

import javax.swing.*;
import javax.swing.border.*;    
import javax.swing.*;

public class javaTextEditor extends JComboBox implements ActionListener, ItemListener {

    JFrame frame = new JFrame("A text modifier frame - Assignment 1");      
    Border layout = BorderFactory.createLineBorder(Color.RED, 1);

    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();

    JLabel label1 = new JLabel("Enter Message:");
    JLabel label2 = new JLabel("Select Font:");
    JLabel label3 = new JLabel();

    JTextField text = new JTextField("UIPG - Assignment 1");

    JComboBox fonts;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String fontName[] = ge.getAvailableFontFamilyNames();
    Vector vector = new Vector();

    ButtonGroup radio = new ButtonGroup();
    JRadioButton red = new JRadioButton("Red");
    JRadioButton green = new JRadioButton("Green");
    JRadioButton blue = new JRadioButton("Blue");

    JCheckBox Capitalized, Bold, Italic;

    JButton Left, Center, Right;

    public void ui(){

        for (int i = 1; i < fontName.length; i++)
            vector.addElement(fontName[i]);

        fonts = new JComboBox(vector); // wwwwqput the font names on the ComboBox

        Capitalized = new JCheckBox("Capitalized");
        Bold = new JCheckBox("Bold");
        Italic = new JCheckBox("Italic");

        Left = new JButton("Left");
        Center = new JButton("Center");
        Right = new JButton("Right");

        radio.add(red);
        radio.add(green);
        radio.add(blue);

        panel.add(label1);
        panel.add(text);
        panel.add(label2);
        panel.add(fonts);

        panel3.add(red);
        panel3.add(green);
        panel3.add(blue);

        panel2.add(Capitalized);
        panel2.add(Bold);
        panel2.add(Italic);

        panel4.add(Left);
        panel4.add(Center);
        panel4.add(Right);

        panel5.setBorder(layout);
        panel5.setBackground(Color.YELLOW);
        panel5.add(label3);

        Left.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == Left)
                    label3.setLocation(10, 8);


            }
        });

        Center.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == Center)
                    label3.setLocation(245, 8);

            }
        });

        Right.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == Right)
                    label3.setLocation(480, 8);

            }
        });
        red.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            if(red.isSelected())
                label3.setForeground(Color.red);
            }
        });

        green.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(green.isSelected())
                label3.setForeground(Color.green);
            }
        });


        blue.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(blue.isSelected())
                    label3.setForeground(Color.blue);
            }
        });

        text.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String str = text.getText();
                label3.setText(str);

            }
        });

        panel5.add(label3);
        frame.setVisible(true);
        frame.setSize(800, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
        panel4.setLayout(new FlowLayout(FlowLayout.CENTER));

        frame.getContentPane().add(BorderLayout.NORTH, panel);
        frame.getContentPane().add(BorderLayout.EAST, panel2);
        frame.getContentPane().add(BorderLayout.WEST, panel3);
        frame.getContentPane().add(BorderLayout.SOUTH, panel4);
        frame.getContentPane().add(BorderLayout.CENTER, panel5);

        Bold.addItemListener(this);
        Italic.addItemListener(this);
        Capitalized.addItemListener(this);


    }



    @Override
    public void actionPerformed(ActionEvent e) {

}



    @Override
    public void itemStateChanged(ItemEvent e) {
        Font f = null;

        if(Bold.isSelected() && Italic.isSelected() && Capitalized.isSelected()){
            f = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
            label3.setText(label3.getText().toUpperCase());
        }
        else if(Bold.isSelected())
            f = new Font("Serif", Font.BOLD, 14);
        else if(Italic.isSelected())
            f = new Font("Serif", Font.ITALIC, 14);
        else if(Capitalized.isSelected())
            label3.setText(label3.getText().toUpperCase());
        else{
            f = new Font("Serif", Font.PLAIN, 14);
        }
        label3.setFont(f);
    }

}

what should i write in the ActionListener that can make the fonts works?


Solution

  • Since you haven't posted you attempt at this (just an empty method), I'll try and point you in the right direction.

    First this you should do is take a look at the Font API. There you'll see methods like

    So in your listener, first get the font of whatever it is you want to change the font on.

    Font font = label.getFont();
    

    Then use the getters above to get the values and create/set a new font based of the new value recieved from the combo box. Something like

    String family = (String)combobox.getSelectedItem();
    Font font = label.getFont();
    int size = font.getSize();
    int style = font.getStyle();
    label.setFont(new Font(family, size, style));
    

    You can use the same technique for your listeners for your check boxes also, since I see you are hard coding the family.


    Side Notes:

    • Use java naming convention - variables begin with lower case letters Boldbold

    • Also I have absolutely no idea why your class is extending JComboBox. Looks like a completely faulty design