Java 单选按钮和 setSelected 还是别的什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18260801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:49:04  来源:igfitidea点击:

Radio buttons and setSelected or something else?

javaswingjradiobutton

提问by user2370759

I have a JFrame form with labels, text fields, a combo box and a button group that has 2 radio buttons. In another form I have a JTable filled with data about students (student id, name...), and when I select a row from a table, the form opens and its field need to be filled with same values (eg. if Peter Peterson was selected from the table, then his name should be shown in text filed Name on the form, so I did txtfieldName.setText(student.getName), my question is how do I do this for radio button? Do I need to have yes/no column in the table, so when I select a table row where the value is, say, ''yes'' in order to have yes radio button selected on the form?

我有一个 JFrame 表单,其中包含标签、文本字段、一个组合框和一个包含 2 个单选按钮的按钮组。在另一个表单中,我有一个 JTable 填充了有关学生的数据(学生 ID、姓名...),当我从表中选择一行时,表单打开并且它的字段需要填充相同的值(例如,如果 Peter Peterson 是从表中选择的,那么他的名字应该显示在表单上的文本字段 Name 中,所以我做了 txtfieldName.setText(student.getName),我的问题是如何为单选按钮执行此操作?我需要有表中是/否列,所以当我选择值是“是”的表行时,以便在表单上选择是单选按钮?

回答by Bill Horvath

You don't have to know which radio button is selected a priori, so you don't have to have that information in the table if you don't want to. You can instantiate both radio buttons with the 'selected' parameter set to false:

您不必知道先验地选择哪个单选按钮,因此如果您不想,则不必在表中提供该信息。您可以将 'selected' 参数设置为 false 来实例化两个单选按钮:

JRadioButton button = new JRadioButton("My button label", false);
JRadioButton otherButton = new JRadioButton("My other button label", false);
ButtonGroup group = new ButtonGroup();
group.add(button);
group.add(otherButton);

回答by Josh M

You select the radio buttons through the ButtonGroup:

您可以通过以下方式选择单选按钮ButtonGroup

group.getSelected(button.getModel(), true);

group.getSelected(button.getModel(), true);

回答by MadProgrammer

This depends. Is the value that is being applied to the radio button part of the underlying table data (ie from your student object)? If it is, then, no, you don't HAVE to have yes/no column, although it might be of use if you are trying to make decisions on it. Instead, you would simply extract it from the student object.

这取决于。应用于基础表数据的单选按钮部分的值是否(即来自您的学生对象)?如果是,那么,不,您不必有是/否列,尽管如果您试图对其做出决定,它可能会有用。相反,您只需从学生对象中提取它。

I would display the yes/no value as a column If it was relevant to the decsion making process.

如果它与决策过程相关,我会将是/否值显示为一列。

If the value you want to use to set the radio button is NOT part of the student oect then you need to make some decisions.

如果您想用来设置单选按钮的值不是学生对象的一部分,那么您需要做出一些决定。

Personally, I would create a wrapper object that represented the state of the row, this would include the student object and any other relevant details you might want. This makes the data self contained and makes accessing that information easier.

就个人而言,我会创建一个表示行状态的包装对象,这将包括学生对象和您可能想要的任何其他相关详细信息。这使得数据自包含并使访问该信息更容易。

If that wasnt desirable, then, yes, you would need to probably display the value as a column and maintain it as separate value within the table model.

如果这是不可取的,那么,是的,您可能需要将该值显示为一列,并将其作为表模型中的单独值进行维护。

This will also come down to how you've Implemented the table model

这也将归结为您如何实现表模型

Updated with feedback from the OP

更新了来自 OP 的反馈

Okay, so, with your studentobject hand. You can get the booleanproperty from the student(that is begin represented on the screen by you radio buttons) and make a choice...

好的,所以,用你的student对象手。您可以booleanstudent(即由您的单选按钮在屏幕上开始表示)获取该属性并做出选择...

trueRadioButton.setSelected(booleanValue);
trueRadioButton.setSelected(!booleanValue);

You could make it even more simpler by using a JCheckBoxinstead...

你可以通过使用一个JCheckBox来让它变得更简单......

checkBox.setSelected(booleanValue);

回答by Shivani

if (result.getString(5).equals("Male")) {
     jRadioButton1.setSelected(true);
} else { 
     jRadioButton2.setSelected(true);
}