2016-10-31 5 views
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
フェッチされるデータベースには、任意の数の行が存在することがあります。 私はsuccessfullly変数S1、S2、S3データは、私が表示したい存在と変数CNT内に上記の機能ではJRadioButtonのテキストを動的に設定する

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

それを設定することができ、データベースからデータをフェッチされたが、いませんでした resultset.next()にインクリメントカウンタの値が存在する

誰も私に
のJRadioButton Variale名はBT1、BT2、BT3のようなものであるのJRadioButtonとJLabelの上のデータを表示する方法についていくつかのヒント...を与えることができるように試しました

+0

それはあなたがあなたの質問のタイトルで言っただけのようです。 'JRadioButton'への参照が' btn'であると仮定すると、単に 'btn.setText(s1)'を呼び出します。 'JLabel'はパブリック' setText'メソッドを持ち、 'JRadioButton'は' AbstractButton'からそれを継承します。 – ccjmne

+0

あなたの提案に感謝しますが、2つ以上のレコードがフェッチされているので、別のJRadioButtonとJLabelに表示したいので、変数名が異なっています。例レコードが2つ取り出されているので、 BT2など詳細は添付のスクリーンショットをご覧ください –

答えて

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()); 
} 
関連する問題