JRadioButton上的setText动态

时间:2016-10-31 04:55:00

标签: java variables dynamic jlabel jradiobutton

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 ......所以我试过

1 个答案:

答案 0 :(得分:1)

由于数据是动态的,因此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());
}