I need to display a dynamic bubble-sorted Array of integers into a JPanel, but after I sort the Array it doesn't show in the JPanel, I'm quite new at programming so this could be a rookie mistake.
This is the code I have so far.
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextField;
public class exH extends javax.swing.JFrame {
private List<JTextField> arrayA;
private List<JTextField> temp;
int n=0;
public exH() {
initComponents();
arrayA=new ArrayList();
temp=new ArrayList();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
n=Integer.parseInt(a.getText());
panel.removeAll();
arrayA.clear();
for (int i=0;i<n;i++){
JTextField txt =new JTextField();
panel.add(txt);
arrayA.add(txt);
panel.updateUI();
}
a.setText("");
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
for (int i=0;i<n-1;i++){
for (int j=0;j<n-1;i++){
int conv1 = Integer.parseInt(String.valueOf(arrayA.get(j)));
int conv2 = Integer.parseInt(String.valueOf(arrayA.get(j+1)));
if (conv1>conv2){
temp.set(0,arrayA.get(j+1));
arrayA.set(j+1,arrayA.get(j));
arrayA.set(j,temp.get(0));
}
}
}
}
And this is how the interface is looking.
This is the problem when you are doing some logic in a GUI.
What your are doing is :
List<JTextField>
and a JPanel with the same JTextField
.List
based on the value of the JTextFieldWhat you are not doing is to rebuild the JPanel with the new order. The JPanel don't care of the order of the List
so you can shuffle it, it won't update the view. If you change the value of the JtextField
instance, this is different because they share the same instances.