Java swing color font

Setting JTextPane Font and Color

Swing’s javax.swing.JTextPane class represents a significant improvement over AWT’s TextArea . It allows complex combinations of character and paragraph attributes, embedded images and components, and other features that let you build styled text editors. Unfortunately, however, while JTextPane makes complex things possible, it makes some simple things difficult. Two examples are setting the default font and color.

Figure 1 shows an example of creating an AWT TextArea that uses a 20-point blue serif font.

import java.awt.TextArea; import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import java.awt.Color; import java.awt.Font; import java.awt.BorderLayout; public class Example1 extends WindowAdapter < public Example1() < Frame f = new Frame("TextArea Example"); f.setLayout(new BorderLayout()); // Make a text area, set its font and color, then add it to the frame TextArea text = new TextArea(); Font font = new Font("Serif", Font.ITALIC, 20); text.setFont(font); text.setForeground(Color.blue); f.add(text, BorderLayout.CENTER); // Listen for the user to click the frame's close box f.addWindowListener(this); f.setSize(400, 400); f.show(); >public void windowClosing(WindowEvent evt) < System.exit(0); >public static void main(String[] args) < Example1 instance = new Example1(); >>

Figure 1. Setting text font and color on an AWT TextArea

The code in Figure 1 includes a call to TextArea ‘s setFont(…) and setForeground(…) methods, which set the font and color of all of the text in the component. Figure 2 shows a (seemingly) equivalent example using Swing’s JFrame and JTextPane classes.

import javax.swing.JTextPane; import javax.swing.JFrame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import java.awt.Color; import java.awt.Font; import java.awt.BorderLayout; public class Example2 extends WindowAdapter < public Example2() < JFrame f = new JFrame("First JTextPane Example"); f.getContentPane().setLayout(new BorderLayout()); // Make a text pane, attempt to set its font and color, then // add it to the frame JTextPane text = new JTextPane(); Font font = new Font("Serif", Font.ITALIC, 20); text.setFont(font); text.setForeground(Color.blue); f.getContentPane().add(text, BorderLayout.CENTER); // Listen for the user to click the frame's close box f.addWindowListener(this); f.setSize(400, 400); f.show(); >public void windowClosing(WindowEvent evt) < System.exit(0); >public static void main(String[] args) < Example2 instance = new Example2(); >>

Figure 2. Seemingly equivalent version of Figure 1 using a Swing JTextPane

The code in Figure 2 does not produce the desired result: The text pane uses the default font and color (defined by the current Swing look-and-feel), rather than the font and color passed to the setFont(…) and setForeground(…) methods. The explanation for this lies in JTextPane ‘s flexibility. Since each character can have a different font and color, the notion of setting the font and color for all of the text does not make as much sense as it does in the case of the less-flexible TextArea .

The trick is to set the character attributes for the entire text content. If the text pane is empty, the character attributes will be used for any text entered later. Figure 3 shows an example of this. The static method setJTextPaneFont(…) contains the logic for updating a JTextPane ‘s attributes from Font and Color objects.

import javax.swing.JTextPane; import javax.swing.JFrame; import javax.swing.text.StyledDocument; import javax.swing.text.StyleConstants; import javax.swing.text.MutableAttributeSet; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import java.awt.Color; import java.awt.Font; import java.awt.BorderLayout; public class Example3 extends WindowAdapter < public Example3() < JFrame f = new JFrame("Second JTextPane Example"); f.getContentPane().setLayout(new BorderLayout()); // Make a text pane, set its font and color, then add it to the frame JTextPane text = new JTextPane(); Font font = new Font("Serif", Font.ITALIC, 20); setJTextPaneFont(text, font, Color.blue); f.getContentPane().add(text, BorderLayout.CENTER); // Listen for the user to click the frame's close box f.addWindowListener(this); f.setSize(400, 400); f.show(); >/** * Utility method for setting the font and color of a JTextPane. The * result is roughly equivalent to calling setFont(. ) and * setForeground(. ) on an AWT TextArea. */ public static void setJTextPaneFont(JTextPane jtp, Font font, Color c) < // Start with the current input attributes for the JTextPane. This // should ensure that we do not wipe out any existing attributes // (such as alignment or other paragraph attributes) currently // set on the text area. MutableAttributeSet attrs = jtp.getInputAttributes(); // Set the font family, size, and style, based on properties of // the Font object. Note that JTextPane supports a number of // character attributes beyond those supported by the Font class. // For example, underline, strike-through, super- and sub-script. StyleConstants.setFontFamily(attrs, font.getFamily()); StyleConstants.setFontSize(attrs, font.getSize()); StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0); StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0); // Set the font color StyleConstants.setForeground(attrs, c); // Retrieve the pane's document object StyledDocument doc = jtp.getStyledDocument(); // Replace the style for the entire document. We exceed the length // of the document by 1 so that text entered at the end of the // document uses the attributes. doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false); >public void windowClosing(WindowEvent evt) < System.exit(0); >public static void main(String[] args) < Example3 instance = new Example3(); >>

Figure 3. Setting font and color on a JTextPane

Читайте также:  Shutil copy python permission denied

The example in Figure 3 will produce 20-point blue text within the JTextPane .

Of course, JTextPane is not intended as a direct replacement for the AWT TextArea . Swing includes a JTextArea class that mimics TextArea ‘s behavior much more closely, and provides direct source code compatibility. There are, however, reasons to use JTextPane instead of JTextArea even in cases where you do not need to support multiple fonts within the text component. For example, JTextArea does not support embedded images or alternate text alignment.

Источник

Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here 🙂

JTextField – Font, Color & Alignment

1. JTextField Introduction

JTextField component will render an Edit control, which will take user input. We can construct it by passing an integer as a parameter and the integer specify the char-width. For example, if we pass a value of 15, Java Swing will create a text field to show 15 letters. We can change the colour of the JTextField by calling its setForground method. The setFont method is useful for setting a font to the JTextField.

2. About the JTextField Example

Have a look at the example below:

Java Swing - JTextField Example

The example shows three JTextFields. Each text fields contains the text in different text colour. Also, we may notice that the texts alignments are different. First JTextField shows the text left aligned. The second control shows the text aligned towards the middle of the text field. Final text field shows the text right aligned. You may also notice we display all texts in a bigger size than the default.

3. Create JTextFields

The below code shows that first we create flow layout for the JFrame. Then we create three JTextFields for our example. In all three instances, we specified a value of 30. This tells Java Swing the size of the text field in terms of Character Width and swing calculates the size in pixels. So, all our text boxes can show thirty letters.

Источник

How Do I Set JLabel Font Size and Color

JLabel is a display area for a short text string or an image, or both, it is a basic GUI Component defined in Java Swing library. A label does not react to input events. As a result, it cannot get the keyboard focus. In this how-to, we will go over how to set appearance: JLabel font size and color.

Читайте также:  Вывод словаря в словаре python

Example Source Code to set JLabel font size

JLabel jUserName = new JLabel("Demo How to Set JLabel font size"); jUserName.setFont(new Font("Serif", Font.BOLD, 11)); JFrame frame = new JFrame("Demo Window"); frame.add(jUserName); frame.setVisible(true);

Set JLabel foreground color

JLabel jUserName = new JLabel("Demo foreground "); jUserName.setForeground(Color.GREEN);

Set JLabel background color
By default, the background of JLabel is transparent, so you can not directly set backgroud for a jlabel component. Instead of, add A JLabel to JPanel and set background for the JPanel.

JLabel jUserName = new JLabel("Demo background"); JPanel titlePanel = new JPanel(); titlePanel.setBackground(Color.blue); titlePanel.add(jUserName);

Hello Dear, I have some error to set font and color in my program.Can you take a look for a moment,please? Here is mine: import java.awt.event.*;
import javax.swing.*; public class showYourLove extends JFrame
//inherits from JFrame class
private JPanel panel; //to references a panel
private JLabel msgLabel;//to reference a label
private JTextField LoveTxtField;//to reference a text field
private JButton Button;//to reference a button
private JLabel picLabel;
private final int width=400;//window width
private final int height=600;//window height public showYourLove() //constructor
setTitle(“Show Your Love !”);
setSize(width,height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true); > private void buildPanel() < //method that adds a lable,textfield and button JLabel msgLabel= new JLabel(“Show your love to your Mom by type I love You Mom”);
msgLabel.setFont(new Font(“Serif”, Font.BOLD, 14));
msgLabel.setForeground(Color.blue); LoveTxtField=new JTextField(10);
Button=new JButton(“Done”);
JLabel picLabel=new JLabel(new ImageIcon(“C:\\Documents and Settings\\User\\My Documents\\JCreator LE\\MyProjects\\LibGui\\showYourLove\\src\\love.GIF”)); //add an action listener to the button,below the code in which you declare a button of type JButton in the buildPanel() method. Button.addActionListener(new ButtonListener());
panel=new JPanel();
panel.add(msgLabel);
panel.add(LoveTxtField);
panel.add(Button);
panel.add(picLabel); > private class ButtonListener implements ActionListener
public void actionPerformed(ActionEvent e)
< String actionCommand=e.getActionCommand(); //Display the result JOptionPane.showMessageDialog(null,”Thank you for showing your love here.”); >
> public static void main(String[]args) < showYourLove love = new showYourLove(); >
>

I’m really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, its rare to see a great blog like this one these days..

Источник

Setting JTextPane Font and Color

Swing’s javax.swing.JTextPane class represents a significant improvement over AWT’s TextArea . It allows complex combinations of character and paragraph attributes, embedded images and components, and other features that let you build styled text editors. Unfortunately, however, while JTextPane makes complex things possible, it makes some simple things difficult. Two examples are setting the default font and color.

Figure 1 shows an example of creating an AWT TextArea that uses a 20-point blue serif font.

import java.awt.TextArea; import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import java.awt.Color; import java.awt.Font; import java.awt.BorderLayout; public class Example1 extends WindowAdapter < public Example1() < Frame f = new Frame("TextArea Example"); f.setLayout(new BorderLayout()); // Make a text area, set its font and color, then add it to the frame TextArea text = new TextArea(); Font font = new Font("Serif", Font.ITALIC, 20); text.setFont(font); text.setForeground(Color.blue); f.add(text, BorderLayout.CENTER); // Listen for the user to click the frame's close box f.addWindowListener(this); f.setSize(400, 400); f.show(); >public void windowClosing(WindowEvent evt) < System.exit(0); >public static void main(String[] args) < Example1 instance = new Example1(); >>

Figure 1. Setting text font and color on an AWT TextArea

The code in Figure 1 includes a call to TextArea ‘s setFont(…) and setForeground(…) methods, which set the font and color of all of the text in the component. Figure 2 shows a (seemingly) equivalent example using Swing’s JFrame and JTextPane classes.

import javax.swing.JTextPane; import javax.swing.JFrame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import java.awt.Color; import java.awt.Font; import java.awt.BorderLayout; public class Example2 extends WindowAdapter < public Example2() < JFrame f = new JFrame("First JTextPane Example"); f.getContentPane().setLayout(new BorderLayout()); // Make a text pane, attempt to set its font and color, then // add it to the frame JTextPane text = new JTextPane(); Font font = new Font("Serif", Font.ITALIC, 20); text.setFont(font); text.setForeground(Color.blue); f.getContentPane().add(text, BorderLayout.CENTER); // Listen for the user to click the frame's close box f.addWindowListener(this); f.setSize(400, 400); f.show(); >public void windowClosing(WindowEvent evt) < System.exit(0); >public static void main(String[] args) < Example2 instance = new Example2(); >>

Figure 2. Seemingly equivalent version of Figure 1 using a Swing JTextPane

Читайте также:  Основные конструкции языка java

The code in Figure 2 does not produce the desired result: The text pane uses the default font and color (defined by the current Swing look-and-feel), rather than the font and color passed to the setFont(…) and setForeground(…) methods. The explanation for this lies in JTextPane ‘s flexibility. Since each character can have a different font and color, the notion of setting the font and color for all of the text does not make as much sense as it does in the case of the less-flexible TextArea .

The trick is to set the character attributes for the entire text content. If the text pane is empty, the character attributes will be used for any text entered later. Figure 3 shows an example of this. The static method setJTextPaneFont(…) contains the logic for updating a JTextPane ‘s attributes from Font and Color objects.

import javax.swing.JTextPane; import javax.swing.JFrame; import javax.swing.text.StyledDocument; import javax.swing.text.StyleConstants; import javax.swing.text.MutableAttributeSet; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import java.awt.Color; import java.awt.Font; import java.awt.BorderLayout; public class Example3 extends WindowAdapter < public Example3() < JFrame f = new JFrame("Second JTextPane Example"); f.getContentPane().setLayout(new BorderLayout()); // Make a text pane, set its font and color, then add it to the frame JTextPane text = new JTextPane(); Font font = new Font("Serif", Font.ITALIC, 20); setJTextPaneFont(text, font, Color.blue); f.getContentPane().add(text, BorderLayout.CENTER); // Listen for the user to click the frame's close box f.addWindowListener(this); f.setSize(400, 400); f.show(); >/** * Utility method for setting the font and color of a JTextPane. The * result is roughly equivalent to calling setFont(. ) and * setForeground(. ) on an AWT TextArea. */ public static void setJTextPaneFont(JTextPane jtp, Font font, Color c) < // Start with the current input attributes for the JTextPane. This // should ensure that we do not wipe out any existing attributes // (such as alignment or other paragraph attributes) currently // set on the text area. MutableAttributeSet attrs = jtp.getInputAttributes(); // Set the font family, size, and style, based on properties of // the Font object. Note that JTextPane supports a number of // character attributes beyond those supported by the Font class. // For example, underline, strike-through, super- and sub-script. StyleConstants.setFontFamily(attrs, font.getFamily()); StyleConstants.setFontSize(attrs, font.getSize()); StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0); StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0); // Set the font color StyleConstants.setForeground(attrs, c); // Retrieve the pane's document object StyledDocument doc = jtp.getStyledDocument(); // Replace the style for the entire document. We exceed the length // of the document by 1 so that text entered at the end of the // document uses the attributes. doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false); >public void windowClosing(WindowEvent evt) < System.exit(0); >public static void main(String[] args) < Example3 instance = new Example3(); >>

Figure 3. Setting font and color on a JTextPane

The example in Figure 3 will produce 20-point blue text within the JTextPane .

Of course, JTextPane is not intended as a direct replacement for the AWT TextArea . Swing includes a JTextArea class that mimics TextArea ‘s behavior much more closely, and provides direct source code compatibility. There are, however, reasons to use JTextPane instead of JTextArea even in cases where you do not need to support multiple fonts within the text component. For example, JTextArea does not support embedded images or alternate text alignment.

Источник

Оцените статью