Found 4338 Articles for Java 8

Set the content of the JLabel to be left-justified and top-aligned in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

401 Views

To set the text of the label component to be left-justified and top-aligned, you need to set the alignment. Set the label to be on the left and top aligned −JLabel label = new JLabel("Department”); label.setVerticalAlignment(JLabel.TOP);Here, we have set the size of the label as well as the color that includes foreground and background color −label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.MAGENTA); label.setForeground(Color.WHITE);The following is an example to set the content of the label to be left-justified and top-aligned −package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {   ... Read More

How to set the location of the Tabs in a JTabbedPane Container to the left in Java?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

516 Views

To set the location of the tabs in a JTabbedPane container, use the LEFT constant. Here, we will set the position to the left −JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);The following is an example to set the location of the Tabs in a JTabbedPane Container to the left −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");       JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);       JTextArea text = new JTextArea(100, 100);       JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, ... Read More

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

George John
Updated on 30-Jul-2019 22:30:26

293 Views

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, ... Read More

Java Program to change the background color of rows in a JTable

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

807 Views

To change the background color of rows, you need to use the setBackground() method −JTable table = new JTable(marks, col); table.setBackground(Color.blue);Above, we have set the color using Color class −Color.blueThe following is an example to change the background color of rows −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class SwingDemo {    public static void main(String[] argv) throws Exception {       Integer[][] marks = {          { 70, 66, 76, 89, 67, 98 },          { 67, 89, 64, 78, 59, 78 },       ... Read More

Can we display a JTabPane with TextArea in one of the tabs with Java

Chandu yadav
Updated on 30-Jul-2019 22:30:26

199 Views

Yes, we can display a JTabPane with TextArea in one of the tabs. For that, let us first create a JTabbedPane component −JTabbedPane tabbedPane = new JTabbedPane();Now, create a text area you want to set under one of the tabs −JTextArea text = new JTextArea(100, 100);Now, set panels for the tabs. Under one of them, set the text area we created above as shown below −panel2 = new JPanel(); panel2.add(text); panel3 = new JPanel(); panel4 = new JPanel(); panel5 = new JPanel(); panel6 = new JPanel(); panel7 = new JPanel(); panel8 = new JPanel();Now, one by one create different tabs ... Read More

Java program to set the content of the JLabel to be left-justified and bottom-aligned

Smita Kapse
Updated on 30-Jul-2019 22:30:26

142 Views

To set the text of the label component to be left-justified and bottom-aligned, you need to set the alignment. Set the label to be on the left and bottom aligned −JLabel label = new JLabel("Fav Sports", JLabel.LEFT); label.setVerticalAlignment(JLabel.BOTTOM);Here, we have set the size of the label as well as the color that includes foreground and background color −label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.YELLOW); label.setForeground(Color.RED);The following is an example to set the content of the label to be left-justified and bottom-aligned −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Frame");       frame.setLayout(new FlowLayout());   ... Read More

How to disable auto resizing for a JTable in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

452 Views

To disable auto resizing for a table in Java, set the setAutoResizeMode() to AUTO_RESIZE_OFF −JTable table = new JTable(tableModel); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);The following is an example to disable auto resizing for a JTable in Java −package my; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       JFrame frame = new JFrame("Demo");       JPanel panel = new JPanel();       String data[][] = { {"Australia", "5", "1"},          {"US", "10", "2"},          {"Canada", ... Read More

How to create two borders for a single component in Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

247 Views

To create two borders for a single component, use the createCompoundBorder() method in Java. Here, we have created LineBorder nd TitledBorder −LineBorder lineBorder = new LineBorder(Color.red); TitledBorder titleBorder = new TitledBorder("Demo Title"); Border border = BorderFactory.createCompoundBorder(lineBorder, titleBorder);Now, set both the borders for a single component −JButton button = new JButton("two borders"); button.setBorder(border);The following is an example to create two borders for a single component −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.LineBorder; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String args[]) {       ... Read More

How to change header background color of a table in Java

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

To change header background color, at first get the header background −JTableHeader tableHeader = table.getTableHeader();Now, set the background color using set Background() −tableHeader.setBackground(Color.black);Above, we have used the Color class to set the color.The following is an example to change the header background color of a JTable −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.JTableHeader; public class SwingDemo {    public static void main(String[] argv) throws Exception {       Integer[][] marks = {          { 70, 66, 76, 89, 67, 98 },          { 67, 89, 64, ... Read More

Display all the titles of JTabbedPane tabs on Console in Java

George John
Updated on 30-Jul-2019 22:30:26

301 Views

To display all the titles of the JTabbedPane, let us first get the count of tabs −int count = tabbedPane.getTabCount();Now, loop through the number of tabs in the JTabbedPane. Use the getTitleAt() to get the title of each and every tab −for (int i = 0; i < count; i++) {    String str = tabbedPane.getTitleAt(i);    System.out.println(str); }The following is an example to display all the titles of JTabbedPane tabs on Console −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");     ... Read More

Advertisements