I'm working on a program that changes words by others and I'm trying that the user can change as many words as he wants adding more lines when "add" is pressed. I'll try to explain myself, the program should looks something like this:
and if I press add:
if I keep pressing "add" it should adds more and more "lines".
To do that I use this:
JTextField oldword = new JTextField(5);
JTextField newword = new JTextField(5);
JPanel Panel = new JPanel();
List<JPanel> message = new ArrayList<JPanel>();
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
Panel.add(new JLabel("Change: "));
Panel.add(oldword);
Panel.add(Box.createHorizontalStrut(15));
Panel.add(new JLabel("by:"));
Panel.add(newword);
message.add(Panel);
while(response!=0 && response!=1){
response = JOptionPane.showOptionDialog(null, message, "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if(response==2){
message.add(Panel);
}
}
But when it gets to "response = JOptionPane.showOptionDialog" I only get a box with this:
If I press Add it starts repeating that message.
Sorry if I am not clear explaining my problem, I'm quite bad with words.
I found a way to do it, I'm sure it's not the best way to do it but...
public String[] setwords(){
int response=-12;
String[] options = new String[] {"Ok", "Cancel", "Add", "Delete"};
JPanel[] panel;
JTextField[] oldword = null;
JTextField[] newword = null;
JTextField[] oldwordaux=null;
JTextField[] newwordaux=null;
int size=1;
Boolean firsttime = true;
Boolean noword = true;
String[] words;
while(response!=0 && response!=JOptionPane.CLOSED_OPTION && response!=1){
panel = new JPanel[size];
oldword = new JTextField[size];
newword = new JTextField[size];
for(int i=0; i<size; i++){
panel[i] = new JPanel();
oldword[i] = new JTextField(3);
newword[i] = new JTextField(3);
panel[i].add(new JLabel("Change: "));
panel[i].add(oldword[i]);
panel[i].add(Box.createHorizontalStrut(15));
panel[i].add(new JLabel("by: "));
panel[i].add(newword[i]);
if(firsttime!=true){
if(i<oldwordaux.length){
oldword[i].setText(oldwordaux[i].getText());
newword[i].setText(newwordaux[i].getText());
}
}
}
firsttime = false;
response = JOptionPane.showOptionDialog(null, panel, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if(response==2){
size++;
oldwordaux=oldword;
newwordaux=newword;
}else if(response==3){
size=(size>1? size-1:size);
oldwordaux = Arrays.copyOfRange(oldword, 0, size);
newwordaux = Arrays.copyOfRange(newword, 0, size);
}
}
for(int j=0; j<oldword.length; j++){
if(oldword[j].getText().length()!=0){
noword=false;
}
}
if(noword){
return null;
}else{
int w=0;
words = new String[oldword.length*2];
for(int i=0; i<oldword.length; i++){
words[i]=oldword[i].getText();
w=i+1;
}
for(int i=0; i<newword.length; i++){
words[w]=newword[i].getText();
w++;
}
return words;
}
}
I create a new array bigger than the last one when "add" is pressed and I display the new one.