JTextArea Cursor no inicio do texto

1 resposta
matheusssilva

Estou exibindo uma janela com um texto pre-escrito dentro de um jtextarea.
Como faço para que quando a janela seja aberta o JTextArea exiba o inicio do texto e não o fim como está acontecendo? Tentei com setCaretPosition mas não funciona. Ele não será editado, servirá apenas para exibir um texto informativo, quero que ele se posicione no inico do texto e não o fim, assim fica a cargo do usuário ir rolando a barra para ir visualizando o resto do texto.

package br.com.folhax.gui;

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class JInternalFrameSobre extends javax.swing.JInternalFrame {

    private static JInternalFrameSobre jifsobre;

    public static JInternalFrameSobre getJInternalFrameSobre(){
        if(jifsobre == null)
            jifsobre = new JInternalFrameSobre();
        return jifsobre;
    }

    public JInternalFrameSobre() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jpsobre = new JPanel(){
            @Override
            public void paintComponent(Graphics g){
                ImageIcon logo = new ImageIcon(getClass().getResource("/br/com/folhax/imagens/FolhaXSplashLogin.png"));
                g.drawImage(logo.getImage(),0,0,null);
                super.paintComponent(g);
            }
        };
        jbok = new javax.swing.JButton();
        jspinformacoes = new javax.swing.JScrollPane();
        jtainformacoes = new javax.swing.JTextArea();

        setIconifiable(true);
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/folhax/imagens/logosobre.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jpsobre.setEnabled(false);
        jpsobre.setOpaque(false);

        jbok.setText("OK");
        jbok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbokActionPerformed(evt);
            }
        });

        jspinformacoes.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jspinformacoes.setFocusable(false);
        jspinformacoes.setRequestFocusEnabled(false);

        jtainformacoes.setColumns(20);
        jtainformacoes.setEditable(false);
        jtainformacoes.setRows(5);
        jtainformacoes.setText("Folha de pagamento é o nome dado a uma lista mensal da remuneração paga aos trabalhadores de uma instituição. Toda empresa no Brasil tem a obrigação legal de prepará-la, contendo: o nome dos funcionários, indicando o cargo; a divisão dos funcionários por categoria de contribuição à previdência: segurado empregado, trabalhador avulso ou contribuinte individual; o nome das funcionárias em gozo de salário-maternidade; as partes integrantes e não-integrantes da");
        jtainformacoes.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jtainformacoes.setFocusCycleRoot(true);
        jtainformacoes.setNextFocusableComponent(jbok);
        jspinformacoes.setViewportView(jtainformacoes);

        javax.swing.GroupLayout jpsobreLayout = new javax.swing.GroupLayout(jpsobre);
        jpsobre.setLayout(jpsobreLayout);
        jpsobreLayout.setHorizontalGroup(
            jpsobreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpsobreLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpsobreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbok, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jspinformacoes, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(183, Short.MAX_VALUE))
        );
        jpsobreLayout.setVerticalGroup(
            jpsobreLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpsobreLayout.createSequentialGroup()
                .addContainerGap(173, Short.MAX_VALUE)
                .addComponent(jspinformacoes, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbok, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpsobre, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpsobre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-500)/2, (screenSize.height-374)/2, 500, 374);
    }// </editor-fold>

    private void jbokActionPerformed(java.awt.event.ActionEvent evt) {                                     
        dispose();
    }                                    

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {                                         
       jifsobre = null;
    }                                        

    private javax.swing.JButton jbok;
    private javax.swing.JPanel jpsobre;
    private javax.swing.JScrollPane jspinformacoes;
    private javax.swing.JTextArea jtainformacoes;
}

Valeu.

1 Resposta

matheusssilva

Reolvido

Fui na propriedade caretPosition do componente no netbeans e escolhi setar código personalizado.
Assim funcionou… Axo que é bronca com netbeans pois só funcionou assim. Setando o valor direto na proprieade não funcionou.

Criado 30 de junho de 2010
Ultima resposta 30 de jun. de 2010
Respostas 1
Participantes 1