Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JScrollPane Difficulty. Help Please

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JScrollPane Difficulty. Help Please

    I am building a menu gui for this application. The user enters the number of players they have and then when they hit next the window is supposed to display entry boxes for their names based on the number of players. I am having trouble with the scroll pane I'm attempting to display them in, it just isn't displaying anything.

    Here is my code:

           public void enterPlayersMenu(){
    		playersArray = new JTextField[numberPlayers];
    		players = new String[numberPlayers];
     
    		JPanel enterPlayersPane = new JPanel();
    		enterPlayersPane.setLayout(null);
     
    		for (int i = 0; i < numberPlayers; i++){
    			playersArray[i] = fieldAskStr(enterPlayersPane, "Player " + (i + 1), null, 0, i * 20);
    		}
     
    		JScrollPane	enterPlayersScrollPane = new JScrollPane(enterPlayersPane);
    		enterPlayersScrollPane.setLayout(new FlowLayout());
    		enterPlayersScrollPane.setBounds(0,0,WIDTH,HEIGHT);
    		enterPlayersScrollPane.createVerticalScrollBar();
     
    		panel.add(enterPlayersScrollPane);
    	}

    and here is the fieldAskStr() method:

    	public JTextField fieldAskStr(JComponent panel, String labelStr, String seed, int posX, int posY){
    		JLabel label = new JLabel(labelStr);
    		label.setBounds(posX, posY, labelStr.length() * 6, 20);
     
    		panel.add(label);
     
    		JTextField textField = new JTextField(20);
    		textField.setBounds(WIDTH/2, posY, WIDTH/2, 20);
    		textField.setDocument(new JTextFieldLimit(20));
    		if (seed != null){textField.setText(seed);}
     
    		panel.add(textField);
     
    		return textField;
    	}

    The code returns an error on the enterPlayersScrollPane.setLayout(new FlowLayout()), that i can only get rid of by deleting the line entirely. I think my problem may be in the layout manager I am using. I just don't know what it is or how to fix it.


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: JScrollPane Difficulty. Help Please

    Just study this piece of code and try if u can figure something out. This is a simple example of a ScrollPane.
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.border.LineBorder;
     
    public class AddingToJScrollPane {
     
      public static void main(String args[]) {
        JFrame frame = new JFrame("Tabbed Pane Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        JLabel label = new JLabel("Label");
        label.setPreferredSize(new Dimension(1000, 1000));
        JScrollPane jScrollPane = new JScrollPane(label);
     
        JButton jButton1 = new JButton();
     
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.setViewportBorder(new LineBorder(Color.RED));
        jScrollPane.getViewport().add(jButton1, null);
     
        frame.add(jScrollPane, BorderLayout.CENTER);
        frame.setSize(400, 150);
        frame.setVisible(true);
      }
    }

  3. The Following User Says Thank You to Karthik Prabhu For This Useful Post:

    AceApple (June 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JScrollPane Difficulty. Help Please

    Thank you so much, that actually helped. Was a little confusing, but then finally I realized what you where saying by covering up the JLabel with the JButton. I needed to use a JPanel in a viewport and call .setPreferredSize(new Dimension(WIDTH, numberPlayers * 20)). Works now thanks!.

Similar Threads

  1. Difficulty with method headers.
    By tripline in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 30th, 2011, 10:58 AM
  2. Get Box to Respond to JScrollPane
    By bgroenks96 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 25th, 2011, 10:18 AM
  3. [SOLVED] Difficulty with string passing...
    By Destined in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 05:43 AM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. JScrollPane
    By MysticDeath in forum AWT / Java Swing
    Replies: 1
    Last Post: February 17th, 2010, 10:21 PM