Question

this is my first time using a JComboBox for a Java GUI assignment, I have multiple JComboBoxes to modify a user's string input (such as font, color and size) and display the output on a JLabel in the GUI. I have successfully modified my output Label using the font and color combo boxes, but I currently have no idea on how to set the size of my label, without interfering with my font and color combo box. Here's my code:

package javaassignment;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class JavaAssignment implements ActionListener {
    JPanel panel;
    JLabel textLabel, fontTypeLabel, fontSizeLabel, fontColorLabel, resultLabel, newLineLabel;
    JTextField textField;
    JComboBox fontTypeComboBox, fontSizeComboBox, fontColorComboBox;
    JButton okButton, cancelButton;

    GridLayout glay;
    //Declaration of all objects needed on the interface

    public JPanel createPaneContent() {
     panel = new JPanel();

     glay = new GridLayout (6,2,50,50); //setting layout grid arrangement and 50,50 for spaces in between

     panel.setLayout(glay);

     textLabel = new JLabel("Enter Text:");
     textField = new JTextField(10);
     fontTypeLabel = new JLabel("Select font type: ");

     String fontTypes[] = {"Consolas", "Calibri", "Arial", "Times New Roman"};
     fontTypeComboBox = new JComboBox(fontTypes);
     fontTypeComboBox.setSelectedIndex(0);
     fontTypeComboBox.addActionListener(this);
     //Declaring arrays for the JComboBox that modifes font styles, and adding action listener.

     fontSizeLabel = new JLabel("Select font size: ");

     String fontSizes[] = {"Small", "Medium", "Big"};
     fontSizeComboBox = new JComboBox(fontSizes);
     fontSizeComboBox.setSelectedIndex(0);
     fontSizeComboBox.addActionListener(this);
     //Declaring arrays for the JComboBox that modifies font sizes, and adding action listener.

     fontColorLabel = new JLabel("Select font color");

     String fontColors[] = {"Default", "Red", "Green", "Blue"};
     fontColorComboBox = new JComboBox(fontColors);
     fontColorComboBox.setSelectedIndex(0);
     fontColorComboBox.addActionListener(this);
     //Declaring arrays for the JComboBox that modifies the color, and adding action listener.

     resultLabel = new JLabel("Your text will appear here");
     newLineLabel = new JLabel("");
     //newLineLabel is used to occupy space on the right side of resultLabel, to force the OK and Cancel button below. 
     okButton = new JButton("OK");
     okButton.addActionListener(this);
     cancelButton = new JButton("Cancel");
     cancelButton.addActionListener(this);

     panel.add(textLabel);
     panel.add(textField);
     panel.add(fontTypeLabel);
     panel.add(fontTypeComboBox);
     panel.add(fontSizeLabel);
     panel.add(fontSizeComboBox);
     panel.add(fontColorLabel);
     panel.add(fontColorComboBox);
     panel.add(resultLabel);
     panel.add(newLineLabel);
     panel.add(okButton);
     panel.add(cancelButton);
     //adding objects to the GUI all in order 

     return panel;

     //referenced from http://www.macs.hw.ac.uk/guidebook/?name=JComboBox&page=1 
    }

    public void actionPerformed(ActionEvent aE) {

        if (aE.getSource() == okButton) {
            String text; //variable to hold text input from result label
            int temp1 = fontTypeComboBox.getSelectedIndex();
            int temp2 = fontSizeComboBox.getSelectedIndex();
            int temp3 = fontColorComboBox.getSelectedIndex();
            //temporary variables initialized to get selected value from JComboBoxes. 
            text = (textField.getText()); //variable to hold string input from textField

            resultLabel.setText("" + text); //setting the modified text to the resultLabel to display in textField on the GUI


            if (temp1 == 0) {
                Font consolasFont = new Font("Consolas", Font.PLAIN, 15);
                resultLabel.setFont(consolasFont);
            } else if (temp1 == 1) {
                Font calibriFont = new Font("Calibri", Font.PLAIN, 15);
                resultLabel.setFont(calibriFont);
            } else if (temp1 == 2) {
                Font arialFont = new Font("Arial", Font.PLAIN, 15);
                resultLabel.setFont(arialFont);
            } else if (temp1 == 3) {
                Font timesNewRomanFont = new Font("Times New Roman", Font.PLAIN, 15);
                resultLabel.setFont(timesNewRomanFont);
            } //end font type if statement

            if (temp2 ==0) {
                resultLabel.setFont(temp1, Font.PLAIN, 10);
            } //this if statement definitely will not work. 

            if (temp3 == 0) {
                resultLabel.setForeground(Color.BLACK);
            } else if (temp3 == 1) {
                resultLabel.setForeground(Color.RED);
            } else if (temp3 == 2) {
                resultLabel.setForeground(Color.GREEN);
            } else if (temp3 == 3) {
                resultLabel.setForeground(Color.BLUE);
            } //end font color statement




        } else if (aE.getSource() == cancelButton) {
            System.exit(0);
        } //end fontTypeComboBox if statement
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Font Modifier");
        frame.setSize(600,500);

        JavaAssignment fontModifier = new JavaAssignment();

        frame.setContentPane(fontModifier.createPaneContent());
        frame.setVisible(true);
    }
}

I tried creating an object similar to my font type if statement, but that would overwrite the font type of the label if I were to select a size in that JComboBox. I attempted to change the output label using "resultLabel.setText()" method but it doesn't work either. Any help or ideas would be appreciated

Was it helpful?

Solution

Here's what I would do.

  • Instead of using Strings for the font size use integers.

    Integer fontSizes[] = {10, 12, 14, 16, 18, 20, 22};
    
  • Instead of getSelectedIndex() use getSelectedItem(). You will need to cast the return value though. So you could do something like this

    fontString = (String)fontTypeComboBox.getSelectedItem();
    fontSize = (int)fontSizeComboBox.getSelectedItem();
    ...
    resultLabel.setFont(new Font(fontString, Font.PLAIN, fontSize));
    

Here are the changes I made, and it seems to work correctly

Integer fontSizes[] = {10, 12, 14, 16, 18, 20, 22};
fontSizeComboBox = new JComboBox(fontSizes);
....
public void actionPerformed(ActionEvent aE) {

    if (aE.getSource() == okButton) {
        String text; //variable to hold text input from result label
        String fontString = (String)fontTypeComboBox.getSelectedItem();
        int fontSize = (int)fontSizeComboBox.getSelectedItem();
        int temp3 = fontColorComboBox.getSelectedIndex();
        //temporary variables initialized to get selected value from JComboBoxes. 
        text = (textField.getText()); //variable to hold string input from textField

        resultLabel.setText("" + text); //setting the modified text to the resultLabel to display in textField on the GUI

        resultLabel.setFont(new Font(fontString, Font.PLAIN, fontSize));

        if (temp3 == 0) {
            resultLabel.setForeground(Color.BLACK);
        } else if (temp3 == 1) {
            resultLabel.setForeground(Color.RED);
        } else if (temp3 == 2) {
            resultLabel.setForeground(Color.GREEN);
        } else if (temp3 == 3) {
            resultLabel.setForeground(Color.BLUE);
        } //end font color statement

    } else if (aE.getSource() == cancelButton) {
        System.exit(0);
    } //end fontTypeComboBox if statement
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top