Like putting a Jscrollpane in a Jtextarea?

Asked

Viewed 303 times

1

I’m trying to create a project where I print several sentences in one JTextArea, but for that, I need to include a JScrollPane. I looked at some examples on the internet but none is working.

public class Projeto extends JFrame {

    JPanel painel = new JPanel();
    public static JTextArea textarea = new JTextArea();
    public static JScrollPane sp = new JScrollPane();
    JButton cmd1;

public static void main(String[] args) {
   new Projeto();
}

public Projeto(){
    super("Árvore");
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    painel.setPreferredSize(new Dimension(600,400));
    painel.setLayout(null);

    cmd1 = new JButton("Start!");
    cmd1.setBounds(450, 50, 100, 30);
    painel.add(cmd1);

    textarea.setBounds(50, 50, 300, 300);
    textarea.setEditable(true);
    sp.add(textarea);

    painel.add(sp);       

    add(painel);
    pack();

    setVisible(true);

    event e = new event();
    cmd1.addActionListener(e);            
}

public static void atualiza(String frase){
    textarea.append(frase);
}   

public class event implements ActionListener{
    Pai p = new Pai();
    public void actionPerformed(ActionEvent e){

        p.start();                       
    }          
}   
}

1 answer

2


Define the JTextArea as portview of JScrollPane:

sp.setViewportView(textarea);

A JScrollPane is a flexible container, which adapts as a component added to it. But for this, this component needs to be a viewport of the scrollable panel. This is only possible by passing the component to the panel in its constructor when starting it, or by using the method setViewportView(). The method add does not work properly for this panel in specific.


A tip:

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the screen and makes your application look different depending on the monitor and resolution that is running.

There are several layouts so you don’t have to worry about positioning or manually organizing components. Not to mention that using layouts makes your code easier to maintain than inserting a lot of setbounds, and if you need to change the position of any component, you will have to position all manually.

In its code itself, by using absolute layout, the JTextArea It will never be displayed, because you set a size and position for it, but for the scrollpane there is nothing about its size and position. With the change below, the component is displayed normally:

sp.setBounds(50, 50, 300, 300);
textarea.setEditable(true);
sp.setViewportView(textarea);
  • The problem is that I use the.add(sp) panel; but Jscrollpane and Jtextarea are no longer appearing in the panel.

  • @Have you changed Gustavocruz as per the answer? This seems to be another different problem.

  • I changed yes and still have the same problem.

  • @Gustavocruz the problem has nothing to do with this, is that you are using absolute layout, which is a terrible practice. Search for layouts managers for swing, absolute layout only causes this kind of problem, especially if you don’t know exactly what you’re doing with it.

  • @Gustavocruz but if you want to continue using absolute layout, see the edition.

  • The problem has been solved. Thank you very much for helping Articuno.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.