I Want to have a JTextArea and a JComboBox, that has some text, and after a while, it changes the text, but for some reason, it's not changing the text for the JTextArea, nor the JComboBox.
Here's the code which is supposed to change the JTextArea and the JComboBox:
//If it's question 1 for the JComboBox
if (questionNum == 1) {
//Choices
choice [0] = "Choice1";
choice [1] = "Choice2";
choice [2] = "Choice3";
choice [3] = "Choice4";
//If it's Question 2 for the JComboBox
} else if (questionNum == 2) {
//Choices
choice [0] = "Choice1";
choice [1] = "Choice2";
choice [2] = "Choice3";
choice [3] = "Choice4";
}
//JTextArea - Questions
JTextArea Question = new JTextArea(" ");
Question.setBounds(w/2 - 100, 150, 200, 60);
Question.setLineWrap(true);
Question.setWrapStyleWord(true);
Question.setEditable(false);
panel.add(Question);
//If it's question 1
if (questionNum == 1) {
Question.setText("Question1");
} else if(questionNum == 2) {
Question.setText("Question2");
}
//JCombox - Answer Choices
answer = new JComboBox(choice);
answer.setBounds(225, 230, 200, 25);
panel.add(answer);
answer.setVisible(true);
start.addActionListener(new Window());
What I Tried: I tried to replace the JTextArea with variables, and then tried the Update method, then tried to use the selectAll and replace method, but nothing worked.
I have some more code that changes the question number, in another method.
Introduction
Since your code was not a minimal runnable example, I went ahead and created the following GUI.
Because this is an example, I only created two answers for each question and I only went two questions deep.
Explanation
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
When I create a Swing GUI, I use the model-view-controller (MVC) pattern. This allows me to separate my concerns and focus on one part of the application at a time.
The model is the major part of this example application. The view and controller are simple because the model is complex.
The model is completely independent of the view and the controller. The view reads a question and answers from the model. The controller gets the next question and answers for the view.
Model
The model consists of four plain Java getter/setter classes.
The first two classes are the
ExternalQuestionand theInternalQuestionclasses.The
ExternalQuestionclass stores aStringprevious answer, aStringquestion, and aStringarray of answers. The answer array can be any length. I provided two answers to each question to keep the example short.Here's the code to load external questions.
The first question has a
nullprevious answer. The following questions must have an exact matching previous answer from the prior question. You can create a complex tree with many questions and answers.This method could be modified to read a text file and create a question tree.
The
InternalQuestionclass stores anintprevious answer index, aStringquestion, and anintarray of answer indexes. The answers are kept in aListand the indexes to those answers are stored. This reduces the amount of memory needed for the question tree structure.The
QuestionsModelbuilds the questions tree using aListfor theStringanswers and aMapfor theInternalQuestioninstances. This allows an easy transversal through the questions, depending on which answer the user selects.The
AdventureModelstores an instance of theQuestionsModel. Any information needed for a more complex text adventure would be stored here.View
I created a
JFrameand aJPanel. TheJPaneluses aGridBagLayoutto lay out the Swing components from top to bottom, making each Swing component the same width.The
JFramehas a defaultBorderLayout. I placed theJPanelin the center of theBorderLayout.Controller
I used a lambda expression to create an anonymous
ActionListenerfor theJButton. The expression gets the nextExternalQuestioninstance and updates theJTextAreaand theJComboBoxwith the question and answers.Code
Here's the complete runnable code. I made all the additional classes inner classes so I could post the code as one block.