Как говорится в вопросе, у меня возникают проблемы с обновлением текста JTextArea, который находится внутри JScrollPane.Как обновить текст JTextArea внутри JScrollPane?
Я могу захватить текст из typViewed getView() JScrollPane. Однако, я попробовал следующее обновить JTextArea.
((JTextArea)(chatWindow.getViewport().getView())).setText("Hello!");
где chatWindow - это JScrollPane.
и я попытался это:
chatWindowInsert.setText(processMessage());
где chatWindowInsert является JTextArea в JScrollPane
Ни работа, к сожалению.
Я не получаю никаких исключений или висит.
Помощь будет оценена!
Вот мой полный код. Пожалуйста, извините меня, если я сломал миллион программ.
public class ChatterBotClient extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private static ChatterBotFactory chatterBotFactory;
private static ChatterBotSession chatterBotSession;
private static ChatterBot chatterBot;
public static JScrollPane chatWindow;
public static JTextField userInput;
public static JTextArea chatWindowInsert;
public ChatterBotClient()
{
try{
chatterBotFactory = new ChatterBotFactory();
chatterBot = chatterBotFactory.create(ChatterBotType.CLEVERBOT);
chatterBotSession = chatterBot.createSession();
}catch(Exception e)
{
e.printStackTrace();
System.out.println("Error :O");
JOptionPane.showMessageDialog(null,
"There has been a problem initializing the Bot. Please restart.");
}
initUI();
}
public void initUI()
{
//Define the mainPanel that everything goes into in the JFrame
JPanel mainPanel = new JPanel(new BorderLayout());
//Define the child panels of "mainPanel"
JPanel infoPanel = new JPanel(new FlowLayout());
JPanel chatHistoryPanel = new JPanel(new FlowLayout());
JPanel userInputAndButtonPanel = new JPanel(new FlowLayout());
setTitle("ChatterBot Chat Client");
setSize(600,300);
setResizable(false);
//Define each component
//Set properties of each component
JLabel infoLabel = new JLabel("Welcome to my Cleverbot Client! Please enjoy! :D");
infoLabel.setPreferredSize(new Dimension(550, 20));
infoLabel.setHorizontalTextPosition(SwingConstants.CENTER);
chatWindowInsert = new JTextArea();
chatWindowInsert.setWrapStyleWord(true);
chatWindow = new JScrollPane(chatWindowInsert);
chatWindow.setPreferredSize(new Dimension(500,225));
chatWindow.setPreferredSize(chatWindow.getPreferredSize());
userInput = new JTextField();
userInput.setPreferredSize(new Dimension(250, 25));
JButton enterBtn = new JButton("Send");
enterBtn.setPreferredSize(new Dimension(75, 25));
//Add each component to the required panels
infoPanel.add(infoLabel);
chatHistoryPanel.add(chatWindow);
userInputAndButtonPanel.add(userInput, BorderLayout.CENTER);
userInputAndButtonPanel.add(enterBtn, BorderLayout.EAST);
//Add the child panels to the mainPanel
mainPanel.add(infoPanel, BorderLayout.NORTH);
mainPanel.add(chatHistoryPanel, BorderLayout.CENTER);
mainPanel.add(userInputAndButtonPanel, BorderLayout.SOUTH);
//Now, add the appropriate listeners to your components
enterBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("You clicked me! :D");
try {
processMessage();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Something went wrong when you pressed the button! :O");
}
}
});
userInput.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0)
{
chatWindow = new JScrollPane(new JTextArea(processMessage()));
}
});
//Tell the JFrame to display what we've made!
add(mainPanel);
}
public static String processMessage()
{
try {
String completeMessage = chatWindowInsert.getText();
completeMessage.concat("You: " + userInput.getText() + "\n");
String response = chatterBotSession.think(userInput.getText());
completeMessage.concat("Bot: " + response + "\n");
userInput.setText("");
return completeMessage;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "ERROR";
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
ChatterBotClient cli = new ChatterBotClient();
cli.setVisible(true);
}
});
}
}
Что значит «иметь проблемы с обновлением текста» и «что все еще не работает»? Любые исключения? Подвесной? отсутствует текст? – StanislavL
Приношу свои извинения. Я отредактировал вопрос, чтобы он был намного понятнее! Я бегу очень мало спать! – Pilapodapostache