ScrollPane?

can anyone help me please?

how do I get the ScrollPane on the JList?

I ve tried to do this:

JScrollPane scr = new JScrollPane(list);

orderBillPanel.add(scr);

but it does not work. Why?

What am I doing wrong?

Thanks.

import javax.swing.UIManager;

import java.awt.*;

public class GuiPointOfSaleClass {

boolean packFrame = false;

/**Construct the application*/

public GuiPointOfSaleClass() {

Frame1 frame = new Frame1();

//Validate frames that have preset sizes

//Pack frames that have useful preferred size info, e.g. from their layout

if (packFrame) {

frame.pack();

}

else {

frame.validate();

}

//Center the window

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = frame.getSize();

if (frameSize.height > screenSize.height) {

frameSize.height = screenSize.height;

}

if (frameSize.width > screenSize.width) {

frameSize.width = screenSize.width;

}

frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

frame.setVisible(true);

}

/**Main method*/

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(Exception e) {

e.printStackTrace();

}

new GuiPointOfSaleClass();

}

}

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

public class Frame1 extends JFrame {

JPanel contentPane;

JPanel orderBillPanel = new JPanel();

JList list;

DefaultListModel listModel;

Border border1;

public Frame1() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

/**Component initialization*/

private void jbInit() throws Exception {

border1 = BorderFactory.createEtchedBorder(Color.white,new Color(165, 163, 151));

//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(null);

this.setTitle("PointOfSale");

this.setSize(new Dimension(1100, 800));

orderBillPanel.setBounds(new Rectangle(11, 138, 293, 406));

orderBillPanel.setLayout(null);

listModel = new DefaultListModel();

//Create the list and put it in a scroll pane.

list = new JList(listModel);

list.setBackground(Color.yellow);

list.setFont(new java.awt.Font("Dialog", 0, 14));

list.setBounds(new Rectangle(11, 8, 377, 418));

contentPane.add(orderBillPanel, null);

orderBillPanel.add(list);

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

list.setSelectedIndex(0);

list.setVisibleRowCount(50);

}

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(Exception e) {

e.printStackTrace();

}

new GuiPointOfSaleClass();

}

}

[3302 byte] By [Max010a] at [2007-10-3 2:14:08]
# 1

Please use the code tags.

Yes this is how to get the scroolpane on the Jlist.

JScrollPane scr = new JScrollPane(list);

orderBillPanel.add(scr);

But change:

contentPane.setLayout(null);

to

contentPane.setLayout(new BorderLayout());

zadoka at 2007-7-14 19:12:58 > top of Java-index,Java Essentials,New To Java...
# 2

Why?

Does the ScrollPane appears only if the contentPane layout is BorderLayout?

I d like to have the JList component inside the orderBillPanel with the latter including a JScrollPane.

I ve changed it as you suggested but I do not see the JScrollPane.

I am getting more confused now.

I have done it many times in the past. Why does not work?

Any advice?

private void jbInit() throws Exception {

border1 = BorderFactory.createEtchedBorder(Color.white,new Color(165, 163, 151));

//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(new BorderLayout());

contentPane.add(orderBillPanel, BorderLayout.CENTER);

this.setTitle("PointOfSaleX");

this.setSize(new Dimension(1100, 800));

orderBillPanel.setLayout(null);

listModel = new DefaultListModel();

//Create the list and put it in a scroll pane.

list = new JList(listModel);

list.setBounds(new Rectangle(0, 0, 412, 456));

list.setBackground(Color.yellow);

list.setFont(new java.awt.Font("Dialog", 0, 14));

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

list.setSelectedIndex(0);

orderBillPanel.add(list);

list.setVisibleRowCount(5);

JScrollPane scr = new JScrollPane(list);

scr.setBounds(new Rectangle(0, 0, 425, 230));

orderBillPanel.add(scr);

}

Max010a at 2007-7-14 19:12:58 > top of Java-index,Java Essentials,New To Java...
# 3
Both versions of your code work fine for me, I get a frame with a yellow list box. And the scrollbars work if you add enough items to the list.
BinaryDigita at 2007-7-14 19:12:58 > top of Java-index,Java Essentials,New To Java...