Removing empty items from a JList

Removing empty items from a JList

By : zanhero
Date : November 22 2020, 10:33 AM
Any of those help I have coded a simply to do list program which adds text the user inputs through a JInputDialog (for example: "go grocery shopping") to a JList. The program is running fine, but I thought I would try to prevent the user from pressing okay in the dialog box without entering text, or just entering spaces, through the following code: , Why not use s.trim() instead of your removeSpaces method?
code :
} else if (src == create) {
    //show an input dialog box
    String s = JOptionPane.showInputDialog(this, "What do you want to remember?");

    /*f the length of the given string is zero, or if the length of the string without spaces
        is zero, then tell the user*/
    if (s.trim.length() == 0) {
        System.out.println("Nothing has been entered");
        JOptionPane.showMessageDialog(this, "You must enter a text value!");
        //if the string is valid, add it to the file
    } else {
        System.out.println("Item added to list. " + s.length());

} else if (src == close) {
public String removeSpaces(String s){
    return s.replaceAll("\\s+", "");

Share : facebook icon twitter icon
Removing dotted border around selected JList items

Removing dotted border around selected JList items

Date : March 29 2020, 07:55 AM
seems to work fine For many look-and-feels you can simple remove the focus border by setting the following UI property:
code :
UIManager.put("List.focusCellHighlightBorder", BorderFactory.createEmptyBorder());
JList adding and removing items (Netbeans)

JList adding and removing items (Netbeans)

By : KMR Adi
Date : March 29 2020, 07:55 AM
it helps some times You set the model of the list to an AbstractListModel. You can't cast the model to a DefaultListModel. Trying to do so will give you a ClassCastException So set the model to a DefaultListModel instead.
code :
jList1.setModel(new DefaultListModel());
Removing items from a JList

Removing items from a JList

By : user3689041
Date : March 29 2020, 07:55 AM
To fix this issue The way you do it is wrong. With your constructor new JList(j) there is only a "read- only model".
http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html It's easy to display an array or Vector of objects, using the JList constructor that automatically builds a read-only ListModel instance for you:
code :
public class JListTest {

public static void main(String[] args) {

    DefaultListModel<String> model = new DefaultListModel<>();
    final JList<String> list = new JList<String>(model);

    JButton open = new JButton("open");
    open.addActionListener(new ActionListener()

        public void actionPerformed(ActionEvent e) {
            DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
    JFrame frame = new JFrame();
    frame.setSize(400, 400);
    Container con = frame.getContentPane();
    con.setLayout(new BorderLayout());

    con.add(open, BorderLayout.LINE_START);
    con.add(list, BorderLayout.CENTER);
    con.add(new JScrollPane(list));

Removing And Adding Tons of Items In JList

Removing And Adding Tons of Items In JList

By : Sky Tử
Date : March 29 2020, 07:55 AM
I wish did fix the issue. DefaultListModel's implementation is not the best for this kind of job. And adding/removing one by one is not efficient too, as it fires refresh event at each call.
Best would be to create your own ListModel by extending AbstractListModel.
Removing items from JList

Removing items from JList

By : trotterman
Date : March 29 2020, 07:55 AM
Hope that helps Assuming your index is non-negative (as mentioned by others), see if this works (in your listener):
Related Posts Related Posts :
  • The type org.openqa.selenium.firefox.FirefoxDriver is not accessible
  • Limitations (drawbacks) of java programming language?
  • Error: ')' expected Error: illegal start of expression
  • TableRowSorter use LookAndFeel of TableHeader, but own Comparator
  • Difference between @GeneratedValue and @GenericGenerator
  • Design Scenario: Smartphone class and will have derived classes like IPhone,AndroidPhone,WindowsMobilePhone can be even
  • Java date parsing without separators?
  • how to map an array of custom type from postgres to java using hibernate
  • Why hibernate always call "update" statement after using "select" statement in MySQL?
  • An interview topic: What can go wrong with this code?
  • How do I regex remove whitespace and newlines from a text, except for when they are in a json's string?
  • How to turn off Preferences > Compiles > User External Build?
  • how to convert arraylist to string array with all items in double quote
  • PMD+Maven+JAVA Error:: Can't find resource rulesets/comments.xml. Make sure the resource is a valid file or URL or is on
  • How to extend AbstractScheduledService.Scheduler
  • Primefaces tabview: set Active Index after some counting
  • Need help in Regex to exclude splitting string within "
  • Get header from HttpUrlConnection object
  • encoding xlsx as base64 and send it as an attachment
  • How to acess HttpServletRequest in Jaas login module in Tomcat
  • How to implement a basic pointer
  • Java Swing Exit Icon?
  • Why onNext has no effect
  • IntelliJ Idea 2018.2 no option for importing gradle project
  • Remove a negative element from an array w/o array.copy
  • Could this prevent overflow in Java?
  • In Java Swing, can I receive Caret events in real time?
  • I need help not using brute force when using JFrames and DrawWindows
  • How do I refactor ArrayList<MyClass> into MyClassLIst?
  • My program keeps saying that the method cannot be resolved
  • BubbleSort -my code returns random addresses
  • JavaFX components inside HTML?
  • Character in Java game not responding
  • Working around access denied in a FileWalking Tree in Java7
  • How to avoid if/else when using multple suppliers?
  • How to pass object from table cell editor to Table Model?
  • Paypal Sandbox payment state pending
  • sejda-console.bat passing parameters with spaces
  • Multiple Consumers Spring Kafka
  • Bitbucket pipeline: environmental variables in build.gradle file not recognized
  • Java static enum method to return a default enum value
  • What is the complexity of empty for loop?
  • Group randomly List of Lists as N subsets with K elements in each
  • How to set texture on a shape drawn by glDrawArrays()?
  • How to make a jlabel resizable according to the window
  • Java Mathematical Expression Syntax
  • assigning values to characters in java
  • Method overriding and Inheritance in java
  • given a set of lists/groups with a series of number, find matching numbers
  • Connection to HID USB device (keyboard and mouse) in android
  • Inject HttpServletRequest in CDI SessionScoped bean
  • Pool game in java - ball collision algorithm
  • Focus ScrollView to selected position programmatically - Android
  • Is it hibernate bug?
  • Glib memory allocation error
  • Android Proguard - step by step
  • Setting the pivot point of a JPanel to its center
  • java check time is greater time
  • how to save a screenshot (matlab)
  • Anti-aliasing filled shapes in libgdx
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org