Ahmed Mostafa
Ahmed Mostafa

Reputation: 5

shifting Label in array of JLabel in swing

I want to shift my elements in my array of JLabel labels values depends on TextField values for example if I entered 10 my labels show 10 , if I entered 10 then 12 my labels shows 10 12 that works perfectly I want to make it sorted for example if I entered 10 then 6 labels show 6 then 10 that not works

 int j;

 for(j=0;j<Items;j++) {
     if(Integer.parseInt(labels[j].getText())
         > Integer.parseInt(uatxt[1].getText())) 
     break;

     for(int k=Items;k>j;k--){
         labels[k]=labels[k-1];
         labels[j]= new JLabel(uatxt[1].getText());
         labels[j].setBounds(40*j, 150, 130, 30);
         p.add(labels[j]);
         p.revalidate();
         p.repaint();
         Items++;

        //labels is my array
        //Items is number of elements in array
        // p is panel
        //uatxt[1] is my TextField 

    }
}

sorry if my question is trivial but i`m beginner in swing

Upvotes: 0

Views: 81

Answers (1)

Mordechai
Mordechai

Reputation: 16204

Don't do anything in the graphics, this problem is a logical problem.

After reading the field, tokenize them into an array:

String text = field.getText();
ArrayList<Integer> nums = new ArrayList<Integer>();
Scanner scan = new Scanner(text);
while(scan.hasNextInt())
    nums.add(scan.nextInt());

Collections.sort(nums)

Here you have beautifully sorted numbers in an ArrayList.

Upvotes: 1

Related Questions