Reputation: 776
I have a JTextPane in a very simple GUI which I use as output console for the game I'm using for learning Java, which I (try to) use with an append method in the window class, calling it from another class (the program itself) or from the command reader class. Theoretically it should output the command I've typed, and in the next lines its output, which comes from said program.
The print method (which uses append for JTextArea, or getText() and setText() for JTextPane) works fine if its called from its own class (when printing the used command and a "> ", however, when called from outside, it behaves randomly, sometimes appending on the very top of the textArea, working as intended, or even duplicating all the text inside of it.
This is the GUI code which is used for that. I'm sure its not perfect or even close to, but I've just started learning the basics of GUIs
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.DefaultCaret;
import graficos.VisorJugador;
public class test extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private String comando = "";
private JTextArea txp_Console;
private JScrollPane jsp_ConsoleScrollPanel;
private JLabel lbl_habitacion;
private VisorJugador[] jugadores;
/**
* Create the frame.
*/
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 480);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
createPanels();
}
public static void lanzar(test frame){
frame.setVisible(true);
}
private void createPanels(){
JPanel panelInferior = new JPanel();
JPanel panelCentral = new JPanel();
panelCentral.setLayout(new BorderLayout(0, 0));
crearInferior(panelInferior);
crearCentral(panelCentral);
contentPane.add(panelInferior, BorderLayout.SOUTH);
contentPane.add(panelCentral, BorderLayout.CENTER);
}
private void crearCentral(JPanel panelCentral) {
JLabel lbl_Consola = new JLabel("Consola");
txp_Console = new JTextArea();
jsp_ConsoleScrollPanel = new JScrollPane(txp_Console);
txp_Console.setEditable(true);
DefaultCaret caret = (DefaultCaret) txp_Console.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panelCentral.add(lbl_Consola, BorderLayout.NORTH);
panelCentral.add(txp_Console, BorderLayout.CENTER);
}
private void crearInferior(JPanel panelInferior) {
JLabel lbl_QueHacer = new JLabel("Que quieres hacer?");
JButton btn_Enviar = new JButton("Hacer");
JTextField txt_consola = new JTextField();
btn_Enviar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comando = txt_consola.getText();
print("> " + txt_consola.getText());
txt_consola.setText("");
}
});
txt_consola.setColumns(10);
panelInferior.add(lbl_QueHacer, BorderLayout.NORTH);
panelInferior.add(txt_consola);
panelInferior.add(btn_Enviar);
}
public void print(String texto) {
String anterior = txp_Console.getText();
txp_Console.setText(anterior + texto);
}
}
I call ventana.print(labels.getString(msg)) every time I have to output something from the program, ventana being a new test instance created at its constructor. I have no understanding of threading so I don't have anything handling it, so, as far as I know, everything is running on the main thread.
Also, here's some sample output
No entiendo --From commands 2 and 3
No entiendo --From commands 2 and 3
Bienvenido al Mundo de Zuul! --Working output
El Mundo de Zuul es un juego de aventuras muy aburrido, pero interesante!. --Working output
Escribe 'ayuda' si necesitas ayuda. --Working output
Estas en el exterior de la entrada principal de la universidad --Working output
Salidas: este, sur, oeste --Working output
> 1 --Works as intended
No entiendo --Output for command 1
> 2 --Output at the top
> 3 --Output at the top
> 4 --No output, windows error sound
While debugging I've seen that a thread for swing/awt is created, but I don't really understand threads at all, so I just hope I don't need them for this. I've also tried setting the quill, moving the scroll bar, removing "/n" from commands, but 0 luck yet.
Upvotes: 1
Views: 344
Reputation: 285450
Your use of threading is likely off, but we don't see the offending code, and so it is hard to tell. One thing I will state is that you should always start your GUI on the Swing event thread, and should strive to make changes to your GUI on this same thread.
One change you can make to the print method is to test if you're on the Swing event thread, and if so, append the text passed in to the JTextArea. If not, then create code using SwingUtilities to queue this change on the event thread, something like so:
public void print(String texto) {
// String anterior = txp_Console.getText();
// txp_Console.setText(anterior + texto);
if (SwingUtilities.isEventDispatchThread()) {
// if on the Swing event thread, call directly
txp_Console.append(texto); // a simple append call is all that is needed
} else {
// else queue it onto the event thread
SwingUtilities.invokeLater(() -> txp_Console.append(texto));
}
}
Do read Lesson: Concurrency in Swing to learn more about Swing threading.
For example, say I had a GUI like so:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class JuanjoTestPanel extends JPanel {
private static final int TA_ROWS = 25;
private static final int TA_COLS = 60;
private JTextArea textArea = new JTextArea(TA_ROWS, TA_COLS);
private JTextField textField = new JTextField(30);
private Action hacerAction = new HacerAction();
public JuanjoTestPanel() {
textArea.setFocusable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
textField.setAction(hacerAction);
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JLabel("¿Que queres hacer?"));
bottomPanel.add(Box.createHorizontalStrut(5));
bottomPanel.add(textField);
bottomPanel.add(new JButton(hacerAction));
setLayout(new BorderLayout());
add(scrollPane);
add(bottomPanel, BorderLayout.PAGE_END);
}
public void printToWindow(final String text) {
if (SwingUtilities.isEventDispatchThread()) {
textArea.append("Console:" + text + "\n");
} else {
SwingUtilities.invokeLater(() -> textArea.append("Console:" + text + "\n"));
}
}
private class HacerAction extends AbstractAction {
public HacerAction() {
super("Hacer");
putValue(MNEMONIC_KEY, KeyEvent.VK_H);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = "> " + textField.getText() + "\n";
textArea.append(text);
textField.selectAll();
textField.requestFocusInWindow();
}
}
}
You could also write to it from the console by calling its printToWindow(...)
method like so:
import java.util.Scanner;
import javax.swing.*;
public class TestSwing2 {
private static final String EXIT = "exit";
public static void main(String[] args) {
// create window
final JuanjoTestPanel testPanel = new JuanjoTestPanel();
// launch it on the Swing event thread
SwingUtilities.invokeLater(() -> createAndShowGui(testPanel));
Scanner scanner = new Scanner(System.in);
String line = "";
while (!line.trim().equalsIgnoreCase(EXIT)) {
System.out.print("Enter text: ");
line = scanner.nextLine();
System.out.println("Line entered: " + line);
testPanel.printToWindow(line); // write to it
}
scanner.close();
System.exit(0);
}
private static void createAndShowGui(JuanjoTestPanel testPanel) {
JFrame frame = new JFrame("Test Swing2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Upvotes: 1