Found 4338 Articles for Java 8

How to display horizontal grid lines in a JTable with Java?

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

647 Views

To display horizontal grid lines in a table, use the setShowHorizontalLines() method and set it to TRUE. Let us first create a table −String[][] rec = {    { "1", "Steve", "AUS" },    { "2", "Virat", "IND" },    { "3", "Kane", "NZ" },    { "4", "David", "AUS" },    { "5", "Ben", "ENG" },    { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header);Now, let us display vertical grid lines −table.setShowHorizontalLines(true);You can also give a color to these lines −table.setGridColor(Color.orange);The following is an example to display ... Read More

How to create JTable from two dimensional array in Java?

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

619 Views

With two dimensional array, set the columns of a table. Additionally, we have set the rows using a one-dimensional array as shown below −DefaultTableModel tableModel = new DefaultTableModel(new Object[][] {    { "Mobile Phones", "100" }, { "RAM", "200" }, { "Caps", "50" },    { "Tablet", "80" }, { "LED", "400" }, { "Trousers", "350" },    { "T-Shirt", "500" }, { "Hoodie", "650" }, { "Jeans", "900" } },    new Object[] { "Items", "Quantity" } );Now set the table model to the table −JTable table = new JTable(tableModel);The following is an example to create a table from ... Read More

Java Program to select a column in JTable?

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

925 Views

To select a column in a JTable, use the setColumnSelectionInterval() and set the interval for the column you would like to select.For example, if you want to select a single column i.e. column2, then set the interval as (2, 2) in the setColumnSelectionInterval() method.The following is an example to select a column from a table in Java −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(); ... Read More

Java Program to select all cells in a table

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

335 Views

To select all cells in a table in Java Swing, you need to use the selectAll() method. Let’s say the following are our table rows and columns −String[][] rec = {    { "001", "Shirts", "40" },    { "002", "Trousers", "250" },    { "003", "Jeans", "25" },    { "004", "Applicances", "90" },    { "005", "Mobile Phones", "200" },    { "006", "Hard Disk", "150" }, }; String[] header = { "ID", "Product", "Quantity" };Set it for a table −JTable table = new JTable(rec, header);Now select all the rows and columns −table.selectAll();The following is an example to ... Read More

How to add JTable to Panel in Java Swing?

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

2K+ Views

To add JTabel to Panel, let us first crerate a panel −JPanel panel = new JPanel();Now, create JTable and add rows and columns with the records −String[][] rec = {    { "1", "Steve", "AUS" },    { "2", "Virat", "IND" },    { "3", "Kane", "NZ" },    { "4", "David", "AUS" },    { "5", "Ben", "ENG" },    { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header);Add the above created table to panel −panel.add(new JScrollPane(table));The following is an example to add JTabel to Panel in Java ... Read More

Java Program to set Orientation and split components along y-axis

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

78 Views

Let us first create components to split. Here. We have two labels −JComponent one = new JLabel("Label One"); one.setBorder(BorderFactory.createLineBorder(Color.red)); JComponent two = new JLabel("Label Two"); two.setBorder(BorderFactory.createLineBorder(Color.blue));Now, set orientation and split along x-axis with HORIZONTAL_SPLIT −JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT); split.setTopComponent(one); split.setBottomComponent(two);The following is an example to set Orientation and split components along y-axis −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSplitPane; public class SwingDemo {    public static void main(String[] a) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComponent one = new JLabel("Label ... Read More

Java Program to enable cell selection in a JTable

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

421 Views

To enable cell selection, use the setCellSelectionEnabled() method and set it to TRUE −table.setCellSelectionEnabled(true);The above sets whether this table allows both a column selection and arow selection to exist simultaneously.The following is an example to enable cell selection in a JTable −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));       ... Read More

How to set orientation and split components along x-axis in Java?

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

103 Views

Let us first create components to split. Here. We have two labels −JComponent one = new JLabel("Label One"); one.setBorder(BorderFactory.createLineBorder(Color.red)); JComponent two = new JLabel("Label Two"); two.setBorder(BorderFactory.createLineBorder(Color.blue));Now, set orientation and split along x-axis with HORIZONTAL_SPLIT −JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); split.setLeftComponent(one); split.setRightComponent(two);The following is an example to set orientation and split components along x-axis in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSplitPane; public class SwingDemo {    public static void main(String[] a) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComponent one = ... Read More

Display only the horizontal scrollbar in Java

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

386 Views

To display only the horizontal scrollbar, use the VERTICAL_SCROLLBAR_NEVER constant, which eventually displays only the horizontal scrollbar.The following is an example to display only the horizontal scrollbar in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("Online Compiler");       JButton button2 = new JButton("Quiz");       JButton button3 = new JButton("Questions and Answers");       JButton button4 = new ... Read More

How to display a large component within a smaller display area in Java?

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

83 Views

To display a large component in a smaller display area, use JScrollPane, so that it’s easier for user to scroll through the component. The following is an example to display large component within a smaller display area in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("One");       JButton button2 = new JButton("Two");       JButton button3 = new JButton("Three"); ... Read More

Advertisements