1

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
Since there can be any numbers of row in Database which will be fetched. I had successfullly Fetched the data from database but not able to set it on JRadioButton and JLabel

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

In above function in Variable s1,s2,s3 data exist which i want to Display and in Variable cnt there is value of counter which increment on resultset.next()

Can anyone give me some tips on how to display data on JRadiobutton and JLabel
JRadioButton Variale name is something like BT1,BT2,BT3... So i tried

Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
  • It's just as you said in your question title. Assuming the reference to your `JRadioButton` is `btn`, simply call `btn.setText(s1)`. `JLabel` does have a public `setText` method, and `JRadioButton` inherits it from `AbstractButton`. – ccjmne Oct 31 '16 at 04:59
  • Thanks for your suggestion but since in case there are more than 1 records fetched i want to display it in separate JRadioButton and JLabel so each time variable name will be different.For Example If there are 2 records fetched so data should display in BT1 and BT2 and so on Please see attached screenshot for more details – Kartik Agarwal Oct 31 '16 at 05:09

1 Answers1

2

Since the data is dynamic the GUI should be dynamic as well.

Consider the following example:

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;
  }
}

This is a simple Object to create and organize a row in your container (probably JPanel, right?)

When you are parsing the resultset you need to build a list of such objects and an add the data in there.

Here is a simple example on how to parse and display the data, which you can split into 2 or more methods and place them in the appropriate entities:

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());
}
Viorel Florian
  • 594
  • 9
  • 29