0

First time writing a GUI in Java

This is it so far, when I click "Coordinate Anomalies" an arraylist of strings is shown in the JTextArea, but the only way to see the text is by highlighting it.

I also tried adding a scroll bar to the JTextArea, but have had no luck.

JTextArea textArea = new JTextArea();
textArea.setBounds(10, 79, 172, 339);
frame.getContentPane().add(textArea);

JButton btnNewButton_1 = new JButton("Coordinate Anomalies");

btnNewButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> anomalies = vessels.coordinateAnomaly();
        JScrollPane jp = new JScrollPane();
        jp.setViewportView(textArea);

        for (String a : anomalies) {
            textArea.append(a + "\n");
        }

        textArea.setBounds(10, 79, 172, 339);
        frame.getContentPane().add(textArea);
    }
});

btnNewButton_1.setBounds(10, 45, 172, 23);
frame.getContentPane().add(btnNewButton_1);

This is my code (sorry for jank), can't stress enough that I'm new to GUIs and any advice is really appreciated.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
guy_sensei
  • 513
  • 1
  • 6
  • 21
  • [Layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) are your friends – Reimeus May 14 '15 at 12:15
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 14 '15 at 20:51

4 Answers4

5

A component can only have a single parent. Add a scrollpane at application startup instead of the JTextArea

frame.add(new JScrollPane(textArea));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I tried this, the JtextArea remains, but the strings are not printed anymore and no scroll. http://oi57.tinypic.com/np2cg9.jpg – guy_sensei May 14 '15 at 12:25
  • Don't add a new scrollpane in your `ActionListener` just update the one `textArea` – Reimeus May 14 '15 at 12:31
  • http://oi57.tinypic.com/2ai0r47.jpg Okay, so no scroll, and JtextArea is not visible before I click the button, but when I click the button the text is no longer invisible. Just need to determine how to show empty Jtextarea before button click and have a scroll. – guy_sensei May 14 '15 at 12:38
  • You're still adding a textarea in your listener. As for the 2nd part, check out [setText](http://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#setText-java.lang.String-) in the docs... – Reimeus May 14 '15 at 12:45
  • If it's not in the action listener, then none of my information/textArea will show when the button is clicked, I tried adding it before and after action listener. maybe ELI5 ? ... – guy_sensei May 14 '15 at 12:55
  • http://oi61.tinypic.com/2zzltnd.jpg Literally the closest I've come, everything is working, just need to add scroll. – guy_sensei May 14 '15 at 13:00
1

Simple Solution:

when adding JTextArea to JFrame just add scroll pane in it like this

JTextArea textarea = new JTextArea();
add(new JScrollPane(textarea));
Achrome
  • 7,773
  • 14
  • 36
  • 45
Programmer
  • 445
  • 4
  • 12
0

When adding components while the GUI is already shown, you need to call re/in/validate to get everything laid out, and repaint() to get it to display

btnNewButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> anomalies = vessels.coordinateAnomaly();
        JScrollPane jp = new JScrollPane();
        jp.setViewportView(textArea);

        for (String a : anomalies) {
            textArea.append(a + "\n");
        }

        textArea.setBounds(10, 79, 172, 339);
        frame.getContentPane().add(jp); // changed
        frame.getContentPane().invalidate(); // added
        frame.getContentPane().validate(); // added
        frame.getContentPane().repaint(); // added
    }
});
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • So I tried this, but the textArea now disappears when I click Coordinate Anomalies http://i57.tinypic.com/fvvh2x.png – guy_sensei May 14 '15 at 12:20
0

Thanks to @Reimeus for help, This was the solution I ended up implementing and it worked.

JTextArea textArea = new JTextArea();
JScrollPane jp = new JScrollPane(textArea);
jp.setBounds(10, 79, 172, 339);
frame.getContentPane().add(jp);

JButton btnNewButton_1 = new JButton("Coordinate Anomalies");
btnNewButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        textArea.setText("");
        ArrayList<String> anomalies = vessels.coordinateAnomaly(); 

        for(String a : anomalies){
               textArea.append(a + "\n");
            }
    }
});
btnNewButton_1.setBounds(10, 45, 172, 23);
frame.getContentPane().add(btnNewButton_1);
guy_sensei
  • 513
  • 1
  • 6
  • 21