在javaFx中,我有类别GridPane,其中我在初始化时动态地加载了按钮。
See Image contains one Category GridPane and another Items GridPane
现在,当我点击任何类别按钮时,例如'类别2'然后它将加载Items GridPane中类别2的所有项目(按钮)。
然后,当我点击另一个类别时,它不会删除Items GridPane的旧元素。 Items GridPane的旧元素保持不变。
那么如何删除Items GridPane的旧元素并添加新Category的新元素。
public class GenerateBillController implements Initializable {
int m1=0,n1=0,m2=0,n2=0,z1=0,z2=0,y1=0,y2=0;
@FXML
GridPane gpCategory;
@FXML
GridPane gpItems;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
try {
cat=lm.getCategory();
list = FXCollections.observableArrayList();
for(int i=0;i<cat.length;i++)
{
list.add(cat[i]);
}
while(z1<16){ //add 16 buttons to Category GridPane
addCatButton();
z1++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addCatButton() {
// TODO Auto-generated method stub
final Button temp = new Button("Button " + y1);
final int numButton= y1;
temp.setId("" + y1);
temp.setText(cat[z1]);
temp.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
System.out.println("id("+temp.getId()+")="+numButton+"->"+temp.getText());
try {
//gpItems.getChildren().removeAll(list);
items=lm.getItems(temp.getText());
for(int i=0;i<items.length;i++)
{
list2.add(items[i]);
}
while(z2<9)
{
addItemButton();
z2++;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void addItemButton() {
// TODO Auto-generated method stub
final Button temp1=new Button("Button "+y2);
final int nb=y2;
temp1.setId(""+y2);
temp1.setText(items[z2]);
temp1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("id("+temp1.getId()+")= "+nb+"-> " +temp1.getText());
try {
String p=lm.getPrice(temp1.getText());
if(p=="")
{
}
else
{
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
gpItems.add(temp1, m2, n2);
if(m2==2)
{
m2=-1;
n2++;
}
m2++;
y2++;
}
});
gpCategory.add(temp, m1, n1);
if(m1==3)
{
m1=-1;
n1++;
}
m1++;
y1++;
}