如何检查JavaFX ComboBox中是否选择了任何项目

时间:2016-05-01 05:55:37

标签: java javafx combobox

检查在JavaFX ComboBox中是否选择了任何项目或者是否没有选择项目时,有什么简单的方法?

检查处于此状态的ComboBox应该返回FALSE: ComboBox in this state should return FALSE

检查处于此状态的ComboBox应返回TRUE: ComboBox in this state should return TRUE

3 个答案:

答案 0 :(得分:13)

您可以使用

while (current != null && current.myData != null) {

如果boolean isMyComboBoxEmpty = myComboBox.getSelectionModel().isEmpty(); 项目中有null个项目,那也可以。

答案 1 :(得分:4)

所以我找到了一个简单的方法:

boolean isMyComboBoxEmpty = (myComboBox.getValue() == null);

答案 2 :(得分:0)

colors_list = ['#1f77b4',  # muted blue
               '#ff7f0e',  # safety orange
               '#2ca02c',  # cooked asparagus green
               '#d62728',  # brick red
               '#9467bd',  # muted purple
               '#8c564b',  # chestnut brown
               '#e377c2',  # raspberry yogurt pink
               '#7f7f7f',  # middle gray
               '#bcbd22',  # curry yellow-green
               '#17becf'  # blue-teal
                ]


dfs = list(df.groupby('dropdown'))
first_title = dfs[0][0]
traces = []
buttons = []
for i,d in enumerate(dfs):
    visible = [False]*4
    visible[i] = True
    name = d[0]
    traces.append(
        go.Scatter(x = d[1]["Year_quarter"],
                  y = d[1]["Price"],
                  text = "Average price",
                  hoverinfo = "x+y",
                  mode = 'lines+markers',
                  visible = True if i==0 else False,
                  name = name,
                  marker = {'color':colors_list[i%len(colors_list)],
                            'size': 10,
                            'opacity': 0.5,
                            'line': {'width': 0.5,
                                     'color': 'white'}
                         }
                  ))
    buttons.append(dict(label=name,
                        method="update",
                        args=[{"visible":visible},
                              {"title":f"Title {name}"}]))

updatemenus = [{'active':0, "buttons":buttons}]


fig = go.Figure(data=traces,
                 layout=dict(updatemenus=updatemenus))
fig.update_layout(title=first_title, title_x=0.5)
fig.update_yaxes(range=[0, df["Price"].max()*1.2])
fig.show()