Reputation: 3
I'm trying to put one JTextArea
next to another JTextArea
in a GUI
I am writing a GUI for a database and wanted to put the data from each column in a different JTextArea. This will make my GUI look a lot better and make it easier to view the data. I have already tried adding the JTextAreas
to a JPanel
, but that doesn't seem to be working.
This is what I've tried so far:
public class GUIDisplayBooks extends JFrame{
JPanel panel = new JPanel();
JTextArea textAreaIsbn = new JTextArea();
JTextArea textAreaTitle = new JTextArea();
JTextArea textAreaSurname = new JTextArea();
JTextArea textAreaForename = new JTextArea();
JTextArea textAreaCategory = new JTextArea();
JScrollPane scrollPane = new JScrollPane(panel);
GUIDisplayBooks(ArrayList<Book> books)
{
this.add(panel);
this.setSize(600,200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
for(Book book : books){
textAreaIsbn.append(book.getIsbn() + "\n");
textAreaTitle.append(book.getTitle() + "\n");
textAreaSurname.append(book.getSurname() + "\n");
textAreaForename.append(book.getForename() + "\n");
textAreaCategory.append(book.getCategory() + "\n");
}
panel.add(textAreaIsbn);
panel.add(textAreaTitle);
panel.add(textAreaSurname);
panel.add(textAreaForename);
panel.add(textAreaCategory);
add(scrollPane);
}
}
I keep getting a blank GUI window. Maybe it's something really obvious, any help appriciated
Upvotes: 0
Views: 176
Reputation: 11153
Swing components can only have a single parent:
JScrollPane scrollPane = new JScrollPane(panel);
I basically the same of this:
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(panel);
Then you're adding that same panel
to your JFrame
:
this.add(panel);
Which removes it from the JScrollPane
and then you add the empty JScrollPane
to the JFrame
:
add(scrollPane);
So, removing this line, should make your program to work:
this.add(panel);
Upvotes: 1
Reputation: 490
You could use a layout. Various implementations exists, you can take a first look here.
A FlowLayout with Horizontal orientation should do it.
Upvotes: 0