我想制作一个应该像地址簿一样使用的JTable。 所以有一个用于创建新托管的按钮。 我想通过使用JOptionPane InpuDialog来创建这些托管,因为我只想要它 至少创建名称和年龄的Entrys。 另外我想检查没有重复(同名)。 所以我发布了我的添加按钮的代码。 检查年龄是否在1到100之间的工作是正确的,但我忘了检查所选名称是否真的不重复,如果你点击ok按钮我会接受重复。 所以我的问题是,它是否适用于实现与i循环的检查(就像我做了下面的波纹管)或者是否有更简单的方法来实现它?
boolean notAllowed=true;
boolean noCreation=false;
DefaultTableModel dtm = (DefaultTableModel) table
.getModel();
String s = JOptionPane.showInputDialog("Select Name");
for (int i = 0; i < dtm.getRowCount(); i++) {
if (s.equals(dtm.getValueAt(i, 0))) {
s = JOptionPane.showInputDialog("Selected Name already in use \n Select an other Name");
i = 0;
}
}
String c = JOptionPane
.showInputDialog("age?",JOptionPane.OK_OPTION);
while(notAllowed){
try{
int teste =Integer.parseInt(c);
if(teste==JOptionPane.CANCEL_OPTION)
{
notAllowed=false;
noCreation=true;
}
if(teste<=100 && teste>0 &¬Allowed)
notAllowed=false;
}
catch( Exception err)
{
notAllowed=false;
noCreation=true;
}
if(notAllowed)
c = JOptionPane.showInputDialog("Only Numbers between 1 and 100 are allowed");
}
if(!noCreation)
{
//create Entry
}
答案 0 :(得分:0)
我的建议是在一个名为controller的包中创建一个名为validation的验证文件中创建一个类Validation whit两个方法一个用于检查名称,另一个用于检查年龄范围然后调用此方法,这是用于re - 使用你的代码并简化。