Found 4337 Articles for Java 8

How to create a compound border for a component in Java?

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

435 Views

Composed border consists of two or more borders i.e. border around a border. We can create it for a component in Java using the createCompoundBorder() method.Let’s say the following is our component −JLabel label; label = new JLabel("This has compound border (border around a border)!");Now, set the compound border −label.setBorder(BorderFactory.createCompoundBorder(BorderFactory    .createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder()));The following is an example to create a compound border for a component −Examplepackage my; import javax.swing.BorderFactory; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       JLabel ... Read More

How to create Titled Border for a component in Java?

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

2K+ Views

To create a titled border for a component in Java, use the createTitledBorder() method. Let’s say we have a panel and we need to set a titled border to it. Here’s our panel −JPanel panel = new JPanel();Now, set the border and set the text for the tites border −panel.setBorder(BorderFactory.createTitledBorder(    BorderFactory.createEtchedBorder(), "My Demo Table", TitledBorder.LEFT, TitledBorder.TOP));The following is an example to create a titled border −Examplepackage my; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();   ... Read More

Java program to set the color of a single tab’s text in a JTabbedPane Container

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

300 Views

To set the the color of a single tab’s text, use the setForegroundAt() method. This gives an option to mention the index and the color. The index here is the index of the specific tab you want to color the text.Let us first create a JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Now, set the background color for one of the tabs with index 2 −tabbedPane.setForegroundAt(2, Color.RED);The following is an example wherein we will update the foreground color of a single tab in the JTabbedPane −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) { ... Read More

How to disable a Tab in a JTabbedPane Container with Java?

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

2K+ Views

To disable a tab in a JTabbedPane container, use the setEnabledAt() method and set it to false with the index of the tab you want to disable.Let’s first create a JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Now, let us disable a tab at index 2 −tabbedPane.setEnabledAt(2, false);The following is an example to disable a tab in a JTabbedPane Container −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();       JTextArea text = new JTextArea(100, 100); ... Read More

How to enable Scrolling Tabs in a JTabbedPane Container

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

213 Views

To enable scrolling tabs in a JTabbedPane container, use the setTabLayoutPolicy() method −tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);Above, we have set the constant to be SCROLL_TAB_LAYOUT, since we want the scroller to be visible when all the tabs won’t fit within a single run.The following is an example to enable scrolling tabs in a JTabbedPane container −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();       JTextArea text = new JTextArea(100, 100);       JPanel panel1, panel2, panel3, ... Read More

How to retrieve the value from a table cell with TableModel in Java?

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

658 Views

At first, create a table with DefaultTableModel −String data[][] = {    {"Australia", "5", "1"},    {"US", "10", "2"},    {"Canada", "9", "3"},    {"India", "7", "4"},    {"Poland", "2", "5"},    {"SriLanka", "5", "6"} }; String col [] = {"Team", "Selected Players", "Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data, col); JTable table = new JTable(tableModel);Now, use the getModel() to retrieve the value from table cell −Object ob = table.getModel().getValueAt(3, 2); System.out.println("Value = "+ob);The following is an example to retrieve the value from a table cell with TableModel −Examplepackage my; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JScrollPane; import ... Read More

Java Program to retrieve the value from a cell in a JTable

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

1K+ Views

To retrieve the value of a cell, use the getValueAt() method. As parameters, set the row and column index value for which you want the cell value −int rIndex = 5; // row index int cIndex = 1; // column index Object ob = table.getValueAt(rIndex, cIndex);Display the cell value in the Console −System.out.println("Value = "+ob);The following is an example to retrieve the value from a cell −Examplepackage 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 ... Read More

How to set magins between cells of a JTable in Java?

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

412 Views

To set the margins i.e. row and columns margins between cells of a table, use the setIntercellSpacing() method −Dimension dim = new Dimension(50, 2); table.setIntercellSpacing(new Dimension(dim));Above, we have used the Dimension class −The following is an example to set margins between cells of a JTable cell −Examplepackage 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[][] = {     ... Read More

Java Program to display both horizontal and vertical grid lines in a JTable

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

228 Views

To display both horizontal and vertical grid lines in a table, use the setShowGrid() method and set it to true −table.setShowGrid(true);The following is an example to display both horizonal and vertical grid lines in a JTable −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 },          { 68, 87, 71, ... Read More

How to set the grid color of a table in Java?

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

814 Views

To set the grid color of a table, use the setGridColor() method.table.setGridColor(Color.yellow);Above, we have used the Color class to set the color.The following is an example to set the grid color of a table −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder(          BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP));       String[][] rec = {       ... Read More

Advertisements