Multiple JTextField in JPanel with scroller

Hi All,

I have a situation where I have to display Multiple jtextfiled in panel and that panel should be with scroller, so that I can add any no of textfield with-in the panel.

Please some one help, if possible give me a sample code aslo.

Thanks and Regards,

dkumar_sharma

[303 byte] By [dkumar_sharmaa] at [2007-11-27 4:49:27]
# 1

JPanel panel = new JPanel();

panel.add(new JTextField(25);

panel.add(new JTextField(25);

..

JScrollPane scrollPane = new JScrollPane(panel, 100, 100);

The two numbers (100 and 100) is the size that the scroll pane will show, so you can change these if you need a different size. If the panel is larger than this dimension, it will have scroll bars so you can scroll to all the text fields.

clairec666a at 2007-7-12 10:02:34 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks clairec666 for response, now its giving me the following excetion

java.lang.IllegalArgumentException: invalid verticalScrollBarPolicy

Below is my code.

/****************************************************************/

/* myscroll*/

/* */

/****************************************************************/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.JScrollBar;

/**

* Summary description for myscroll

*

*/

public class myscroll extends JFrame

{

// Variables declaration

private JTabbedPane jTabbedPane1;

private JPanel contentPane;

//--

private JTextField jTextField1;

private JPanel jPanel1;

//--

// End of variables declaration

public myscroll()

{

super();

initializeComponent();

//

// TODO: Add any constructor code after initializeComponent call

//

this.setVisible(true);

}

private void initializeComponent()

{

jTabbedPane1 = new JTabbedPane();

contentPane = (JPanel)this.getContentPane();

//--

jTextField1 = new JTextField();

jPanel1 = new JPanel();

JScrollPane jsb = new JScrollPane(jPanel1, 100, 100);

//--

//

// jTabbedPane1

//

jTabbedPane1.addTab("jPanel5", jPanel1);

jTabbedPane1.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e)

{

jTabbedPane1_stateChanged(e);

}

});

//

// contentPane

//

contentPane.setLayout(null);

addComponent(contentPane, jTabbedPane1, 2,5,611,456);

//

// jTextField1

//

jTextField1.setText("jTextField6");

jTextField1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

jTextField1_actionPerformed(e);

}

});

//

// jPanel1

//

jPanel1.setLayout(null);

addComponent(jPanel1, jTextField1, 37,411,100,22);

//

// myscroll

//

this.setTitle("myscroll - extends JFrame");

this.setLocation(new Point(0, 0));

this.setSize(new Dimension(669, 506));

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

//

// TODO: Add any appropriate code in the following Event Handling Methods

//

private void jTabbedPane1_stateChanged(ChangeEvent e)

{

System.out.println("\njTabbedPane1_stateChanged(ChangeEvent e) called.");

// TODO: Add any handling code here

}

private void jTextField1_actionPerformed(ActionEvent e)

{

System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

//

// TODO: Add any method code to meet your needs in the following area

//

//============================= Testing ================================//

//==//

//= The following main method is just for testing this class you built.=//

//= After testing,you may simply delete it.=//

//======================================================================//

public static void main(String[] args)

{

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

try

{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}

catch (Exception ex)

{

System.out.println("Failed loading L&F: ");

System.out.println(ex);

}

new myscroll();

}

//= End of Testing =

}

Please have look, what mistake I am doing here?

Thanks & Regards,

dkumar_sharma

dkumar_sharmaa at 2007-7-12 10:02:34 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks clairec666 for response, now its giving me the following exception

java.lang.IllegalArgumentException: invalid verticalScrollBarPolicy

Below is my code.

/****************************************************************/

/* myscroll*/

/* */

/****************************************************************/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.JScrollBar;

/**

* Summary description for myscroll

*

*/

public class myscroll extends JFrame

{

// Variables declaration

private JTabbedPane jTabbedPane1;

private JPanel contentPane;

//--

private JTextField jTextField1;

private JPanel jPanel1;

//--

// End of variables declaration

public myscroll()

{

super();

initializeComponent();

//

// TODO: Add any constructor code after initializeComponent call

//

this.setVisible(true);

}

private void initializeComponent()

{

jTabbedPane1 = new JTabbedPane();

contentPane = (JPanel)this.getContentPane();

//--

jTextField1 = new JTextField();

jPanel1 = new JPanel();

JScrollPane jsb = new JScrollPane(jPanel1, 100, 100);

//--

//

// jTabbedPane1

//

jTabbedPane1.addTab("jPanel5", jPanel1);

jTabbedPane1.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e)

{

jTabbedPane1_stateChanged(e);

}

});

//

// contentPane

//

contentPane.setLayout(null);

addComponent(contentPane, jTabbedPane1, 2,5,611,456);

//

// jTextField1

//

jTextField1.setText("jTextField6");

jTextField1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

jTextField1_actionPerformed(e);

}

});

//

// jPanel1

//

jPanel1.setLayout(null);

addComponent(jPanel1, jTextField1, 37,411,100,22);

//

// myscroll

//

this.setTitle("myscroll - extends JFrame");

this.setLocation(new Point(0, 0));

this.setSize(new Dimension(669, 506));

}

/** Add Component Without a Layout Manager (Absolute Positioning) */

private void addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

//

// TODO: Add any appropriate code in the following Event Handling Methods

//

private void jTabbedPane1_stateChanged(ChangeEvent e)

{

System.out.println("\njTabbedPane1_stateChanged(ChangeEvent e) called.");

// TODO: Add any handling code here

}

private void jTextField1_actionPerformed(ActionEvent e)

{

System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");

// TODO: Add any handling code here

}

//

// TODO: Add any method code to meet your needs in the following area

//

//============================= Testing ================================//

//==//

//= The following main method is just for testing this class you built.=//

//= After testing,you may simply delete it.=//

//======================================================================//

public static void main(String[] args)

{

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

try

{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}

catch (Exception ex)

{

System.out.println("Failed loading L&F: ");

System.out.println(ex);

}

new myscroll();

}

//= End of Testing =

}

Please have a look, what mistake I am doing here?

Thanks & Regards,

dkumar_sharma

dkumar_sharmaa at 2007-7-12 10:02:34 > top of Java-index,Desktop,Core GUI APIs...
# 4
Sorry, I'm not sure what's causing this error! Maybe somebody else can help? (Please!)
clairec666a at 2007-7-12 10:02:34 > top of Java-index,Desktop,Core GUI APIs...
# 5

Check the API for the JScrollPane constructors. The second and third argument in this constructor new JScrollPane(panel, 100, 100);

does not set the size of the scroll pane, but rather the vertical and horizontal scrolling policies. There are only a fixed set of valid policies (if this class had been written today these two arguments should have been enums), and 100 is not one of them.

[url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JScrollPane.html#JScrollPane(java.awt.Component,%20int,%20int)]JScrollPane(Component, int, int)[/url]

Torgila at 2007-7-12 10:02:34 > top of Java-index,Desktop,Core GUI APIs...
# 6
So what you need is:JScrollPane jsb = new JScrollPane(jPanel1);jsb.setPreferredSize(100, 100);
clairec666a at 2007-7-12 10:02:34 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thanks for suggestion, I fixed the problem by adding dimension. Thanks a lot guys.dkumar_sharma
dkumar_sharmaa at 2007-7-12 10:02:34 > top of Java-index,Desktop,Core GUI APIs...