Hello Forum, Vaironl here.
I'm going to ask a question, that will most likely annoy you all, but first let me say this, I did some research but cannot use a Jscrollbar efficiently.

That being said, I want to give more details.

I'm trying to add a scrollbar to a panel which is going to contain JLabels and JTextFields.
It will contain 40 of each ones.
It should be set to the East.
I only need a vertical scrollbar since this will be a test only.

I actually am making a test program instead of my main project for testing purposes, since it has worked before this way when I had any problems.
Here is the test with the things I'm doing wrong.

Main class

import javax.swing.*;


public class Driver {

	public static void main(String[] args)
	{
		Frame frame = new Frame();

	}

}

Panel

import java.awt.Color;

import javax.swing.*;

public class Panel extends JPanel{


	JTextField field[] = new JTextField[40];
	public Panel()
	{

		setBackground(Color.red);
		for(int i=0;i<40;i++)
		{
			field[i] = new JTextField(40);
			field[i].setText("Field["+(i+1)+"]");
			add(field[i]);
		}
	}

}

Frame

import java.awt.BorderLayout;

import javax.swing.*;

public class Frame extends JFrame
{

	JScrollBar ver = new JScrollBar(JScrollBar.VERTICAL);
	
	Panel panel = new Panel();
	public Frame()
	{
		super("Testing Scroller");
		setLayout(null);
		ver.setLocation(0,0);
		ver.setSize(400,500);
		panel.setSize(500,500);
		ver.add(panel);
		add(ver);
		setSize(800,600);
		setLocation(10,30);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

}

This is normally done by using a JScrollPane (which has the scroll bar(s) and handles all the sizing and scrolling activity). Just create your JPanel and place it in a JScrollPane. Here's the tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

Thanks for the reference, I have read the article before but for some reason I could not get it to work.
Once again thanks, I will now learn how to indent the things I need Once again thanks

This is normally done by using a JScrollPane (which has the scroll bar(s) and handles all the sizing and scrolling activity). Just create your JPanel and place it in a JScrollPane. Here's the tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

hello once again JamesCherrill, and sorry to bother you at the moment.
I'm having a bit of trouble... I'm trying to set all of my labels etc to start at the left side, When I do c.anchor = GridBagConstraints.EAST;
there are no syntax errors but everything is centered.

It's hard/impossible to diagnose without more of the code. Is "c" the container or a label? Can you post the code that does the adding and setting of the GridBagLayout parameters?

It's hard/impossible to diagnose without more of the code. Is "c" the container or a label? Can you post the code that does the adding and setting of the GridBagLayout parameters?

I will post only the variables and the part in which it's being used since the file its fairly long.

private JPanel subPanel = new JPanel(); // Ingredients Panel
	private JScrollPane scroller = new JScrollPane(subPanel); // scrollpane for ingredients

//ingredients
		scroller.setSize(width-500,200);
		scroller.setLocation(ingrScrollPosx+10, ingrScrollPosy);
		subPanel.setBackground(Color.red);
		subPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx =0;
		c.gridy =0;
		c.fill = c.EAST;
		for(int i=0;i<ingredientsLabel.length;i++)
		{
			ingredientsLabel[i] = new JLabel("Ingredient"+(i+1)+":");
			ingredientsField[i] = new JTextField(25);
			ingredientsLabel[i].setSize(30,15);
		}
		for(int i=0;i<ingredientsLabel.length;i++)
		{
			c.gridx++;
			subPanel.add(ingredientsLabel[i], c);
			c.gridx++;
			subPanel.add(ingredientsField[i], c);
			if(c.gridx==2)
			{
				c.gridx=0;
				c.gridy++;
			}			
		}

		add(scroller);

EAST is right, use WEST for left.
Leave fill at its default (none) and anchor WEST and see if that helps

EAST is right, use WEST for left.
Leave fill at its default (none) and anchor WEST and see if that helps

Sorry for that, I tried that yesterday and now... still it will not work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.