Reputation: 33
Mr. Hovercraft Full Of Eels
I almost did what you said. MY APOLOGY it is still not showing :(
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 760, 666);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.getContentPane().setLayout(null);
//frame.getContentPane().setLayout();
textArea = new JTextArea(20,20);
textArea.setPreferredSize(new Dimension(100, 100));
textArea.setLineWrap(true);
JScrollPane sp = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(sp);
frame.setVisible(true);
}
I'm following a tutorial on YouTube using WindowBuilderPro in Eclipse. I want to create (scrollable) JTextArea using JScollPane. Even though I followed the principles, it didn't work! This little thing consumes my time so I decided to ask you guys :( KINDLY, can someone tell me why JTextArea isn't showing in JFrame .
public class ProjectGUI {
private JFrame frame;
private JTextField urls;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProjectGUI window = new ProjectGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ProjectGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 760, 666);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "X.509 Extractor", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(18, 16, 722, 145);
panel.setLayout(null);
frame.getContentPane().add(panel);
urls = new JTextField();
urls.setText("For multiple URLs, separate them by comma ,");
urls.setForeground(Color.BLUE);
urls.setBounds(59, 57, 646, 28);
panel.add(urls);
JButton button1 = new JButton("EXTRACT");
button1.setBounds(303, 97, 117, 40);
frame.setLocationRelativeTo(null);
panel.add(button1);
textArea = new JTextArea();
textArea.setBounds(18, 212, 722, 414);
textArea.setLineWrap(true);
JScrollPane sp= new JScrollPane(textArea);
frame.getContentPane().add(sp);
frame.setVisible(true);
}
}
Upvotes: 1
Views: 467
Reputation: 285460
You're setting bounds of your JTextArea, something you should never do, and this is preventing it from growing within the JScrollPane.
Your JTextArea isn't showing because you're using a null layout and add a component without specifying its size or position, the JScrollPane.
Suggestions:
setBounds
Edit
Regarding your latest post, let me add, just to be clear:
You will see scroll bars when the text inside of the JTextArea is larger than the bounds of the JScrollPane's viewport. For example:
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JTextAreaTest {
public static void main(String[] args) {
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 20; j++) {
for (int k = 0; k < random.nextInt(5) + 5; k++) {
char c = (char) ('a' + random.nextInt(26));
sb.append(c);
}
sb.append(' ');
}
sb.append('\n');
}
JTextArea textArea = new JTextArea(15, 40);
textArea.setText(sb.toString());
JScrollPane scrollpane = new JScrollPane(textArea);
JOptionPane.showMessageDialog(null, scrollpane);
}
}
Upvotes: 4