Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JTable numeric validation

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
 
Rancher
Posts: 3263
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recommended reading http://download.oracle.com/javase/tutorial/uiswing/components/table.html#data
 
jonathan benz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

}
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3263
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:


Surely a check using instanceof would be preferred over catching a ClassCastException?
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic