Found 9150 Articles for Object Oriented Programming

How to create JTable from two dimensional array in Java?

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

783 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 25-Oct-2024 23:40:18

1K+ Views

In this article, we will learn how to select a specific column in a JTable using Java’s Swing library. The program creates a simple table displaying a list of products along with their quantities. We use setColumnSelectionInterval() to highlight a single column based on an interval, so in this example, the “Quantity” column (column index 2) will be selected. The program also ensures that only columns, and not rows, can be selected. Steps to select a column in JTableFollowing are the steps to select a column in JTable −First we will import the classes from the javax.swing and java.awt package.Set up ... Read More

Java Program to select all cells in a table

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

453 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

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

141 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

Alshifa Hasnain
Updated on 15-Jan-2025 18:58:02

776 Views

In this article, we will learn to enable cell selection in a JTable in Java Swing. By default, JTable allows row or column selection, but enabling cell selection gives users the ability to select individual cells. This feature is useful for tasks that require precise control over table data, such as editing or copying specific values. Approach for Enabling Cell Selection in JTableThe goal is to create a JTable where users can select individual cells, rather than just rows or columns. The approach focuses on: Creating the JTable: We define a table with data ... Read More

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

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

158 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

542 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

137 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

How to set Raised and Lowered EtchedBorder for components in Java?

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

272 Views

To set raised EtchedBorder −Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);To set lowered EtchedBorder −Border loweredBorderEtched = new EtchedBorder(EtchedBorder.LOWERED);Now, set both the borders for components −JButton raisedButton = new JButton("Raised Border"); raisedButton.setBorder(raisedBorder); JLabel loweredLabel = new JLabel("Lowered Border Etched"); loweredLabel.setBorder(loweredBorderEtched);The following is an example to set raised and lowered EtchedBorder for components −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.EmptyBorder; import javax.swing.border.SoftBevelBorder; import javax.swing.border.EtchedBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       ... Read More

Advertisements