• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem with JScrollPane

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am having a problem with a JScrollPane.
I am creating a JPanel and adding couple of JPanls with different Radio buttons.

JScrollpane is created with the JPanel. while displaying the Panel Vertical scrollBar is not starting from 0. I mean to say that the content is not displaying from most top and left. insted it is showing from the middle. every time i need to scroll up to see from strating. I will be thankfull if anybody suggest the right solution
Hari
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i am not sure what cause this happened, i think, it may be because of that the radiobutton which is always getting focus is just in the middle of JScrollpane. so that, the JScrollpane automatically scrolls to the middle, just guess.

----------
Create your Java GUI application in minutes, http://www.mars3000.com
[ August 29, 2004: Message edited by: Pat Hays ]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Hari Babburi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot fro giving info. But it is still not working
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code.

Works OK in this simple demo

 
Hari Babburi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is some problem it seems. I tried through debugging.
Even I set to value to zero. it is still having some events in it and making to move the scroll. I am pasting the code here. refresh method will be called from other class to refresh this

/*
* Created on Jul 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ViewportUI;

import Answer;
import Question;


public class CopyOfJMainPane extends JScrollPane {

public static final Integer NO_ANSWER = new Integer(0);
boolean isMultipleAnswer = false;
JEditorPane groupText = new JEditorPane();
JLabel groupCanvas = new JLabel();

JEditorPane questionText = new JEditorPane();
JLabel questionCanvas = new JLabel();

JCheckBox choiceACheck = new JCheckBox();
JCheckBox choiceBCheck = new JCheckBox();
JCheckBox choiceCCheck = new JCheckBox();
JCheckBox choiceDCheck = new JCheckBox();
JCheckBox choiceECheck = new JCheckBox();

ButtonGroup group = new ButtonGroup();
JRadioButton choiceARadio = new JRadioButton();
JRadioButton choiceBRadio = new JRadioButton();
JRadioButton choiceCRadio = new JRadioButton();
JRadioButton choiceDRadio = new JRadioButton();
JRadioButton choiceERadio = new JRadioButton();

JEditorPane choiceA = new JEditorPane();
JEditorPane choiceB = new JEditorPane();
JEditorPane choiceC = new JEditorPane();
JEditorPane choiceD = new JEditorPane();
JEditorPane choiceE = new JEditorPane();

JLabel choiceACanvas = new JLabel();
JLabel choiceBCanvas = new JLabel();
JLabel choiceCCanvas = new JLabel();
JLabel choiceDCanvas = new JLabel();
JLabel choiceECanvas = new JLabel();

JPanel choiceAPanel = new JPanel();
JPanel choiceBPanel = new JPanel();
JPanel choiceCPanel = new JPanel();
JPanel choiceDPanel = new JPanel();
JPanel choiceEPanel = new JPanel();

JPanel mainPanel = new JPanel();

public CopyOfJMainPane(){
mainPanel.setLayout(new BorderLayout());
StringBuffer buffer = new StringBuffer("Welcome to Test\n");
buffer.append("This is test Exam");
groupText.setContentType("text/html");
questionText.setContentType("text/html");
choiceA.setContentType("text/html");
choiceB.setContentType("text/html");
choiceC.setContentType("text/html");
choiceD.setContentType("text/html");
choiceE.setContentType("text/html");

groupText.setEditable(false);
questionText.setEditable(false);
choiceA.setEditable(false);
choiceB.setEditable(false);
choiceC.setEditable(false);
choiceD.setEditable(false);
choiceE.setEditable(false);

mainPanel.setBackground(Color.white);
mainPanel.setForeground(Color.white);
choiceACheck.setBackground(Color.white);
choiceBCheck.setBackground(Color.white);
choiceCCheck.setBackground(Color.white);
choiceDCheck.setBackground(Color.white);
choiceECheck.setBackground(Color.white);

choiceARadio.setBackground(Color.white);
choiceBRadio.setBackground(Color.white);
choiceCRadio.setBackground(Color.white);
choiceDRadio.setBackground(Color.white);
choiceERadio.setBackground(Color.white);

choiceACanvas.setBackground(Color.white);
choiceBCanvas.setBackground(Color.white);
choiceCCanvas.setBackground(Color.white);
choiceDCanvas.setBackground(Color.white);
choiceECanvas.setBackground(Color.white);
groupCanvas.setBackground(Color.white);
questionCanvas.setBackground(Color.white);

choiceAPanel.setBackground(Color.white);
choiceBPanel.setBackground(Color.white);
choiceCPanel.setBackground(Color.white);
choiceDPanel.setBackground(Color.white);
choiceEPanel.setBackground(Color.white);

FlowLayout layout = (FlowLayout)choiceAPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceBPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceCPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceDPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceEPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);

group.add(choiceARadio);
group.add(choiceBRadio);
group.add(choiceCRadio);
group.add(choiceDRadio);
group.add(choiceERadio);

groupText.setText(buffer.toString());
mainPanel.add(groupText, BorderLayout.CENTER);
setViewportView(mainPanel);
//this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//this.setHorizontalScrollBarPolicy(
//JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.setPreferredSize(new Dimension(500, 400));
}

public Integer getAnswer(){
if(isMultipleAnswer) {
int a = 0; if(choiceACheck.isSelected()) a = 1;
int b = 0; if(choiceBCheck.isSelected()) b = 2;
int c = 0; if(choiceCCheck.isSelected()) c = 4;
int d = 0; if(choiceDCheck.isSelected()) d = 8;
int e = 0; if(choiceECheck.isSelected()) e = 16;
return new Integer(a|b|c|d|e);
}
else {
if(choiceARadio.isSelected()) return new Integer(1);
else if(choiceBRadio.isSelected()) return new Integer(2);
else if(choiceCRadio.isSelected()) return new Integer(4);
else if(choiceDRadio.isSelected()) return new Integer(8);
else if(choiceERadio.isSelected()) return new Integer(16);
}
return new Integer(0);
}

public void setAnswer(Integer ans){
if(ans == null) return;
int answer = ans.intValue();
if(isMultipleAnswer) {
if((answer & 1) == Answer.A) choiceACheck.setSelected(true);
if((answer & 2) == Answer.B) choiceBCheck.setSelected(true);
if((answer & 4) == Answer.C) choiceCCheck.setSelected(true);
if((answer & 8) == Answer.D) choiceDCheck.setSelected(true);
if((answer & 16) == Answer.E) choiceECheck.setSelected(true);
}
else {
if(answer == Answer.A) choiceARadio.setSelected(true);
if(answer == Answer.B) choiceBRadio.setSelected(true);
if(answer == Answer.C) choiceCRadio.setSelected(true);
if(answer == Answer.D) choiceDRadio.setSelected(true);
if(answer == Answer.E) choiceERadio.setSelected(true);
}

}

public void refreshQuestion(Question question){
refresh(question);
this.revalidate();
System.out.println(this.getBounds());
//this.setViewportView(questionText);
//this.getBounds().y=0;
this.repaint();
this.getVerticalScrollBar().setValue(0);
//this.getVerticalScrollBar().add
//this.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){

//public void adjustmentValueChanged(AdjustmentEvent arg0) {
//System.out.println(arg0.getSource());
//System.out.println("Value : " + arg0.getValue());
////super.getVerticalScrollBar().setValue(0);
//}});
System.out.println("value amount " + this.getVerticalScrollBar().getValue());

}

/**
* Refreshing view for current question
*/
private void refresh(Question question) {
mainPanel.removeAll();
Answer ans = Answer.getAnswer(question.getAnswer());
isMultipleAnswer = ans.isMultipleAnswer();
if(question.isAlreadyAsked()) setAnswer(question.getUserAnswer());
int rows = question.getNoOfRowsNeededToDisplay();
mainPanel.setLayout(new GridLayout(rows, 1));
if(question.getGroup() != null){
if(question.getGroup().getGroupText() !=null && question.getGroup().getGroupText().trim().length() > 0) {
mainPanel.add(groupText);
groupText.setText(applyHtml(question.getGroup().getGroupText()));
}
if(question.getGroup().getGroupImage() !=null) {
mainPanel.add(groupCanvas);
groupCanvas.setIcon(new ImageIcon(question.getGroup().getGroupImage()));
}
}
if(question.getQuestionText() != null && question.getQuestionText().trim().length()>0) {
mainPanel.add(questionText);
questionText.setText(applyHtml(question.getQuestionText()));
questionText.setPreferredSize(new Dimension(500, 50));
}
if(question.getQuestionImage() != null) {
mainPanel.add(questionCanvas);
questionCanvas.setIcon(new ImageIcon(question.getQuestionImage()));
}
if(question.getChoiceAText() != null && question.getChoiceAText().trim().length()>0) {
choiceAPanel.removeAll();
if(isMultipleAnswer) {
choiceAPanel.add(choiceACheck);
}
else {
choiceAPanel.add(choiceARadio);
}
choiceAPanel.add(choiceA);
choiceA.setText(applyHtml(question.getChoiceAText()));
mainPanel.add(choiceAPanel);
}
if(question.getChoiceAImage() != null) {
mainPanel.add(choiceACanvas);
choiceACanvas.setIcon(new ImageIcon(question.getChoiceAImage()));
}
if(question.getChoiceBText() != null && question.getChoiceBText().trim().length()>0) {
choiceBPanel.removeAll();
if(isMultipleAnswer) {
choiceBPanel.add(choiceBCheck);
}
else {
choiceBPanel.add(choiceBRadio);
}
choiceBPanel.add(choiceB);
choiceB.setText(applyHtml(question.getChoiceBText()));
mainPanel.add(choiceBPanel);
}
if(question.getChoiceBImage() != null) {
mainPanel.add(choiceBCanvas);
choiceBCanvas.setIcon(new ImageIcon(question.getChoiceBImage()));
}
if(question.getChoiceCText() != null && question.getChoiceCText().trim().length()>0) {
choiceCPanel.removeAll();
if(isMultipleAnswer) {
choiceCPanel.add(choiceCCheck);
}
else {
choiceCPanel.add(choiceCRadio);
}
choiceCPanel.add(choiceC);
choiceC.setText(applyHtml(question.getChoiceCText()));
mainPanel.add(choiceCPanel);
}
if(question.getChoiceCImage() != null) {
mainPanel.add(choiceCCanvas);
choiceCCanvas.setIcon(new ImageIcon(question.getChoiceCImage()));
}
if(question.getChoiceDText() != null && question.getChoiceDText().trim().length()>0) {
choiceDPanel.removeAll();
if(isMultipleAnswer) {
choiceDPanel.add(choiceDCheck);
}
else {
choiceDPanel.add(choiceDRadio);
}
choiceDPanel.add(choiceD);
choiceD.setText(applyHtml(question.getChoiceDText()));
//choiceD.setPreferredSize(new Dimension(500, 50)); removed hari
mainPanel.add(choiceDPanel);
}
if(question.getChoiceDImage() != null) {
mainPanel.add(choiceDCanvas);
choiceDCanvas.setIcon(new ImageIcon(question.getChoiceDImage()));
}
if(question.getChoiceEText() != null && question.getChoiceEText().trim().length()>0) {
choiceEPanel.removeAll();
if(isMultipleAnswer) {
choiceEPanel.add(choiceECheck);
}
else {
choiceEPanel.add(choiceERadio);
}
choiceEPanel.add(choiceE);
choiceE.setText(applyHtml(question.getChoiceEText()));
mainPanel.add(choiceEPanel);
}
if(question.getChoiceEImage() != null) {
mainPanel.add(choiceECanvas);
choiceECanvas.setIcon(new ImageIcon(question.getChoiceEImage()));
}
question.setAlreadyAsked(true);
//Rectangle r = this.getBounds();
//r.height = 1;
//this.scrollRectToVisible(r);
//this.scrollRectToVisible(r);
//this.validate();
//mainPanel.revalidate();
initChoices(question);
}

private void initChoices(Question question){
choiceACheck.setSelected(false);
choiceBCheck.setSelected(false);
choiceCCheck.setSelected(false);
choiceDCheck.setSelected(false);
choiceECheck.setSelected(false);

choiceARadio.setSelected(false);
choiceBRadio.setSelected(false);
choiceCRadio.setSelected(false);
choiceDRadio.setSelected(false);
choiceERadio.setSelected(false);

if(question.getUserAnswer() != null && !question.getUserAnswer().equals(NO_ANSWER)) {
this.setAnswer(question.getUserAnswer());
}
}

private String applyHtml(String input){
StringBuffer buffer = new StringBuffer();
buffer.append("<html><head><title>Exam</title></head>");
buffer.append("<body>");
buffer.append(input);
buffer.append("</body></html>");
return buffer.toString();
}
}
 
Hari Babburi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is some problem it seems. I tried through debugging.
Even I set to value to zero. it is still having some events in it and making to move the scroll. I am pasting the code here. refresh method will be called from other class to refresh this

/*
* Created on Jul 24, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ViewportUI;

import Answer;
import Question;


public class CopyOfJMainPane extends JScrollPane {

public static final Integer NO_ANSWER = new Integer(0);
boolean isMultipleAnswer = false;
JEditorPane groupText = new JEditorPane();
JLabel groupCanvas = new JLabel();

JEditorPane questionText = new JEditorPane();
JLabel questionCanvas = new JLabel();

JCheckBox choiceACheck = new JCheckBox();
JCheckBox choiceBCheck = new JCheckBox();
JCheckBox choiceCCheck = new JCheckBox();
JCheckBox choiceDCheck = new JCheckBox();
JCheckBox choiceECheck = new JCheckBox();

ButtonGroup group = new ButtonGroup();
JRadioButton choiceARadio = new JRadioButton();
JRadioButton choiceBRadio = new JRadioButton();
JRadioButton choiceCRadio = new JRadioButton();
JRadioButton choiceDRadio = new JRadioButton();
JRadioButton choiceERadio = new JRadioButton();

JEditorPane choiceA = new JEditorPane();
JEditorPane choiceB = new JEditorPane();
JEditorPane choiceC = new JEditorPane();
JEditorPane choiceD = new JEditorPane();
JEditorPane choiceE = new JEditorPane();

JLabel choiceACanvas = new JLabel();
JLabel choiceBCanvas = new JLabel();
JLabel choiceCCanvas = new JLabel();
JLabel choiceDCanvas = new JLabel();
JLabel choiceECanvas = new JLabel();

JPanel choiceAPanel = new JPanel();
JPanel choiceBPanel = new JPanel();
JPanel choiceCPanel = new JPanel();
JPanel choiceDPanel = new JPanel();
JPanel choiceEPanel = new JPanel();

JPanel mainPanel = new JPanel();

public CopyOfJMainPane(){
mainPanel.setLayout(new BorderLayout());
StringBuffer buffer = new StringBuffer("Welcome to Test\n");
buffer.append("This is test Exam");
groupText.setContentType("text/html");
questionText.setContentType("text/html");
choiceA.setContentType("text/html");
choiceB.setContentType("text/html");
choiceC.setContentType("text/html");
choiceD.setContentType("text/html");
choiceE.setContentType("text/html");

groupText.setEditable(false);
questionText.setEditable(false);
choiceA.setEditable(false);
choiceB.setEditable(false);
choiceC.setEditable(false);
choiceD.setEditable(false);
choiceE.setEditable(false);

mainPanel.setBackground(Color.white);
mainPanel.setForeground(Color.white);
choiceACheck.setBackground(Color.white);
choiceBCheck.setBackground(Color.white);
choiceCCheck.setBackground(Color.white);
choiceDCheck.setBackground(Color.white);
choiceECheck.setBackground(Color.white);

choiceARadio.setBackground(Color.white);
choiceBRadio.setBackground(Color.white);
choiceCRadio.setBackground(Color.white);
choiceDRadio.setBackground(Color.white);
choiceERadio.setBackground(Color.white);

choiceACanvas.setBackground(Color.white);
choiceBCanvas.setBackground(Color.white);
choiceCCanvas.setBackground(Color.white);
choiceDCanvas.setBackground(Color.white);
choiceECanvas.setBackground(Color.white);
groupCanvas.setBackground(Color.white);
questionCanvas.setBackground(Color.white);

choiceAPanel.setBackground(Color.white);
choiceBPanel.setBackground(Color.white);
choiceCPanel.setBackground(Color.white);
choiceDPanel.setBackground(Color.white);
choiceEPanel.setBackground(Color.white);

FlowLayout layout = (FlowLayout)choiceAPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceBPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceCPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceDPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
layout = (FlowLayout)choiceEPanel.getLayout();
layout.setAlignment(FlowLayout.LEFT);

group.add(choiceARadio);
group.add(choiceBRadio);
group.add(choiceCRadio);
group.add(choiceDRadio);
group.add(choiceERadio);

groupText.setText(buffer.toString());
mainPanel.add(groupText, BorderLayout.CENTER);
setViewportView(mainPanel);
//this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//this.setHorizontalScrollBarPolicy(
//JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.setPreferredSize(new Dimension(500, 400));
}

public Integer getAnswer(){
if(isMultipleAnswer) {
int a = 0; if(choiceACheck.isSelected()) a = 1;
int b = 0; if(choiceBCheck.isSelected()) b = 2;
int c = 0; if(choiceCCheck.isSelected()) c = 4;
int d = 0; if(choiceDCheck.isSelected()) d = 8;
int e = 0; if(choiceECheck.isSelected()) e = 16;
return new Integer(a|b|c|d|e);
}
else {
if(choiceARadio.isSelected()) return new Integer(1);
else if(choiceBRadio.isSelected()) return new Integer(2);
else if(choiceCRadio.isSelected()) return new Integer(4);
else if(choiceDRadio.isSelected()) return new Integer(8);
else if(choiceERadio.isSelected()) return new Integer(16);
}
return new Integer(0);
}

public void setAnswer(Integer ans){
if(ans == null) return;
int answer = ans.intValue();
if(isMultipleAnswer) {
if((answer & 1) == Answer.A) choiceACheck.setSelected(true);
if((answer & 2) == Answer.B) choiceBCheck.setSelected(true);
if((answer & 4) == Answer.C) choiceCCheck.setSelected(true);
if((answer & 8) == Answer.D) choiceDCheck.setSelected(true);
if((answer & 16) == Answer.E) choiceECheck.setSelected(true);
}
else {
if(answer == Answer.A) choiceARadio.setSelected(true);
if(answer == Answer.B) choiceBRadio.setSelected(true);
if(answer == Answer.C) choiceCRadio.setSelected(true);
if(answer == Answer.D) choiceDRadio.setSelected(true);
if(answer == Answer.E) choiceERadio.setSelected(true);
}

}

public void refreshQuestion(Question question){
refresh(question);
this.revalidate();
System.out.println(this.getBounds());
//this.setViewportView(questionText);
//this.getBounds().y=0;
this.repaint();
this.getVerticalScrollBar().setValue(0);
//this.getVerticalScrollBar().add
//this.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){

//public void adjustmentValueChanged(AdjustmentEvent arg0) {
//System.out.println(arg0.getSource());
//System.out.println("Value : " + arg0.getValue());
////super.getVerticalScrollBar().setValue(0);
//}});
System.out.println("value amount " + this.getVerticalScrollBar().getValue());

}

/**
* Refreshing view for current question
*/
private void refresh(Question question) {
mainPanel.removeAll();
Answer ans = Answer.getAnswer(question.getAnswer());
isMultipleAnswer = ans.isMultipleAnswer();
if(question.isAlreadyAsked()) setAnswer(question.getUserAnswer());
int rows = question.getNoOfRowsNeededToDisplay();
mainPanel.setLayout(new GridLayout(rows, 1));
if(question.getGroup() != null){
if(question.getGroup().getGroupText() !=null && question.getGroup().getGroupText().trim().length() > 0) {
mainPanel.add(groupText);
groupText.setText(applyHtml(question.getGroup().getGroupText()));
}
if(question.getGroup().getGroupImage() !=null) {
mainPanel.add(groupCanvas);
groupCanvas.setIcon(new ImageIcon(question.getGroup().getGroupImage()));
}
}
if(question.getQuestionText() != null && question.getQuestionText().trim().length()>0) {
mainPanel.add(questionText);
questionText.setText(applyHtml(question.getQuestionText()));
questionText.setPreferredSize(new Dimension(500, 50));
}
if(question.getQuestionImage() != null) {
mainPanel.add(questionCanvas);
questionCanvas.setIcon(new ImageIcon(question.getQuestionImage()));
}
if(question.getChoiceAText() != null && question.getChoiceAText().trim().length()>0) {
choiceAPanel.removeAll();
if(isMultipleAnswer) {
choiceAPanel.add(choiceACheck);
}
else {
choiceAPanel.add(choiceARadio);
}
choiceAPanel.add(choiceA);
choiceA.setText(applyHtml(question.getChoiceAText()));
mainPanel.add(choiceAPanel);
}
if(question.getChoiceAImage() != null) {
mainPanel.add(choiceACanvas);
choiceACanvas.setIcon(new ImageIcon(question.getChoiceAImage()));
}
if(question.getChoiceBText() != null && question.getChoiceBText().trim().length()>0) {
choiceBPanel.removeAll();
if(isMultipleAnswer) {
choiceBPanel.add(choiceBCheck);
}
else {
choiceBPanel.add(choiceBRadio);
}
choiceBPanel.add(choiceB);
choiceB.setText(applyHtml(question.getChoiceBText()));
mainPanel.add(choiceBPanel);
}
if(question.getChoiceBImage() != null) {
mainPanel.add(choiceBCanvas);
choiceBCanvas.setIcon(new ImageIcon(question.getChoiceBImage()));
}
if(question.getChoiceCText() != null && question.getChoiceCText().trim().length()>0) {
choiceCPanel.removeAll();
if(isMultipleAnswer) {
choiceCPanel.add(choiceCCheck);
}
else {
choiceCPanel.add(choiceCRadio);
}
choiceCPanel.add(choiceC);
choiceC.setText(applyHtml(question.getChoiceCText()));
mainPanel.add(choiceCPanel);
}
if(question.getChoiceCImage() != null) {
mainPanel.add(choiceCCanvas);
choiceCCanvas.setIcon(new ImageIcon(question.getChoiceCImage()));
}
if(question.getChoiceDText() != null && question.getChoiceDText().trim().length()>0) {
choiceDPanel.removeAll();
if(isMultipleAnswer) {
choiceDPanel.add(choiceDCheck);
}
else {
choiceDPanel.add(choiceDRadio);
}
choiceDPanel.add(choiceD);
choiceD.setText(applyHtml(question.getChoiceDText()));
//choiceD.setPreferredSize(new Dimension(500, 50)); removed hari
mainPanel.add(choiceDPanel);
}
if(question.getChoiceDImage() != null) {
mainPanel.add(choiceDCanvas);
choiceDCanvas.setIcon(new ImageIcon(question.getChoiceDImage()));
}
if(question.getChoiceEText() != null && question.getChoiceEText().trim().length()>0) {
choiceEPanel.removeAll();
if(isMultipleAnswer) {
choiceEPanel.add(choiceECheck);
}
else {
choiceEPanel.add(choiceERadio);
}
choiceEPanel.add(choiceE);
choiceE.setText(applyHtml(question.getChoiceEText()));
mainPanel.add(choiceEPanel);
}
if(question.getChoiceEImage() != null) {
mainPanel.add(choiceECanvas);
choiceECanvas.setIcon(new ImageIcon(question.getChoiceEImage()));
}
question.setAlreadyAsked(true);
//Rectangle r = this.getBounds();
//r.height = 1;
//this.scrollRectToVisible(r);
//this.scrollRectToVisible(r);
//this.validate();
//mainPanel.revalidate();
initChoices(question);
}

private void initChoices(Question question){
choiceACheck.setSelected(false);
choiceBCheck.setSelected(false);
choiceCCheck.setSelected(false);
choiceDCheck.setSelected(false);
choiceECheck.setSelected(false);

choiceARadio.setSelected(false);
choiceBRadio.setSelected(false);
choiceCRadio.setSelected(false);
choiceDRadio.setSelected(false);
choiceERadio.setSelected(false);

if(question.getUserAnswer() != null && !question.getUserAnswer().equals(NO_ANSWER)) {
this.setAnswer(question.getUserAnswer());
}
}

private String applyHtml(String input){
StringBuffer buffer = new StringBuffer();
buffer.append("<html><head><title>Exam</title></head>");
buffer.append("<body>");
buffer.append(input);
buffer.append("</body></html>");
return buffer.toString();
}
}
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic