hadesflames5
asked on
Problem with JScrollPane in Java
Below is my code.....when I run this the scroll bar doesn't show...any help please?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class randomPass{
public static String gen(int x){
String nums = "";
for(int i = 0; i<x; i++){
nums += (char) ((int) (33+Math.random()*94));
}
return nums;
}
public static void main(String args[]){
final String vernum = "v1.2";
final JTextField in = new JTextField("Insert the password length here.", 20);
final JFrame f = new JFrame("Random Password Generator v1.1.0");
JButton btn = new JButton("Close");
final JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
final JScrollPane scroll = new JScrollPane(p);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(in);
f.getContentPane().add(btn);
f.getContentPane().add(scroll);
in.selectAll();
f.setSize(370,250);
f.setLocationRelativeTo(null);
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(370, 250);
f.setVisible(true);
in.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String x = in.getText();
int b = Integer.parseInt(x);
if(b<1){
JOptionPane.showMessageDialog(null,"The password length must be greater than 0, please try again","Random Password Generator "+vernum,JOptionPane.ERROR_MESSAGE);
in.selectAll();
}else{
final JTextArea out = new JTextArea("Your random password is :\n\n"+gen(b),5,20);
JScrollPane scroll2 = new JScrollPane(out);
scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
p.add(scroll2);
f.setVisible(false);
f.setVisible(true);
in.selectAll();
}
}
}
);
btn.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"Thank you for using the Random Password Generator "+vernum, "Random Password Generator "+vernum,JOptionPane.INFORMATION_MESSAGE);
f.dispose();
}
});
}
}
The scroll bar will only appear when it's needed. Since you don't have line wrap on, the vertical one will never appear. You should set the FlowLayout before you add your components
ASKER
ok I added the FlowLayout before the components but it still doesn't show up...
However, if you keep copy and pasting the text. you'll see both scrollbars appear
try this:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class randomPass {
public static String gen(int x) {
String nums = "";
for (int i = 0; i < x; i++) {
nums += (char) ((int) (33 + Math.random() * 94));
}
return nums;
}
public static void main(String args[]) {
final String vernum = "v1.2";
final JTextField in = new JTextField(
"Insert the password length here.", 20);
final JFrame f = new JFrame("Random Password Generator v1.1.0");
JButton btn = new JButton("Close");
JPanel p = new JPanel();
p.add(in);
p.add(btn);
final JTextArea out = new JTextArea();
final JScrollPane scroll = new JScrollPane(out);
scroll
.setVerticalScrollBarPolic y(JScrollP ane.VERTIC AL_SCROLLB AR_AS_NEED ED);
f.getContentPane().setLayo ut(new BorderLayout());
f.getContentPane().add(Bor derLayout. NORTH, p);
f.getContentPane().add(Bor derLayout. CENTER, scroll);
in.selectAll();
f.setLocationRelativeTo(nu ll);
f.setDefaultCloseOperation (JFrame.EX IT_ON_CLOS E);
f.setSize(370, 250);
f.setVisible(true);
in.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven t e) {
String x = in.getText();
int b = Integer.parseInt(x);
if (b < 1) {
JOptionPane
.showMessageDialog(
null,
"The password length must be greater than 0, please try again",
"Random Password Generator " + vernum,
JOptionPane.ERROR_MESSAGE) ;
in.selectAll();
} else {
out.setText("Your random password is :\n\n" + gen(b));
in.selectAll();
}
}
});
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven t e) {
JOptionPane.showMessageDia log(null,
"Thank you for using the Random Password Generator "
+ vernum,
"Random Password Generator " + vernum,
JOptionPane.INFORMATION_ME SSAGE);
f.dispose();
}
});
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class randomPass {
public static String gen(int x) {
String nums = "";
for (int i = 0; i < x; i++) {
nums += (char) ((int) (33 + Math.random() * 94));
}
return nums;
}
public static void main(String args[]) {
final String vernum = "v1.2";
final JTextField in = new JTextField(
"Insert the password length here.", 20);
final JFrame f = new JFrame("Random Password Generator v1.1.0");
JButton btn = new JButton("Close");
JPanel p = new JPanel();
p.add(in);
p.add(btn);
final JTextArea out = new JTextArea();
final JScrollPane scroll = new JScrollPane(out);
scroll
.setVerticalScrollBarPolic
f.getContentPane().setLayo
f.getContentPane().add(Bor
f.getContentPane().add(Bor
in.selectAll();
f.setLocationRelativeTo(nu
f.setDefaultCloseOperation
f.setSize(370, 250);
f.setVisible(true);
in.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
String x = in.getText();
int b = Integer.parseInt(x);
if (b < 1) {
JOptionPane
.showMessageDialog(
null,
"The password length must be greater than 0, please try again",
"Random Password Generator " + vernum,
JOptionPane.ERROR_MESSAGE)
in.selectAll();
} else {
out.setText("Your random password is :\n\n" + gen(b));
in.selectAll();
}
}
});
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
JOptionPane.showMessageDia
"Thank you for using the Random Password Generator "
+ vernum,
"Random Password Generator " + vernum,
JOptionPane.INFORMATION_ME
f.dispose();
}
});
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks. I tried doing it the way you did it but I didn't know of the setText() method so it didn't work out so well lol.