-
Strings and delimeters
HI people,
I am trying to get user input in a table in the form 00:00
with this in mind, the input has to be returned as a string for it to be in that format. Am thinking of using delimeters to seperate the two values and if the user attempts to input int or double an error shoud be displayed.
Anyone with an idea how i can do this?
Your opinions are highly appreciated
Best regards,
manfizy
-
You can use a Pattern and a Matcher to achieve this. With the correct regular expression you can make sure you always get the input you want.
-
I suggest to use regular expressions too in this case. It's much much better/safer than delimiters and stuff.
How did you going to have value type after separation, when you displaying the error message?
-
Am still a newbie in java and so am just trying things out - trial and error you know..
I tried reading the Pattarn and matcher class but i couldnt understand a thing lol!
All i want is to make sure as the user edits the table, the table can only accept inputs in "00:00" format. Apparently the input is time, and thats why i want it that way.
@Erlanga, i didnt understand your opinion/question please...
Best Regards,
Manfizy
-
Hi,
Are you using Java GUI or is it a HTML page?.Because ,if you use swings for user entry then we are having "javax.swing.JFormattedTextField" class in java.This class will allow the user to enter in the required format.Inside this "JFormattedTextField",they have internally used Patterns.
-Regards
Ramya
-
Search for some info on regular expressions. A simple one to use would be ^\d\d:\d\d$ but you could also be clever and restrict it to valid times using a re as well.
-
@Ramya - am using Netbeans to build my GUI. Already i have a table that is editable and the user can input some data on it. Your idea seems good. With my little knowledge in java i know that if am to use JFormatTextField i have to listen for editing changes or something..
Just shade somelight by a small example that listens to changes and then uses JFormatTextField.
I hope you dont mind my dumbass questions but am just eager to learn to java.
Thanks
-
Hi ,
I have taken a sample code from the net and edited according to your need.Just gothru.
This code will accept only number in "00:00" format.Just gothru the highlighted portion and study.
Code:
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.text.MaskFormatter;
public class SimpleFTF {
public static void main(String args[]) throws ParseException {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
[B] MaskFormatter mf1 = new MaskFormatter("##:##");
mf1.setPlaceholderCharacter('_');
JFormattedTextField ftf1 = new JFormattedTextField(mf1);[/B] content.add(ftf1);
f.setSize(300, 100);
f.setVisible(true);
}
}
-
Hi, thank you for your reply
This seems to be the solution to my problem but how can i impliment this on a table such that this happens only when the cells in the table are being edited??
-
You could of course, use the String.split function.
-
I did alil bit of research on how i can implement the maskFormatter method in a table and this is what i could come up with. BUT i still get some errors.
Could you please tell me exactly where i am making a mistake in my code
Code:
public void focusGained(FocusEvent e) {
try{
int rows = table1.getRowCount();
MaskFormatter mf1 = new MaskFormatter("##:##");
mf1.setPlaceholderCharacter(' ');
JFormattedTextField ftf1 = new JFormattedTextField(mf1); table1.add(ftf1);
for (int i =0;i<rows;i++){
table.setValueAt(ftf1 , i, 0);
}
}catch (NumberFormatException nfe){
System.out.println("invalid input");
}
}
public void focusLost(FocusEvent e) {
focusGained(e);
}
Apparently the formatting should only be in the first column cells. The rest should remain the way they are.
Best Regards,
Manfizy
-
I did a lil bit of research on how i can implement the masFormatter method in a JTable and this is what i came up with. BUT i still get some errors in my code.
Couls you please tell me exactly where i am making a mistake in my code??
Code:
public void focusGained(FocusEvent e) {
try{
int rows = table1.getRowCount();
MaskFormatter mf1 = new MaskFormatter("##:##");
mf1.setPlaceholderCharacter(' ');
JFormattedTextField ftf1 = new JFormattedTextField(mf1); table1.add(ftf1);
for (int i =0;i<rows;i++){
table1.setValueAt(ftf1 , i, 0);
}
}catch (NumberFormatException nfe){
System.out.println("invalid input");
}
}
public void focusLost(FocusEvent e) {
focusGained(e);
}
Apparently the formatting should be only in the first column cells and the rest should remain the way they are.
Best Regards,
Manfizy
-
I did a research on how i can implement masFormatter method in Jtable and came up with the code below THOUGH i still get some errors in my code.
could you please tell me where i could be making a mistake in my code.
Code:
public void focusGained(FocusEvent e) {
try{
int rows = table1.getRowCount();
MaskFormatter mf1 = new MaskFormatter("##:##");
mf1.setPlaceholderCharacter(' ');
JFormattedTextField ftf1 = new JFormattedTextField(mf1); table1.add(ftf1);
for (int i =0;i<rows;i++){
table1.setValueAt(ftf1 , i, 0);
}
}catch (NumberFormatException nfe){
System.out.println("invalid input");
}
}
public void focusLost(FocusEvent e) {
focusGained(e);
}
Apparently the formatting should only be in the first column cells and the rest should remain the same.
Best Regards
manfizy:confused: