Ceriwis  

Go Back   Ceriwis > HOBI > Komputer & Teknologi > Programming

Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman.

Reply
 
Thread Tools
  #1  
Old 20th November 2011
Darkc0der Darkc0der is offline
Ceriwiser
 
Join Date: Nov 2011
Posts: 598
Rep Power: 14
Darkc0der mempunyai hidup yang Normal
Default Input only number in textfield java

Gan gw lagi belajar cara menggunakan plain document di textfield, tapi kenapa pada saat input textfield yang pertama, textfield yang kedua otomatis terisi juga yang isinya sama persis yang ada di textfield pertama, mohon pencerahan y gan....



thanks.




Code:

import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;

class DemoKomponen {

public static void main(String[] args) {

JFrame frame = new JFrame("Text Field");
JTextField text1 = new JTextField(10);
JTextField text2 = new JTextField(10);

PlainDocument numberOnly = new PlainDocument(){
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
char ch = str.charAt(0);
if (Character.isDigit(ch))
super.insertString(offs,str,a);
}
};

text1.setDocument(numberOnly);
text2.setDocument(numberOnly);
frame.getContentPane().add(text1);
frame.getContentPane().add(text2);
frame.setLayout(new FlowLayout());
frame.setSize(300,100);
frame.show();
}
}





Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


 


All times are GMT +7. The time now is 06:41 PM.


no new posts