2016-10-31 12 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 변수에 위의 함수에서 데이터베이스의 데이터가 아니라 JRadioButton의 그것을 설정할 수 및 JLabel의JRadioButton의 텍스트를 동적으로 설정하십시오.

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

을 인출했다, S2, 데이터가 내가 표시 할 존재 변수 탄소 나노 튜브에 (S3) resultset.next()을 증가 카운터의 값이

사람이 나에게
JRadioButton의 Variale 이름 BT1, BT2, BT3 같은 것입니다 JRadioButton의 및 JLabel의 데이터를 표시하는 방법에 대한 몇 가지 팁 ...을 줄 수 있습니다 시도했습니다

+0

그것은 단지 당신이 당신의 질문 제목에서 말했듯이 : 여기

구문 분석하고 해당 기관에서 그들을 2 개 이상의 방법으로 분할하고 배치 할 수있는 데이터를 표시하는 방법에 대한 간단한 예입니다 . 'JRadioButton'에 대한 참조가'btn'이라면 간단히'btn.setText (s1)'을 호출하십시오. 'JLabel'은 public'setText' 메소드를 가지고 있고'JRadioButton'은'AbstractButton'으로부터 그것을 상속받습니다. – ccjmne

+0

당신의 제안을 가져 주셔서 감사하지만 이후에 하나 이상의 레코드가 페치 되었기 때문에 별도의 JRadioButton과 JLabel에 표시하고 싶을 때마다 변수 이름이 다를 수 있습니다. 예 레코드가 두 개 있으면 BT1에 데이터가 표시되고 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?) 만들고 용기에 행을 구성하는 간단한 객체이다

당신은 당신이 필요로하는 결과 집합을 파싱 그러한 객체들의 목록을 만들고 거기에 데이터를 추가하십시오.

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