
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to validate if JTable has an empty cell in Java?
A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable will generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces.
We can validate whether the JTable cell is empty or not by implementing the getValueAt() method of JTable class. If we click on the "Click Here" button, it will generate an action event and display a popup message like "Field is Empty" to the user.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableEmptyValidateTest extends JFrame { private JPanel panel; private JTable table; private JButton button; String[] columnNames = new String[] {"Student 1", "Student 2"}; String[][] dataValues = new String[][] {{"95", "100"}, {"", "85"}, {"80", "100"}}; public JTableEmptyValidateTest() { setTitle("Empty Validation Table"); panel = new JPanel(); table = new JTable(); TableModel model = new myTableModel(); table.setModel(model); panel.add(new JScrollPane(table)); button = new JButton("Click Here"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(validCheck()) { JOptionPane.showMessageDialog(null,"Field is filled up"); } else { JOptionPane.showMessageDialog(null, "Field is empty"); } } }); add(panel, BorderLayout.CENTER); add(button, BorderLayout.SOUTH); setSize(470, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public boolean validCheck() { if(table.getCellEditor()!= null) { table.getCellEditor().stopCellEditing(); } for(int i=0; i < table.getRowCount(); i++) { for(int j=0; j < table.getColumnCount(); j++) { String value = table.getValueAt(i,j).toString(); if(value.trim().length() == 0) { return false; } } } return true; } class myTableModel extends DefaultTableModel { myTableModel() { super(dataValues, columnNames); } public boolean isCellEditable(int row, int cols) { return true; } } public static void main(String args[]) { new JTableEmptyValidateTest(); } }
Output
- Related Questions & Answers
- Java Program to enable cell selection in a JTable
- How to set a value in a particular JTable cell with Java?
- How can we add/insert a JButton to JTable cell in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?
- How can we disable the cell editing inside a JTable in Java?
- Java Program to retrieve the value from a cell in a JTable
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- How to validate if an element in an array is repeated? - JavaScript
- How to know if an object has an attribute in Python?
- How to empty an array in Java
- How to check if String is empty in Java?
- How to write an empty function in Java
- How to check if an element has a class in jQuery?
- How to validate an email address using Java regular expressions.
- MySQL query to update a specific cell to be empty
Advertisements