动态地在 JRadioButton 上设置文本

setText on JRadioButton dynamically

I have one JInternalframe in which there are many JRadioButton and JLabel. I want to Retrive Data from Database and display on this JRadioButton and JLabel
由于数据库中可以有任意数量的行将被提取。 我已成功从数据库中获取数据,但无法在 JRadioButton 和 JLabel 上设置它

void setData(String s1, String s2, String s3, int cnt) {
       //Setting JRadioButton and JLabel Coding Part Here
  }

在上面的函数中,变量 s1,s2,s3 中存在我要显示的数据,变量 cnt 中的值为在 resultset.next()

上递增的计数器

谁能给我一些关于如何在 JRadiobutton 和 JLabel 上显示数据的提示
JRadioButton Variale 名称类似于 BT1、BT2、BT3... 所以我尝试了

由于数据是动态的,因此 GUI 也应该是动态的。

考虑以下示例:

import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class GuiRow {

  /** jRadioButton */
  private JRadioButton jRadioButton;

  /** studentIdLabel */
  private JLabel studentIdLabel;

  /** nameLabel */
  private JLabel nameLabel;

  /** courseLabel */
  private JLabel courseLabel;

  /**
   * Constructs a new instance.
   * 
   * @param studentIdtext
   * @param nameText
   * @param courseText
   */
  public GuiRow(String studentIdtext, String nameText, String courseText) {
    this.setjRadioButton(new JRadioButton(""));
    // TODO configure radio button

    this.setStudentIdLabel(new JLabel(studentIdtext));
    this.setNameLabel(new JLabel(nameText));
    this.setCourseLabel(new JLabel(nameText));
  }

  /**
   * Get jRadioButton.
   * 
   * @return jRadioButton
   */
  public JRadioButton getjRadioButton() {
    return this.jRadioButton;
  }

  /**
   * Set jRadioButton.
   * 
   * @param jRadioButton
   */
  public void setjRadioButton(JRadioButton jRadioButton) {
    this.jRadioButton = jRadioButton;
  }

  /**
   * Get studentIdLabel.
   * 
   * @return studentIdLabel
   */
  public JLabel getStudentIdLabel() {
    return this.studentIdLabel;
  }

  /**
   * Set studentIdLabel.
   * 
   * @param studentIdLabel
   */
  public void setStudentIdLabel(JLabel studentIdLabel) {
    this.studentIdLabel = studentIdLabel;
  }

  /**
   * Get nameLabel.
   * 
   * @return nameLabel
   */
  public JLabel getNameLabel() {
    return this.nameLabel;
  }

  /**
   * Set nameLabel.
   * 
   * @param nameLabel
   */
  public void setNameLabel(JLabel nameLabel) {
    this.nameLabel = nameLabel;
  }

  /**
   * Get courseLabel.
   * 
   * @return courseLabel
   */
  public JLabel getCourseLabel() {
    return this.courseLabel;
  }

  /**
   * Set courseLabel.
   * 
   * @param courseLabel
   */
  public void setCourseLabel(JLabel courseLabel) {
    this.courseLabel = courseLabel;
  }
}

这是一个简单的对象,用于在您的容器(可能是 JPanel,对吗?)中创建和组织行

当您解析结果集时,您需要构建此类对象的列表并在其中添加数据。

下面是一个简单的示例,说明如何解析和显示数据,您可以将其拆分为 2 个或更多方法并将它们放在适当的实体中:

int resultsetSize = resultset.getFetchSize();

//init the list 
List<GuiRow> guiRowList = new ArrayList<GuiRow>(resultsetSize);
while (resultset.next()) {
  //get the column values
  String id = resultset.getString("studentId");
  String name = resultset.getString("studentName");
  String course = resultset.getString("course");
  //create the the entry 
  GuiRow guiRow = new GuiRow(id, name, course);
  //add it to the list
  guiRowList.add(guiRow);
}

//now that you have a nice list of rows add them one by one to the container
JPanel container = new JPanel(new GridLayout(resultsetSize, 4));
//further container configuration could be done here ...

for (GuiRow row : guiRowList) {
  container.add(row.getjRadioButton());
  container.add(row.getStudentIdLabel());
  container.add(row.getNameLabel());
  container.add(row.getCourseLabel());
}