Found 7442 Articles for Java

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

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

316 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

2K+ 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

446 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

How to set a MatteBorder from BorderFactory in Java?

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

199 Views

Set a MatteBorder from BorderFactory class −MatteBorder border = (MatteBorder)BorderFactory.createMatteBorder(2, -1, 5, 10, icon);Now, set the above created MatteBorder to a component −JButton button = new JButton("Matte Border"); button.setBorder(border);The following is an example to set a MatteBorder from BorderFactory class −Examplepackage my; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.MatteBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);       ImageIcon icon = new ImageIcon("new.gif");   ... Read More

How to apply adjustments to the last column of a JTable only, when the width of any column is changed in Java Swing?

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

416 Views

To apply adjustments to the last column only, use the setAutoResizeMode and set the mode. The mode here would be AUTO_RESIZE_LAST_COLUMN. This will allow you to adjust only the last columns even if any of the column header is dragged to resize.Let us first see an example to create a table in Java −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.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Technology"); ... Read More

How to create and set an Empty Border from BorderFactory class in Java?

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

277 Views

To create and set empty border to a component, use the BorderFactory class createEmptyBorder() method −EmptyBorder emptyBorder = (EmptyBorder) BorderFactory.createEmptyBorder();To set the above border to a component, use the setBorder() method −JButton button = new JButton("Empty Border"); button.setBorder(emptyBorder);The following is an example to create and set and empty border from BorderFactory class −package 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.JLabel; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.border.SoftBevelBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       ... Read More

How to get the number of rows and columns of a JTable in Java Swing

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

2K+ Views

To count the number of rows of a table, use the getRowCount() method −table.getRowCount()To count the number of columns of a table, use the getColumnCount() method −table.getColumnCount()The following is an example to get the number of rows and columns of a JTable −Examplepackage my; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Language/ Technology");       tableModel.addColumn("Text Tutorial");       tableModel.addColumn("Video Tutorial");     ... Read More

How to add empty border to a JButton in Java?

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

543 Views

To add empty border to a component, use the BorderFactory class createEmptyBorder() method −Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 0, 0);To set the above border to a component, use the setBorder() method −JButton button = new JButton("Empty Border"); button.setBorder(emptyBorder);The following is an example to ad empty border to a JButton −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.JLabel; import javax.swing.border.Border; import javax.swing.border.SoftBevelBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Border raisedBorder = new SoftBevelBorder(SoftBevelBorder.RAISED, ... Read More

Set whether the row in the table model can be selected or deselected in Java?

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

428 Views

We can set or disallow selection of row in the table using setRowSelectionAllowed().Let’s say the following is our table −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);If you want to allow selection of row, then set the method to TRUE −table.setRowSelectionAllowed(true);If you want to disallow selection of row, then set the method to FALSE −table.setRowSelectionAllowed(false);We have disallowed selection of rows in the below example −Examplepackage my; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       ... Read More

Java Program to add Combo Box to JTable

Chandu yadav
Updated on 19-Aug-2024 18:30:53

2K+ Views

In this article, we will learn how to add a JComboBox to a JTable in Java Swing. The JComboBox allows you to create a drop-down list within a table cell, enabling users to select from predefined options. Steps to add a combo box to JTable Following are the steps to add a combo box to JTable − First import the necessary packages. Initialize a JTable with 5 rows and 5 columns. Create a JComboBox and add items to it. Get the first column of ... Read More

Advertisements