-2

EDIT

I want to make a program wich allows me to randomly creates panels, I have a MVC implementation.

Here the scenario :

When I click button Next, I want to update the number of panels(Its a list of panels within a panelContainer) but the GUI won't. When I use the debugger, it looks correct. I empty the panelsContainerand and the valuesList and I fill it with new ones, but in the GUI, the old panels stay and the new are just added. So I find my self with 20 to N panels.

See the image

I think the problem is in Test()(GameView)

the old one was like this :

public void test(int number, List<Integer> panels){
    myListFigures.clear();
    nbpanels = number;
    myListTmp = panels;     

    panelsContrainer.removeAll();
    panelsContrainer.revalidate();
    setWork(nbpanels,myListTmp);
    panelsContrainer.revalidate();
}

My code:

GameModel: class where the numbers of panels and their values is created.

public class GameModel {

private boolean isDragging;       
private int numberToAcheve;
private int numberOfCut;
private int numberOfPanels;
private int sum;
private List<Integer> myListRandomValue = new ArrayList<>(); 
private List<Integer> myListPanelsValue = new ArrayList<>(); 

GameModel(){  

   this.isDragging = false;
   this.numberToAcheve = 0;
   this.numberOfCut= 0;
   this.numberOfPanels = 0;
   this.sum = 0;
   this.allWork();
}

public void allWork(){     
    int maximum = 6;
    int minimum = 3;
    Random rn = new Random();
    StringBuilder sb = new StringBuilder(); 
    int randomNumber = 0; 
    int range =  0;

    // decoupage()
    range =  maximum - minimum + 1;
    numberOfCut = rn.nextInt(range) + minimum; 

    // NumberAttribution()
    for(int instance = 0; instance < numberOfCut;instance++){
        if (Math.random() > 0.5) {
            randomNumber = 1 + (int) (Math.random() * 9);
        } else {
            randomNumber = 10 + (int) (Math.random() * 90);
        }
        myListRandomValue.add(randomNumber);
    }   

    // panelConcat()
    for(int i=0; i < myListRandomValue.size(); i++){
        sb.append(myListRandomValue.get(i));
    }

    for(int j=0; j< sb.length(); j++){
        myListPanelsValue.add(Integer.parseInt(sb.substring(j,j+1)));
    }
       numberOfPanels = myListPanelsValue.size();
}    

public int numberToAcheve(){
    for(int i = 0; i < myListPanelsValue.size(); i++)
    {
        sum += myListPanelsValue.get(i);
    }
 return sum;      
}   

public int getNumberToAcheve() {
    return numberToAcheve;
}

public int getSum() {
    return sum;
}   

public int getNumberOfPanels() {
    return numberOfPanels;
}

public List<Integer> getMyListPanelsValue() {
    return myListPanelsValue;
}
}  

GameView : class contain all components and adds panels with values by setWork()to GUI

public class GameView {

public List<Integer> myListTmp = new ArrayList<>(); 
public int nbpanels = 0;

public  List<Figure> myListFigures = new ArrayList<>(); 
private JPanel panelsContrainer = new JPanel(new GridLayout(1,0));
private JPanel controlsContrainer = new JPanel();

private final JFrame frame;
private JButton next   = new JButton("Next");        


public GameView(){                  

    frame = new JFrame("");
    frame.getContentPane().setLayout(new BorderLayout()); 

    controlsContrainer.add(next);        


    frame.getContentPane().add(panelsContrainer, BorderLayout.NORTH);
    frame.getContentPane().add(controlsContrainer, BorderLayout.CENTER);  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
    frame.setSize(800,300);
    frame.setVisible(true);   
}

public JButton getNext() {
    return next;
}
public void setWork(int numberOfPanels, List<Integer> panelsValues){
    for(int instance = 0; instance < numberOfPanels; instance++){
        myListFigures.add(new Figure(panelsValues.get(instance)));
        panelsContrainer.add(myListFigures.get(instance));    
    }
}

public void test(int number, List<Integer> panels){
    myListFigures.clear();
    nbpanels = number;
    myListTmp = panels;     

} 

}

GameController:

public class GameController {

private GameModel model;
private GameView view;


public GameController(GameModel model, GameView view) {
    this.model = model;
    this.view = view;        
      this.view.setWork(this.model.getNumberOfPanels(),this.model.getMyListPanelsValue());

}

public void contol(){        
    view.getNext().addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
            action();
       }
    });

}

private void action(){
model.allWork();                
    view.test(model.getNumberOfPanels(),model.getMyListPanelsValue());
}     

}
  • *"Change panel dynamically when button clicked"* Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Oct 16 '15 at 17:45
  • 1
    *"Can some one give me an example."* SO is not a code generation machine. *"I tried a lot of code, suggestions, but without results."* It would help if you show what you have tried, and describe why it did not work. – Andrew Thompson Oct 16 '15 at 17:46
  • Try calling `revalidate()` and then `repaint()` on your main `JFrame` after adding/removing components. – kajacx Oct 18 '15 at 20:02
  • The panelsContrainer is refreshing, but like I montienned above. The olds panels are not removed in the GUI. so the new ones are just added. I don't know why? – user3889307 Oct 18 '15 at 20:17
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Oct 18 '15 at 20:24
  • But this is what I have done ??. I can remove all the code but when I asked this question before. They demand me to post my code. – user3889307 Oct 18 '15 at 20:29

1 Answers1

0

There are many different possibilities (and using different Frameworks), let's cover two of them:

  1. You throw everything on the stage. What you do not want the user to see you can set to invisible (usually: node.setVisible(false);). If the user clicks Next you can hide the old nodes and display the new ones.
  2. You use prepared panels, and just swap them out. Example (JavaFX):

root.setCenter(new FirstPage());

If the user clicks on the Next Button:

root.setCenter(new SecondPage()):

Where root is a BorderPane (and your root view), FirstPage and SecondPage are classes extending a Node or Pane and contain your UI-elements.

showp1984
  • 378
  • 1
  • 2
  • 13