Reputation: 314
I am writing an email app with java swing. I want the user to be able to change the font while writing an email, but I am not sure how to do that. I created a JComboBox that has all the fonts.
I guess I should use getSelectedItem()
so and add an actionListener to the JComboBox to pass this info to the JTextArea? Or there are other ways?
Here is my code:
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
JComboBox comboBox = new JComboBox(fonts);// create a combo box with the array
comboBox.setFont(new Font("Times New Roman", Font.PLAIN, 12));// set the font
comboBox.setBounds(21, 6, 193, 25);// set size and location
add(comboBox);
How should I make all the text field change the font according to the selected item in the comboBox?
Upvotes: 0
Views: 13046
Reputation: 112
Here you can find a working example of what you're asking (if I've understood right), embedded in a simple main method.
In order to keep it simple, I've used only a combo box for font name selection, but it would be nice to put two other combos in order to make font size and style (BOLD, PLAIN, ITALIC) selectable too.
Basically yes, the most common way is to bind an actionListener to comboBox, in order to change font when the comboBox has user's interaction. For sure this is not the only way, you can even listen for keyboard keys pressed to trigger the action, or implement some other way to recognize the user's intent to change font but, as far as I know, the action listener I used in the example is the most simple way.
package fontchooser;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ComboFont {
// Put this in other two combo-boxes if you want to make these selectable by user
public static int default_size = 16;
public static int default_style = Font.PLAIN;
public static void main(String[] args) {
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
GridBagLayout layout = new GridBagLayout();
layout.columnWidths = new int[] {400};
layout.rowHeights = new int[] {100,300};
JFrame container = new JFrame();
container.setLayout(layout);
container.setBounds(150,150,400,400);
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox<String> comboFontNames = new JComboBox<String>(fonts);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(textArea);
GridBagConstraints comboContraints = new GridBagConstraints();
comboContraints.gridx = 0;
comboContraints.gridy = 0;
// This only set font to display on combo
comboFontNames.setFont(new Font("Times New Roman", Font.PLAIN, 12));
container.add(comboFontNames, comboContraints);
GridBagConstraints scrollerContraints = new GridBagConstraints();
scrollerContraints.gridx = 0;
scrollerContraints.gridy = 1;
scrollerContraints.gridwidth = 400;
scrollerContraints.fill = GridBagConstraints.BOTH;
container.add(scrollPane, scrollerContraints);
// Variant of action listener with lambda (since java 8)
comboFontNames.addActionListener((e) -> {
String selectedFamilyName = (String)comboFontNames.getSelectedItem();
Font selectedFont = new Font(selectedFamilyName, default_style, default_size);
textArea.setFont(selectedFont);
textArea.repaint();
});
container.setVisible(true);
}
}
I Hope this is what you needed! bye
Alessio
Upvotes: 1