Reputation: 1623
I want to add an object of type JPanel to a JFrame.
I'm trying this, but the Jpanel is not added.
the idea is: Add to P2 a P5 that has the components defined in class P5.
What could be happening ?, I do not want to create all the JPanel in the class First_view, since the code would be messed up a lot.
CODE:
import javax.swing.*;
import java.awt.*;
public class First_view extends JFrame {
Container Frame;
public First_view() {
super("title");
Frame = this.getContentPane();
Frame.setLayout(new BorderLayout());
Frame.add((new P2()), BorderLayout.WEST);
setSize(900, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class P2 extends JPanel {
public P2() {
this.setLayout(new BorderLayout());
add((new P5()), BorderLayout.NORTH);
}
}
class P5 extends JPanel {
JScrollPane informacion = new JScrollPane(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JTextArea T1 = new JTextArea();
public P5() {
setLayout(new FlowLayout());
setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 0));
add(setInformacion());
}
private JScrollPane setInformacion() {
T1.append("Information, bla bla bla");
T1.setEditable(false);
T1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
T1.setLineWrap(true);
informacion.add(T1);
informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return informacion;
}
}
PIC:
Upvotes: 1
Views: 528
Reputation: 29730
The component to display in the JScrollPane
should not be added, use setViewportView
instead.
private JScrollPane setInformacion() {
T1.append("Information, bla bla bla");
...
informacion.setViewportView(T1);
...
informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return informacion;
}
Obs: the arguments passed to the constructor of JScrollPane
are in the wrong order, that is, the vertical police comes first:
JScrollPane informacion = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Edit: as Andrew commented it is not a good idea to extend a class just to use it (JFrame, JPanel). Example, I tried not to change too much of your original flow: package cfh.test;
import javax.swing.*;
import java.awt.*;
public class FirstView {
private JFrame frame;
private JTextArea informacionText; // not sure if that is needed as field
public FirstView() {
informacionText = new JTextArea();
informacionText.setEditable(false);
informacionText.setBorder(BorderFactory.createLineBorder(Color.BLACK));
informacionText.setLineWrap(true);
informacionText.append("Information, bla bla bla");
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
scrollPane.setViewportView(informacionText);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new FlowLayout());
infoPanel.add(scrollPane);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
leftPanel.add(infoPanel, BorderLayout.NORTH);
// TODO consider moving above code to an own method returning the left panel
frame = new JFrame("title");
frame.setLayout(new BorderLayout());
frame.add(leftPanel, BorderLayout.WEST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Upvotes: 3