- 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
How to highlight multiple rows in a sequence in a table with Java Swing?
To highlight multiple rows in a table, you can use the addRowSelectionInterval() method. At first create a table −
DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);
Add some columns −
tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Views");
Now, add rows to the table −
tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});
Highlight multiple rows by adding interval of rows from both ends. Set interval (index) for both the parameters since we want multiple rows to be highlighted −
table.addRowSelectionInterval(5, 1);
The following is an example to highlight multiple rows in a sequence in JTable −
Example
package my; import java.awt.Color; import java.awt.Font; 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("Views"); tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"}); tableModel.addRow(new Object[] { "AWS", "No", "Yes", "8900"}); tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "1300"}); tableModel.addRow(new Object[] { "Java", "Yes", "No", "9686"}); tableModel.addRow(new Object[] { "jQuery", "Yes", "Yes", "4500"}); tableModel.addRow(new Object[] { "Python", "Yes", "No", "6789"}); tableModel.addRow(new Object[] { "Scala", "Yes", "No", "3400"}); tableModel.addRow(new Object[] { "Swift", "No", "Yes", "9676"}); table.addRowSelectionInterval(5, 1); table.setBackground(Color.BLUE); table.setForeground(Color.white); Font font = new Font("Verdana", Font.CENTER_BASELINE, 12); table.setFont(font); JFrame frame = new JFrame(); frame.setSize(600, 400); frame.add(new JScrollPane(table)); frame.setVisible(true); } }
Output
- Related Articles
- How to highlight a row in a table with Java Swing?
- How to multiple a matrix rows in R with a vector?
- How to insert multiple rows in a table using a single INSERT command in program?
- How to get the number of rows and columns of a JTable in Java Swing
- I want to highlight all the text in the Java Swing Control Text Pane
- How to make a canvas in Java Swing?
- How to replace rows in a MySQL table with conditions?
- How to deselect a range of rows from a table in Java?
- How can we delete multiple rows from a MySQL table?
- How to add a new row to JTable with insertRow() in Java Swing
- How to add some rows to a table through DefaultTableModel in Java
- How to leave space with EmptyBorder in Java Swing
- How to count number of rows in a table with jQuery?
- How to add a title to JTable in Java Swing?
- How will you extract multiple rows from a DB2 table in a single FETCH call?

Advertisements