0

I'm currently dealing with Swing based desktop application in Java. I have come across this problem below.

I have been trying to add scrollbars into my ListArea (JList type) but I couldn't do so albeit so many things there were I intended. Here is the code snippet.. I'm writing this code in MainFrame extending JFrame. Any help would be appreciated. Thanks...

super(title)
Jpanel panel = new Panel()
panel(add)
panel.setlayout(null)

final JList<String> listArea = new JList<String>(labels);
    listArea.setBounds(50, 180, 700, 300);
    listArea.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listArea.setFont(new Font("Serif", Font.ITALIC, 14));
    listArea.setVisibleRowCount(-1);
    JScrollPane listScroller = new JScrollPane();
    listScroller.setViewportView(listArea);
    listArea.setLayoutOrientation(JList.VERTICAL);
    panel.add(listArea); 
    panel.add(listScroller);
Ozan Goylusun
  • 29
  • 4
  • 9

1 Answers1

4

Get rid of panel.add(listArea);, it's removing the listArea from the JScrollPane

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify.

JScrollPane (and JViewport) have there own layout routines, which you don't control. Setting the size of the JList will have no effect once you pass it to the JScrollPane.

See Why is it frowned upon to use a null layout in SWING? for more details

I don't know what you expect listArea.setVisibleRowCount(-1); to do, but I'd advise against it.

Updated with example

A List

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestList {

    public static void main(String[] args) {
        new TestList();
    }

    public TestList() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JPanel panel = new JPanel(new BorderLayout());

                List<String> labels = new ArrayList<>(25);
                for (int index = 0; index < 100; index++) {
                    labels.add("Item " + index);
                }

                final JList<String> listArea = new JList<String>(labels.toArray(new String[labels.size()]));
                listArea.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                listArea.setFont(new Font("Serif", Font.ITALIC, 14));
                JScrollPane listScroller = new JScrollPane();
                listScroller.setViewportView(listArea);
                listArea.setLayoutOrientation(JList.VERTICAL);
                panel.add(listScroller);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    When I remove panel.add(listArea), inherently the list doesn't show up anymore. Besides that, what could I use instead of panel.setLayout(null) The way I formulate my items is as follows (one of them): JLabel serverLabel = new JLabel("ServerName"); serverLabel.setBounds(250, 10, 80, 25); panel.add(serverLabel); final JTextField serverText = new JTextField(30); serverText.setBounds(340, 10, 160, 25); panel.add(serverText); – Ozan Goylusun Oct 29 '14 at 02:11
  • I appreciate it if you could make a slight arrangement in that code snippet in particular – Ozan Goylusun Oct 29 '14 at 02:12
  • What should I use instead of null for that layout? – Ozan Goylusun Oct 29 '14 at 02:19
  • `BorderLayout`, `GridBagLayout`, `GridLayout`, `BoxLayout`...See [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for more ideas – MadProgrammer Oct 29 '14 at 02:20
  • Sure. Let me see them – Ozan Goylusun Oct 29 '14 at 02:37
  • In this case, how should I formulate my other items like ServerName Text, PortNumber Text, Username, password etc. Since it covers the whole frame. I couldn't put my other items using setBounds function. – Ozan Goylusun Oct 29 '14 at 21:16
  • Either use a different layout (like `GridBagLayout`) or a compound series of layouts to achieve the desired results – MadProgrammer Oct 29 '14 at 21:20
  • I decided to use SpringLayout, but I couldn't move it from the upper-left corner of the screen to whatever I want to put listArea.setLayoutOrientation(JList.VERTICAL); layout.putConstraint(SpringLayout.WEST, listArea, 100, SpringLayout.WEST, contentPane); layout.putConstraint(SpringLayout.NORTH, listArea, 60, SpringLayout.NORTH, contentPane); contentPane.add(listScroller); – Ozan Goylusun Oct 29 '14 at 21:43
  • Can you suggest me a Layout so that I can both formulate a login form consisting of four fields (text fields with labels), one button (login), and scrollable listarea where I can move that area (the one with scrolls) – Ozan Goylusun Oct 29 '14 at 21:45
  • Okay Fair Enough, well how should I position that textArea with scrolls in my Frame. The only reason I'm changing my whole frame was to bring up with a new textarea with scrolls as you know. Thanks though again! – Ozan Goylusun Oct 29 '14 at 21:57