JavaFX CombBoxにTableViewから取得した値や、Databaseからの値をもとにレコードを選択したいと考えております。
現在は、cmbCombBox.getSelectionModel().select(i);
これを、AccessなどのCombBoxのように値をセットすると該当のレコードが表示されるようにできないでしょうか?
開発環境は、Luna 4.4.2です。
JavaFX
1// CombBoxクラス 2public class cmbSyainID { 3 private final SimpleIntegerProperty iSyainID = new SimpleIntegerProperty(); 4 private final SimpleStringProperty sSyainName = new SimpleStringProperty(); 5 6 public cmbSyainID() { 7 this (0,""); 8 } 9 public cmbSyainID (Integer id, String name) { 10 setSyainID(id); 11 setSyainName(name); 12 } 13// ――――――――――――――――――――――――――――――――――――――――――――――――――――― 14 public Integer getSyainID() { 15 return iSyainID.get(); 16 } 17 public void setSyainID(Integer id){ 18 iSyainID.set(id); 19 } 20 public String getSyainName() { 21 return sSyainName.get(); 22 } 23 public void setSyainName(String name) { 24 sSyainName.set(name); 25 } 26 public void setSelectedItem(Integer id) { 27 iSyainID.set(id); 28 } 29 public Integer getSelectedItem() { 30 return iSyainID.get(); 31 } 32// ――――――――――――――――――――――――――――――――――――――――――――――――――――― 33 public int getValue() { 34 return iSyainID.get(); 35 } 36 public void setValue(Integer id) { 37 iSyainID.set(id); 38 } 39 public String getText() { 40 return sSyainName.get(); 41 } 42 public void setText(String name) { 43 sSyainName.set(name); 44 } 45 public String toString() { 46 return sSyainName.get(); 47 } 48} 49// Controllerクラス 50public class SyainController implements Initializable { 51 @FXML ComboBox<cmbSyainID> cSyainID; 52 @FXML TableView<LstSyain> LstSyain; 53 @FXML TextField tSyainName; 54 55 Integer iBusyoID = 10; 56 PreparedStatement pstmt = null; 57 ResultSet rset = null; 58 59 @Override 60 public void initialize(URL url , ResourceBundle rb) { 61// * CombBox List 62// ――――――――――――――――――――――――――――――――――――――――――――――――――――― 63 setSyainList(); 64 cSyainID.getSelectionModel().select(0); 65 66// * TableView List 67// ――――――――――――――――――――――――――――――――――――――――――――――――――――― 68 LstSyain.getItems().setAll(returnSyainList()); 69 70 LstSyain.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<LstSyain> () { 71 @Override 72 public void changed(ObservableValue<? extends LstSyain> value, LstSyain oldValue, LstSyain newValue) { 73 if (LstSyain.getSelectionModel().getSelectedItem() != null) { 74 //getSyainName()は、LstSyainクラスにて宣言 75 tSyainName.setText((String) newValue.getSyainName()); 76 // ※※※※※※※※※※※※※※※※※※※※※※※※※※※ 77 //Listの動きに伴って、CombBoxの値も変更したい 78 //今回指摘して頂いた ⇒ select(T item) (B)選択すべきアイテムのvalueを指定をしてるつもり 79 cSyainID.getSelectionModel().select(newValue.getSyainID()); 80 // ※※※※※※※※※※※※※※※※※※※※※※※※※※※ 81 } 82 } 83 }); 84} 85 // * CombBox List. 86 //────────────────────────────── 87 public void setSyainList() { 88 String strSQL = "Execute dbo.PStad" 89 + " @NN = 'CBoxSource'" 90 + ", @BusyoID = " + Integer.toString(iBusyoID); 91 92 PreparedStatement pstmt = null; 93 ResultSet rset = null; 94 95 List<cmbSyainID> ll = new LinkedList<cmbSyainID>(); 96 cmbSyainID roww = new cmbSyainID(); 97 98 // ────────────────────────────── 99 try { 100 Connection con = DBConnect.connect(); //DBConnec Connectionクラス 101 pstmt = con.prepareStatement(strSQL); 102 rset = pstmt.executeQuery(); 103 104 while (rset.next()) { 105 roww = new cmbSyainID(); 106 roww.setSyainID(rset.getInt(1)); 107 roww.setSyainName(rset.getString(2)); 108 ll.add(roww); 109 } 110 rset.close(); 111 pstmt.close(); 112 } catch (Exception e) { 113 e.printStackTrace(); 114 } 115 cSyainID.getItems().setAll(ll); 116 } 117 118 // * TableView List. 119 //────────────────────────────── 120 private List<LstSyain> returnSyainList() { 121 Integer iID = 100; 122 123 PreparedStatement pstmt = null; 124 ResultSet rset = null; 125 List<LstSyain> ll = new LinkedList<LstSyain>(); 126 127 try { 128 Connection cnn = DBConnect.connect(); 129 String strSQL = "Execute dbo.PStad" 130 + " @nn = 'ListReturn'" 131 + ", @ID = " + Integer.toString(iID); 132 133 pstmt = cnn.prepareStatement(strSQL); 134 rset = pstmt.executeQuery(); 135 136 int i = 0; 137 while (rset.next()) { 138 i = i + 1; 139 //────────────────────────────── 140 Integer id = rset.getInt(1); 141 String name = rset.getString(2); 142 143 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 144 // 一番上のレコードの値ををコントロールに表示 145 if (i == 1) { 146 //※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ 147 // select(i) indexではなくValueでSelectしたい 148 // cmbCombBox.select(T item) 149 cSyainID.getSelectionModel().select(id); 150 //※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ 151 tSyainName.setText(name); 152 } 153 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 154 155 LstSyain roww = new LstSyain(); 156 roww.setSyainID(id); 157 roww.setSyainName(name); 158 159 ll.add(roww); 160 } 161 rset.close(); 162 pstmt.close(); 163 } 164 catch (Exception e) { 165 e.printStackTrace(); 166 } 167 return ll; 168 } 169}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/09 01:03
2017/03/09 03:41 編集
2017/03/09 05:03
2017/03/09 05:44