Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

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

0
hadesflames5
Asked:
hadesflames5
  • 2
  • 2
  • 2
1 Solution
 
CEHJCommented:
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
0
 
hadesflames5Author Commented:
ok I added the FlowLayout before the components but it still doesn't show up...
0
 
CEHJCommented:
However, if you keep copy and pasting the text. you'll see both scrollbars appear
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
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();
                  }
            });
      }
}

0
 
objectsCommented:
You had a variety of problems with it, let me know if you have any questions :)

0
 
hadesflames5Author Commented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now