Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:36:27+00:00 2026-06-18T09:36:27+00:00

Trying to simple create a JList with a specified visible row count, but a

  • 0

Trying to simple create a JList with a specified visible row count, but a minimum width. It seems “setMinimumSize()” does nothing though…

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class UserInterface 
{   
    final static private int HEIGHT = 400;
    final static private int WIDTH = 650;

    public static void main(String[] args) {

    JPanel content = new JPanel();

    String[] entries = { "Entry 1", "Entry 2", "Entry 3",
    "Entry 4", "Entry 5", "Entry 6" };

    DefaultListModel sampleModel = new DefaultListModel();

    for(int i=0; i<entries.length; i++)
        sampleModel.addElement(entries[i]);

    JList sampleList = new JList(sampleModel);

    sampleList.setMinimumSize(new Dimension(1000,1000));
    sampleList.setMaximumSize(new Dimension(1000,1000));

    content.add(sampleList);

    //main window frame
    JFrame window = new JFrame("NAD Assignment 1");
    window.setSize(WIDTH, HEIGHT);
    window.setContentPane(content);
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    window.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0); 
        }
    });
}
}

setPreferredSize() seems to work, but it overrides whatever I set up in setVisibleRowCount():

public static void main(String[] args) {

    JPanel content = new JPanel();

    String[] entries = { "Entry 1", "Entry 2", "Entry 3",
    "Entry 4", "Entry 5", "Entry 6" };

    DefaultListModel sampleModel = new DefaultListModel();

    for(int i=0; i<entries.length; i++)
        sampleModel.addElement(entries[i]);

    JList sampleList = new JList(sampleModel);

    sampleList.setPreferredSize(new Dimension(200,10));
    sampleList.setVisibleRowCount(8);

    content.add(sampleList);

    //main window frame
    JFrame window = new JFrame("NAD Assignment 1");
    window.setSize(WIDTH, HEIGHT);
    window.setContentPane(content);
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    window.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0); 
        }
    });
}

How can I set a minimum width on my JList and specify the height with setVisibleRowCount? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:36:28+00:00Added an answer on June 18, 2026 at 9:36 am

    There were several things wrong with your code.

    1. Always invoke a Swing application with SwingUtilities. This ensures that the Swing components are created and executed on the Event Dispatch thread (EDT).

    2. Separate out the data creation from the GUI creation. This allows you to focus on one part of the code at a time. Also, put your code into class methods, rather than trying to write everything in the main method.

    3. Break your application up into smaller and smaller pieces until you can code each piece without much thought. The thinking is in breaking up your application.

    4. You must use a Swing layout. I chose the BorderLayout, but another layout may work better for what you want. Study Oracle’s Visual Guide to Layout Managers until you can describe the Swing layout managers in your sleep.

    5. Finally, JList works better when you enclose it in a JScrollPane.

    .

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    
    public class UserInterface implements Runnable {
        final static private int    HEIGHT  = 400;
        final static private int    WIDTH   = 650;
    
        private String[]            entries = { "Entry 1", "Entry 2", "Entry 3",
                "Entry 4", "Entry 5", "Entry 6" };
    
        private DefaultListModel    sampleModel;
    
        public UserInterface() {
            this.sampleModel = new DefaultListModel();
        }
    
        @Override
        public void run() {
            createPartControl();
        }
    
        protected void createPartControl() {
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
    
            for (int i = 0; i < entries.length; i++) {
                sampleModel.addElement(entries[i]);
            }
    
            JList sampleList = new JList(sampleModel);
            sampleList.setMinimumSize(new Dimension(1000, 1000));
            sampleList.setMaximumSize(new Dimension(1000, 1000));
    
            JScrollPane scrollPane = new JScrollPane(sampleList);
    
            content.add(scrollPane, BorderLayout.CENTER);
    
            // main window frame
            JFrame window = new JFrame("NAD Assignment 1");
            window.setSize(WIDTH, HEIGHT);
            window.setContentPane(content);
            window.setLocationRelativeTo(null);
            window.setVisible(true);
    
            window.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new UserInterface());
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a simple pan and zoom app using silverlight 4, but
Im trying to create a simple input box with form validation, but im not
I'm trying to create simple Ajax call, but after clicking the link I get
I'm trying to create simple urlMapping, but it doesn't work. It goes into constructor,
I am trying to do something very simple: create a new branch. But I
I'm trying to create simple script that subscribes a user to a company calendar.
I'm studying webrat and cucumber and trying to create simple example. Here is my
I setup a Paypal Sandbox account and am trying to create simple purchases through
I'm trying to create a simple widget to retrieve a users feeds, I have
I am trying to create a simple about page that uses JQuery click to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.