aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable numeric validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable numeric validation" Watch "JTable numeric validation" New topic
Author

JTable numeric validation

jonathan benz
Greenhorn

Joined: Aug 22, 2011
Posts: 11
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

Joined: Jun 13, 2009
Posts: 2091
    
    7
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

Joined: Aug 22, 2011
Posts: 11
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

Joined: Jul 26, 2007
Posts: 9990
    
    7

Recommended reading http://download.oracle.com/javase/tutorial/uiswing/components/table.html#data


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
jonathan benz
Greenhorn

Joined: Aug 22, 2011
Posts: 11
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

Joined: Aug 22, 2011
Posts: 11
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
Here is an example of a custom editor with a popup. You will need to customize the edit:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Rob Camick wrote:

Surely a check using instanceof would be preferred over catching a ClassCastException?
 
jQuery in Action, 2nd edition
 
subject: JTable numeric validation
 
Similar Threads
ctrl+tab is not working in editing mode
using tab key in jtable,my table consists panels ad cells
JTextField on getText() method
How to make JTextField accepts negative numeric input?
Concatnating value of DTO