How can we change the default font of the JTabbedPane tabs in Java?



To display the default font of the tabs, you need to use the Font class. Let’s say we created a JTabbedPane in Java βˆ’

JTabbedPane tabbedPane = new JTabbedPane();

Now, set the Font with font face, style and font size βˆ’

Font font = new Font("Arial", Font.CENTER_BASELINE, 20);
tabbedPane.setFont(font);

The following is an example to change the default font of the JTabbedPane tabs βˆ’

package my;
import javax.swing.*;
import java.awt.*;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Technologies");
      JTabbedPane tabbedPane = new JTabbedPane();
      JPanel panel1, panel2, panel3, panel4, panel5;
      panel1 = new JPanel();
      panel2 = new JPanel();
      panel3 = new JPanel();
      panel4 = new JPanel();
      panel5 = new JPanel();
      tabbedPane.addTab("Blockchain", panel1);
      tabbedPane.addTab("Salesforce", panel2);
      tabbedPane.addTab("SAS", panel3);
      tabbedPane.addTab("Matlab ", panel4);
      tabbedPane.addTab("Servlet", panel5);
      tabbedPane.setBackground(Color.blue);
      tabbedPane.setForeground(Color.white);
      Font font = new Font("Arial", Font.CENTER_BASELINE, 20);
      tabbedPane.setFont(font);
      frame.add(tabbedPane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(660,350);
      frame.setVisible(true);
   }
}

Output

Updated on: 2019-07-30T22:30:26+05:30

416 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements