a me funziona alla perfezione:
codice:
public class JScrollText extends JFrame {
private JTextArea console;
private JButton b;
/** Creates a new instance of JScrollText */
public JScrollText() {
super("This is a test");
this.setSize(400,300);
this.getContentPane().setLayout(new BorderLayout());
b = new JButton("Add Text");
console = new JTextArea();
console.setBackground(Color.BLACK);
console.setForeground(Color.WHITE);
console.setEditable(false);
console.setFont(new Font("Courier", Font.PLAIN, 10));
JScrollPane consolepanel = new JScrollPane(console, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
consolepanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "Console", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP, new Font("Verdana", Font.PLAIN, 12)));
consolepanel.setOpaque(false);
this.getContentPane().add(consolepanel, BorderLayout.CENTER);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
console.append("Ho aggiunto una riga\n");
//console.setCaretPosition(0);
}
});
this.getContentPane().add(b, BorderLayout.NORTH);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
new JScrollText();
}
}