Font background color java

How To Set A Background Color, Font Size, Font Color To A JTable In Java


Project Source Code:

package javaapp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JTableColor < public static void main(String[] args) < JFrame frame = new JFrame();
JTable table = new JTable();

// create columns and rows to fill the jtable
Object[] columns =;

Object[][] rows =,
,
,
,
,
,
,

>;

DefaultTableModel model = new DefaultTableModel(rows, columns);

// set the model to the JTable
table.setModel(model);

// set a Background color to the Jtable
table.setBackground(Color.decode(«#058dc7»));

// set Font To table
table.setFont(new Font(«», 1, 42));

// set height to the table rows
table.setRowHeight(50);

// set color to the JTable Font
table.setForeground(Color.white);

JScrollPane pane = new JScrollPane(table);

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.add(pane,BorderLayout.CENTER);

frame.setContentPane(panel);

frame.setSize(900,500);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
>
>

/////////////// OUTPUT:

Источник

Customize the tooltip font, color , background and foreground color in Java

To customize the tooltip font, color and background, use UIManager.

UIManager.put("ToolTip.background", Color.ORANGE); UIManager.put("ToolTip.foreground", Color.BLACK); UIManager.put("ToolTip.font", new Font("Arial", Font.BOLD, 14));

Above, we have set the font with −

We have set the foreground and background color with the following above −

ToolTip.foreground ToolTip.background

The following is an example to customize tooltip −

Example

package my; import java.awt.Color; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.UIManager; public class SwingDemo < public static void main(String[] args) throws Exception < JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Register!"); JLabel label1, label2, label3; frame.setLayout(new GridLayout(2, 2)); label1 = new JLabel("Id", SwingConstants.CENTER); label1.setToolTipText("" + "Add the id given to you" + "
" + "in the beginning!" + ""); UIManager.put("ToolTip.background", Color.ORANGE); UIManager.put("ToolTip.foreground", Color.BLACK); UIManager.put("ToolTip.font", new Font("Arial", Font.BOLD, 14)); label2 = new JLabel("Rank", SwingConstants.CENTER); label2.setToolTipText("Enter rank"); label3 = new JLabel("Password", SwingConstants.CENTER); label3.setToolTipText("Enter Password"); JTextField emailId = new JTextField(20); JTextField rank = new JTextField(20); JPasswordField passwd = new JPasswordField(); passwd.setEchoChar('*'); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(emailId); frame.add(rank); frame.add(passwd); Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); int width = 500; int height = 200; frame.setBounds(center.x - width / 2, center.y - height / 2, width, height); frame.setVisible(true); > >

Источник

An Example of Setting Background and Font Color Code for Jlabel

To achieve your desired outcome, try implementing Solution 1 which involves adding two lines of code before setting the background color. First, set the opaque value to true. Additionally, note that a border is automatically set in JFrame but can be adjusted according to your preferences. Lastly, it is worth noting that text is typically displayed to the right of the image by default.

JButton, JTextFeild, JLabel set background color not working

To achieve your desired outcome, simply include two additional lines of code prior to assigning the background color.

  1. To assign a true opaque value, simply use the following code: btnlogin.setOpaque(true);
  2. In JFrame, the border is initially set by default, but it can be modified to your preference. To illustrate, I’ll demonstrate with the use of MatteBorder in the following example: btnlogin.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));

Subsequently, your anticipated outcome will be attained.

This piece of code alters the button’s color.

btnlogin.setOpaque(true); btnlogin.setBorder(null); btnlogin.setBackground(Color.red); // Any color you wish 

Changing font and color of jlabel Code Example, JLabel title = new JLabel(«Want a Raise?», JLabel.CENTER); title.setForeground(Color.white);

Changing font and background color of jTableHeader

Instead of attempting to modify the header renderer, you can use table.getTableHeader().setBackground() and table.getTableHeader().setFont() to accomplish this. Take a look at this example.

import javax.swing.*; import java.awt.*; public class TableHeaderFont < public static void main(String[] args) < JTable table = new JTable(new String[][] , >, new String[] ); table.getTableHeader().setBackground(new Color(239,198,46)); table.getTableHeader().setFont(new Font("Times New Roman", Font.BOLD, 12)); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new JScrollPane(table)); frame.pack(); frame.setVisible(true); > > 

Java — How to add Hex font color to a JLabel?, I searched stackOverFlow for answers but I still coudn’t find any (only for android. I use commandline). I want to add Hex color code as the font color. I first used this but I need to add out from JDK given system/defined colors. g2l2.setForeground(Color.BLUE); Something like this. But didn’t work.

How can set Font and Background color at child node of JXTreeTable

Since you are utilizing TreeCellRenderer , you are almost there. To determine your position within the tree and adjust the renderer accordingly, simply access the model (similar to DefaultTreeModel ) and implement getPathToRoot() .

DefaultTreeCellRenderer defaultTreeCellRenderer = new DefaultTreeCellRenderer() < @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) < setOpaque(true); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); if (model.getPathToRoot((TreeNode) value).length >2) < //Do your rendering goodness here >else if (expanded) < setForeground(Color.PINK); setBackground(Color.PINK); setFont(fontTotale); >return this; > >; 

Please note that I have made certain assumptions, therefore kindly make any necessary corrections based on your requirements.

  1. The model is a DefaultTreeModel
  2. The node is a TreeNode
  3. You only show concern if the node’s depth is greater than or equal to 3 levels.

Additionally, ensure that this function aligns with your requirements by referring to its JavaDoc.

Setting a background text color in java Code Example, Swift queries related to “setting a background text color in java” how to set jlabel text color; swing background color; set text color lable java; Jframe set label custom color; Jframe change color to lable; java set background color; set font color in jlabel; how to set background colour jlabel; change color in java swing; …

After setting the background image for a JLabel , how to display text over the image?

The JLabel should have the image as its background and the text must be overlaid on top of it.

The image is positioned in such a way that the text appears on its right side by default.

One way to modify this is by adjusting the attributes of the JLabel class. For instance:

label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.CENTER); 

Consult the JLabel API to explore additional properties that may prove helpful.

Java — After setting the background image for a JLabel, 6) new Font(«Jokerman», 3, 20) Don’t use numbers for defined constants. Something like new Font(«Jokerman», Font.ITALIC + Font.BOLD, 20) is both more informative to the reader, and gets compile-time checking. 7) «Jokerman» unless that font is supplied with the app, best to plan on using backups if it’s not …

Источник

How to Set Background and Font Color in XLSX Using POI in Java

In this page we will learn how to set color in our XLSX. Generally we need to set background color and font color for our rows and cell in excel file. Setting color can be used as heading or column name that increases the readability of excel file. In this example we will understand from scratch how to color in XLSX. All we need is to get an instance of CellStyle and then set the desired color to CellStyle and then assign it to XLSX cell. Create a XSSFWorkbook. Get CellStyle from XSSFWorkbook as below.

CellStyle style = workbook.createCellStyle();

We will set background color and also set the fill pattern. IndexedColors provides the index for a desired color. Find the code snippet how to do it.

style.setFillForegroundColor(IndexedColors.GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND);

Now we will see how to set font color. Get Font from XSSFWorkbook . And set desired font color as below. Finally assign the font instance to CellStyle instance.

Font font = workbook.createFont(); font.setColor(IndexedColors.RED.getIndex()); style.setFont(font);

Complete Example

Now we are all set with CellStyle instance that will be used while creating or updating cell. In our example we will create a row with two cell and set the CellStyle. Find the example.
ColorXLSX.java

package com.concretepage.poi; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ColorXLSX < public static void main(String[] args) throws IOException < Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Color Test"); Row row = sheet.createRow(0); CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); Font font = workbook.createFont(); font.setColor(IndexedColors.RED.getIndex()); style.setFont(font); Cell cell1 = row.createCell(0); cell1.setCellValue("ID"); cell1.setCellStyle(style); Cell cell2 = row.createCell(1); cell2.setCellValue("NAME"); cell2.setCellStyle(style); FileOutputStream fos =new FileOutputStream(new File("D:/xlsx/cp.xlsx")); workbook.write(fos); fos.close(); System.out.println("Done"); >>

To test the example, create a blank XLSX file on desired location and set the path. After running the program, the excel will look like as below.

Источник

Читайте также:  Circular images with css
Оцените статью