Data Structure
 Networking
 RDBMS
 Operating System
 Java
 MS Excel
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP
- Selected Reading
 - UPSC IAS Exams Notes
 - Developer's Best Practices
 - Questions and Answers
 - Effective Resume Writing
 - HR Interview Questions
 - Computer Glossary
 - Who is Who
 
Java Articles - Page 328 of 745
 
			
			798 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
 
			
			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
 
			
			461 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
 
			
			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
 
			
			151 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
 
			
			800 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
 
			
			167 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
 
			
			551 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
 
			
			144 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
 
			
			285 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