JTable Cell Empty Validation: How to check or know JTable cell is empty or not and also avoid null or space entry validation in JTable cell before saving JTable data in Database

JTable empty cell validation While designing or preparing  software you may face problem  to check  JTable cell is empty or not while entering JTable data in Database. Another thing may happen, suppose you are trying to validate your JTable cell empty or not but it is taking space or null entry as a string and your empty cell validation may not work. Here I am sharing you a little but useful technique by which you will be able to check your empty JTable cell and also if you enter space in the JTable cell it will catch this cell as empty and will show error. Just run the given program and check how it works.

 

 

 

Here is the Code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

class ButtonBelowTable extends JFrame

{

private JPanel topPanel;

private JTable table;

public JScrollPane scrollPane;

public JScrollPane scrollPane1;

private String[] columnNames=new String[3];

private String[][] dataValues=new String[3][3];

JButton button=new JButton(“Click Here”);

public ButtonBelowTable()

{

setTitle(“Button Below the Table”);

setSize(320,300);

setDefaultCloseOperation (EXIT_ON_CLOSE);

button.setBounds(75,200,90,30);

columnNames=new String[] {“Column 1″ ,”Column 2”, “Column 3”};

dataValues=new String[][]

{

{“a”,”2″,” 1″},

{“b”,”5″,” 5″},

{“c”,”8″,” 7″}

};

topPanel=new JPanel();

topPanel.setLayout(null);

getContentPane().add(topPanel);

table=new JTable();

TableModel  model=new myTableModel();

table.setModel(model);

scrollPane=new JScrollPane(table);

scrollPane.setSize(300,200);

scrollPane1=new JScrollPane(button);

scrollPane1.setSize(90,30);

scrollPane1.setBounds(75,200,150,30);

topPanel.add(scrollPane);      

topPanel.add(scrollPane1);

button.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(vaidCheck())

{

JOptionPane.showMessageDialog(null,” Great !! Field is filled up”);

}

else

{

JOptionPane.showMessageDialog(null,”Field is empty”);                                                                            

}

}

}

);

}

public boolean vaidCheck()

{

if(table.getCellEditor()!=null){

table.getCellEditor().stopCellEditing();

}

for(int i=0;i< table.getRowCount();i++)

{

for (int j=0;j<table.getColumnCount();j++)

{

String om=table.getValueAt(i,j).toString();

System.out.println(“\n Value =”+om+ ” and Length :”+om.length());

if(om.trim().length()==0)

{

return false;

}

}

}

return true;

}

public class myTableModel extends DefaultTableModel

{

myTableModel()

{

super(dataValues,columnNames);

}

public boolean isCellEditable(int row,int cols)

{

return true;

}

}

public static void main(String args[])

{

ButtonBelowTable x=new ButtonBelowTable();

x.setVisible(true);

}

}

5 Responses to JTable Cell Empty Validation: How to check or know JTable cell is empty or not and also avoid null or space entry validation in JTable cell before saving JTable data in Database

  1. Cruze says:

    I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the web will be much more useful than ever before.

    Like

  2. Hansford says:

    I like the valuable info you provide in your articles. I will bookmark your blog and check again here frequently. I am quite certain I will learn a lot of new stuff right here! Best of luck for the next!

    Like

  3. Tolliver says:

    You actually make this JTable empty cell validation seem so easy with your presentation but I find this topic to be actually something which I think I would never understand. It seems too complex and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

    Like

  4. Wynne says:

    We are a group of volunteers and opening a new scheme in our community. Your web site offered us with valuable info on JTable Empty Cell Validation . You have done an impressive job and our entire community will be grateful to you.

    Like

  5. Farhana says:

    i want to insert values in jtable and save in an array. when using tableChanged method only last value is displayed

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s