Java Basic Problems

JTable Cell Edit for validation: How to assign two Different JTextField in two different JTable Column Cells.

Two JTextField in two different Table Column cellThis post is also related to JTable cell validation, suppose our JTable has 3 columns.  First column will take String or character type input and the second column or Column 2 will take integer type input from users. So, if we want to implement this scenario in our software we can solve it by assigning or using two different JTextField  for the cells of  Column 1 and Column 2 in JTable. In our example we used textBox1 (JTextField) in Column 1, which will  validate string or char type input from users and  for Column 2  I have assigned    textBox (JTextField) to validate int type input from the users.  The given solution is just a basic you can implement it as your own way.  Just Copy the following code and run it.

 

Code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

class JtableCellValidation extends JFrame

{

private JPanel topPanel;

private JTable table;

private JScrollPane scrollPane;

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

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

JTextField textBox=new JTextField();

JTextField textBox1=new JTextField();

public JtableCellValidation()

{

setTitle(“Two JTextFied In Two JTable Column”);

setSize(300,300);

setDefaultCloseOperation (EXIT_ON_CLOSE);

topPanel=new JPanel();

topPanel.setLayout(new BorderLayout());

getContentPane().add(topPanel);

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

dataValues=new String[][]

                   {

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

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

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

                   };

TableModel  model=new myTableModel();

table=new JTable();

table.setRowHeight(50);

table.setModel(model);

TableColumn soprtColumn=table.getColumnModel().getColumn(1);

soprtColumn.setCellEditor(new DefaultCellEditor (textBox));

TableColumn soprtColumn1=table.getColumnModel().getColumn(0);

soprtColumn1.setCellEditor(new DefaultCellEditor (textBox1));

table.setCellSelectionEnabled(true);

scrollPane=new JScrollPane(table);

topPanel.add(scrollPane,BorderLayout.CENTER);

textBox.addKeyListener(new KeyAdapter()

{

public void keyTyped(KeyEvent e)

{

if(!Character.isDigit(e.getKeyChar()) && e.getKeyChar() !=KeyEvent.VK_BACK_SPACE)

{

textBox.setEditable(false);

textBox.setBackground(Color.WHITE);

}

else

{

 textBox.setEditable(true);

}

}

});

textBox1.addKeyListener(new KeyAdapter()

{

public void keyTyped(KeyEvent ex)

{

if(Character.isDigit(ex.getKeyChar()) && ex.getKeyChar() !=KeyEvent.VK_BACK_SPACE)

{

textBox1.setEditable(false);

textBox1.setBackground(Color.WHITE);

}

else

{

textBox1.setEditable(true);

}

}

});

table.addMouseListener(new java.awt.event.MouseAdapter()  

{

public void mouseClicked(java.awt.event.MouseEvent e)

{

}

});

}

public class myTableModel extends DefaultTableModel

{

myTableModel()

{

super(dataValues,columnNames);

}

public boolean isCellEditable(int row,int cols)

{

return true;

}

}

public static void main(String args[])

  {

  JtableCellValidation x=new JtableCellValidation();

  x.setVisible(true);

  }  

}

1 thought on “JTable Cell Edit for validation: How to assign two Different JTextField in two different JTable Column Cells.”

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 )

Facebook photo

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

Connecting to %s