2016-03-21 6 views
-2

に削除ボタンを追加する方法を私は、下図のように削除ボタンとコンボボックスを作成したいと思います:コンボボックス

enter image description here

画像は、JavaのSwingを使用し、私は何をするのか分かりませんこれはJavaFXで行います。 2つのコンボボックス(a、b)を作成したいと思います。 ComboBox aで "cross"をクリックすると、アイテムを削除してこのアイテムをComboBox bに追加し、ComboBox bを追加したいと思います。

コンボボックスA: (1)、その後からそれを削除し、

B上のコンボボックスbの追加項目をクリックしてください: を(1)項目をクリックして何か(例の操作を行います。プリントアイテムを)

(2)次に、Bからそれを削除して

アドオンを横切るクリック
package UnitTest; 

import Peer.Peer_Manager; 
import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.chart.XYChart; 
import javafx.geometry.Insets; 
import javafx.scene.control.ComboBox; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 

import javafx.scene.text.Text; 

public class temp extends Application { 
    final int height = 200; 
    final int weight = 300; 
    final int offset = 5; 
    Peer_Manager p_management; 
    XYChart.Series series_hop; 
    XYChart.Series series_gd; 

    @Override 
    public void start(Stage primaryStage) { 
     VBox vbox = new VBox(); 
     vbox.setPadding(new Insets(5, 5, 5, 5)); 
     vbox.setStyle("-fx-background-color: CORNSILK;"); 

     Scene scene = new Scene(vbox, weight, height); 
     primaryStage.setScene(scene); 

     HBox hbBtn = new HBox(); 
     Text t1=new Text(" A:"); 
     Text t2=new Text(" B:"); 

     String[] filename = {"A","B","C"};//conf.load_all(); 
     ComboBox<String> cb = new ComboBox<String>(); 
     cb.setItems(FXCollections.observableArrayList(filename)); 
     cb.setVisibleRowCount(10); 

     ComboBox<String> cb2 = new ComboBox<String>(); 
     cb.setVisibleRowCount(10); 

     vbox.getChildren().add(hbBtn); 
     hbBtn.getChildren().add(t1); 
     hbBtn.getChildren().add(cb); 
     hbBtn.getChildren().add(t2); 
     hbBtn.getChildren().add(cb2); 

     cb.setOnAction(e -> { 
      try { 
       Object object = cb.getValue(); 
       if (object != null) { 
        cb2.getItems().add(object); 
        cb.getSelectionModel().clearSelection(); 
        cb.getItems().remove(object); 
       } 
      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     }); 

     //would like to do something(ex:print item),but don't remove 
     //add the "cross" beside items,click "cross" to remove item and add on cb 
     cb2.setOnAction(e -> { 
      try { 
       Object object = cb2.getValue(); 
       System.out.println(object); 
       if (object != null) { 
        cb1.getItems().add(object); 
        cb2.getSelectionModel().clearSelection(); 
        cb2.getItems().remove(object); 
       } 

      } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
      } 
     }); 
     primaryStage.setTitle("SimulatorFX"); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

コードはありますか? – Joris

+0

あなたの質問を編集し、問題があるものの[mcve]を含めてください。 –

+0

ControlsFXから[CheckComboBox](http://fxexperience.com/controlsfx/features/#checked)をチェックアウトすることができます。 – Itai

答えて

2

正しい方法は、CellFactoryを使用して、必要な要素を含むグラフィックノードを作成することです。ここに例があります:

public void start(Stage primaryStage) throws Exception { 
    ComboBox<String> cba = new ComboBox<>(); 
    ComboBox<String> cbb = new ComboBox<>(); 
    cba.getItems().addAll("A", "B", "C"); 
    cbb.getItems().addAll("123", "456", "789"); 

    // Set a cell factory for ComboBox A. A similar thing should be done for B. 
    cba.setCellFactory(lv -> 
      new ListCell<String>() { 
       // This is the node that will display the text and the cross. 
       // I chose a hyperlink, but you can change to button, image, etc. 
       private HBox graphic; 

       // this is the constructor for the anonymous class. 
       { 
        Label label = new Label(); 
        // Bind the label text to the item property. If your ComboBox items are not Strings you should use a converter. 
        label.textProperty().bind(itemProperty()); 
        // Set max width to infinity so the cross is all the way to the right. 
        label.setMaxWidth(Double.POSITIVE_INFINITY); 
        // We have to modify the hiding behavior of the ComboBox to allow clicking on the hyperlink, 
        // so we need to hide the ComboBox when the label is clicked (item selected). 
        label.setOnMouseClicked(event -> cba.hide()); 

        Hyperlink cross = new Hyperlink("X"); 
        cross.setVisited(true); // So it is black, and not blue. 
        cross.setOnAction(event -> 
          { 
           // Since the ListView reuses cells, we need to get the item first, before making changes. 
           String item = getItem(); 
           System.out.println("Clicked cross on " + item); 
           if (isSelected()) { 
            // Not entirely sure if this is needed. 
            cba.getSelectionModel().select(null); 
           } 
           // Remove the item from A and add to B. You can add any additional logic in here. 
           cba.getItems().remove(item); 
           cbb.getItems().add(item); 
          } 
        ); 
        // Arrange controls in a HBox, and set display to graphic only (the text is included in the graphic in this implementation). 
        graphic = new HBox(label, cross); 
        graphic.setHgrow(label, Priority.ALWAYS); 
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY); 
       } 

       @Override 
       protected void updateItem(String item, boolean empty) { 
        super.updateItem(item, empty); 
        if (empty) { 
         setGraphic(null); 
        } else { 
         setGraphic(graphic); 
        } 
       } 
      }); 

    // We have to set a custom skin, otherwise the ComboBox disappears before the click on the Hyperlink is registered. 
    cba.setSkin(new ComboBoxListViewSkin<String>(cba) { 
     @Override 
     protected boolean isHideOnClickEnabled() { 
      return false; 
     } 
    }); 

    VBox vb = new VBox(cba, cbb); 
    primaryStage.setScene(new Scene(vb)); 
    primaryStage.show(); 
} 
+0

'setSkin()'コールをどこにも置くことができないということを追加すると、コントローラのコンストラクタの後にスキンが即座に 'dispose()' dになり、これが'NullPointerException'のソース' ComboBox'を表示していました。コンストラクタで新しいスキンを設定することでそれが解決されました。 – Arno