Link to home
Start Free TrialLog in
Avatar of hadesflames5
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();
				}
			});
		}
	}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of hadesflames5
hadesflames5

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.ActionListener;

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
                        .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            f.getContentPane().setLayout(new BorderLayout());
            f.getContentPane().add(BorderLayout.NORTH, p);
            f.getContentPane().add(BorderLayout.CENTER, scroll);
            in.selectAll();
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            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 {
                                          out.setText("Your random password is :\n\n" + gen(b));
                              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();
                  }
            });
      }
}

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.