- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set whether the column in the table model can be selected or deselected in Java?
We can set or disallow selection of column in the table using setColumnSelectionAllowed().
Let’s say the following is our table −
DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);
If you want to allow selection of column, then set the method to TRUE −
table.setColumnSelectionAllowed(true);
If you want to disallow selection of column, then set the method to FALSE −
table.setRowSelectionAllowed(false);
Here, in the below example we have disallowed selection of columns −
Example
package 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(); JTable table = new JTable(tableModel); tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Interview QA"); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "AWS", "No", "No", "Yes"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "Java", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "Yes"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "Yes"}); table.setRowHeight(3, 30); table.setColumnSelectionAllowed(false); JFrame f = new JFrame(); f.setSize(600, 400); f.add(new JScrollPane(table)); f.setVisible(true); } }
The output is as follows. Now you won’t be able to select a column −
Output
- Related Articles
- Set whether the row in the table model can be selected or deselected in Java?
- Set whether the cell in the table model can be selected or deselected in Java?
- Set whether the text of the element can be selected or not with CSS
- How to set whether the text of an element can be selected or not with JavaScript?
- Some changes are listed in the following table. For each change, write in the blank column, whether the change can be reversed or not.
- Check whether the file can be read in Java
- Set whether the table border should be collapsed into a single border or not with JavaScript?
- How can I set a table in a JPanel in Java?
- How to make a polygon object react to the selected and deselected event using FabricJS?
- How can we highlight the selected tab of a JTabbedPane in Java?
- How can we use LPAD() or RPAD() functions with the values in the column of a MySQL table?
- How can we set the background/foreground color for individual column of a JTable in Java?
- Set whether an animation should be played forwards or using CSS
- How to set whether the image-border should be repeated, rounded or stretched with JavaScript?
- How to set NULL values in a table column and insert the same

Advertisements