Witam! Mam prosty programik wyświetlający pole tekstowe, które jest scrollowalne - JScrollPane + JTextArea. Niestety przy wpisywaniu czegokolwiek z klawiatury, efekt jest taki jak na screenie Oprócz tego pierwotnie wpisany tekst to: abcde a jak widać na screenie wyświetlony jest wspak i do tego wyświetla się po każdym enterze, mimo że został wpisany tylko raz.
Co jest nie tak?
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.text.DefaultCaret;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
public class app {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
app window = new app();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public app() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.blue);
JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
textArea.addInputMethodListener(new InputMethodListener() {
public void caretPositionChanged(InputMethodEvent arg0) {
}
public void inputMethodTextChanged(InputMethodEvent arg0) {
//textArea.revalidate();
//textArea.repaint();
//textArea.update(textArea.getGraphics());
textArea.updateUI();
}
});
//frame.getContentPane().add(textArea, BorderLayout.CENTER);
textArea.setBackground(new Color(0,0,0,0));
JScrollPane scrollPane = new JScrollPane(textArea);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
//scrollPane.setViewportView(textArea);
//scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
scrollPane.setBackground(new Color(0,0,0,0));
scrollPane.setDoubleBuffered(true);
textArea.setDoubleBuffered(true);
}
}