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 4018780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:01:31+00:00 2026-05-20T10:01:31+00:00

Hi I have to pick an element from a JList to another, removing it

  • 0

Hi I have to pick an element from a JList to another, removing it from the first
The method I’ve created inserts only one element, overwriting the last one and doesn’t remove the selected item from the first JList
Here’s the code:

First list

private javax.swing.JList listaRosa;

Populated by this method:

private void visualizzaRosaButtonvisualizzaRosa(java.awt.event.ActionEvent evt) {                                                    
    // TODO add your handling code here:
    visualizzaSquadraSelezionata();
    String fileSquadra;
    fileSquadra = squadraDaVisualizzare.getText();
    DefaultListModel listModel = new DefaultListModel();
    try {
        FileInputStream fstream = new FileInputStream("C:/Users/Franky/Documents/NetBeansProjects/JavaApplication5/src/javaapplication5/Rose/"+fileSquadra+"");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            listModel.addElement(strLine);
            System.out.println(strLine);
        }
        listaRosa.setModel(listModel);
        //Close the input stream
        in.close();
    } catch (Exception e) {
    }

The second list, where I want to insert items removing from the first:

private javax.swing.JList listaTitolari

Here’s the NOT working code:

private void aggiungiTitolareButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    // TODO add your handling code here:
    DefaultListModel listModel = new DefaultListModel();
    String daInserire;
    listModel.addElement(listaRosa.getSelectedValue());
    listModel.removeElement(listaRosa.getSelectedValue());
    listaTitolari.setModel(listModel);
} 

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-05-20T10:01:31+00:00Added an answer on May 20, 2026 at 10:01 am

    The problem is

    listModel.addElement(listaRosa.getSelectedValue());
    listModel.removeElement(listaRosa.getSelectedValue());
    

    you may be adding an element and immediatly removing it since both add and remove operations are on the same listModel.

    Try

    private void aggiungiTitolareButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    
        DefaultListModel lm2 = (DefaultListModel) listaTitolari.getModel();
        DefaultListModel lm1  = (DefaultListModel) listaRosa.getModel();
        if(lm2 == null)
        {
            lm2 = new DefaultListModel();
            listaTitolari.setModel(lm2);
        }
        lm2.addElement(listaTitolari.getSelectedValue());
        lm1.removeElement(listaTitolari.getSelectedValue());        
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Userform that lets the user pick a worksheet from a
Can grep pick lines if at least one element from a list of words
I have to pick an element from an ascending array. Smaller elements are considered
I have a function to pick out lumps from a list of strings and
I have a picker view that has a list of numbers to pick from.
Possible Duplicate: Pick random property from a Javascript object suppose I have a javascript
have been away from Android for more than a year, trying to pick up
I have an array like $keywords = array('apple'=>10,'orange'=>2,'grape'=>12); I want to randomly pick one
I have a workflow that contains a Pick activity. Each PickBranch is triggered by
Just random pick some methods that I have found: sforce.connection.describeSObject sforce.SearchResult sforce.sObject sforce.LeadConvert() sforce.connection.create()

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.