Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable numeric validation

 
jonathan benz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having a j table. if i click the column and add string, the column do the numeric validation.
And if i press tab to the column and insert some string value, the column accept it.

This happen as i add Jtextfield into the j table column. Only if the user click on the column the JTextField will
come into action. And if the user simple TAB to the column and add value , then the JTextField will not get activated and the column might accept string value.

Please suggest how i can make the TAB operation set focus to the column's text field OR please suggest alternative for this issue.

Thanks.



TableColumn TC1 = table.getColumnModel().getColumn(1);
final JTextField text = new JTextField();
text.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent EVT) {
String value = text1.getText();
int l = value.length();
if ((EVT.getKeyChar() >= '0' && EVT.getKeyChar() <= '9') || EVT.getKeyChar()=='.'|| EVT.getKeyChar()=='\b' ) {
} else {
JOptionPane.showMessageDialog(null, "Please enter numeric value only")
text.setText("");
}}
});
table.setDefaultEditor(Object.class, new DefaultCellEditor(text));
 
Rob Camick
Ranch Hand
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override the getColumnClass(...) method of the JTable or TableModel to return Integer.class. The the table will choose a default numeric editor which does numeric editing for you by default so you don't need to create a custom editor.
 
jonathan benz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi . I am newbie to Swing.
please guide on how i can i Override the getColumnClass(...) method of the JTable or TableModel to return Integer.class
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
jonathan benz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for guidance.

Instead highlight the bolder red for non-numeric input , am i can popup JOptionPane message "Please enter numeric value".
please assist.
 
jonathan benz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i popup JOptionPane message('Please enter numeric value') for non-numeric value in JTable (colum 1 & column 3) instead default bolder highlight red .

//here the code:

public class Frame6 extends javax.swing.JFrame {

public Frame6() {

initComponents();

}

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setName("Form");

jScrollPane1.setName("jScrollPane1");

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

{null, null, null, null},

{null, null, null, null},

{null, null, null, null},

{null, null, null, null}

},

new String [] {

"Title 1", "Title 2", "Title 3", "Title 4"

}

) {

Class[] types = new Class [] {

java.lang.Object.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Integer.class

};

public Class getColumnClass(int columnIndex) {

return types [columnIndex];

}

});

jTable1.setName("jTable1"); // NOI18N

jScrollPane1.setViewportView(jTable1);

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jtabledemo.JTableDemoApp.class).getContext().getResourceMap(Frame6.class);

jTable1.getColumnModel().getColumn(0).setResizable(false);

jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N

jTable1.getColumnModel().getColumn(1).setResizable(false);

jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N

jTable1.getColumnModel().getColumn(2).setResizable(false);

jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N

jTable1.getColumnModel().getColumn(3).setResizable(false);

jTable1.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable1.columnModel.title3")); // NOI18N

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap(15, Short.MAX_VALUE)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap(14, Short.MAX_VALUE)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

pack();

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Frame6().setVisible(true);

}

});

}

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTable jTable1;

}
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan, can you please UseCodeTags next time? I would add them, but your code also lacks indentation so it would be almost as unreadable as it is now.
 
Rob Camick
Ranch Hand
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of a custom editor with a popup. You will need to customize the edit:

 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:

Surely a check using instanceof would be preferred over catching a ClassCastException?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic