Java: How can I check an observable list in Java to make sure it contains at least one value before...












0















I am fairly new at Java, doing a class project where I need to make a user interface using Java FXML for a company inventory system and then make it workable. I have parts and products that are added, deleted, modified, etc. Each product has a variety of parts and when you modify the product you can add/delete parts. There must be at least one part in the product at all time.



I have a table that represents all of the parts in a particular product, and I need to modify it to where it will not delete a part if it's the only part in the observable list for that product.



Currently I'm using the below to delete the parts which works fine, but obviously does not stop me from fully deleting. I was thinking that using the is.Empty method might help because if it's empty, I can pop up an error saying 'you must have at least one part'. However I'm having trouble implementing that. I was also thinking a while or if/then statement could be useful, because I could just put the segment for deleting inside the while or if/then loop with the condition the observable list isn't empty and the error in the case it is, but I'm not totally sure how I would implement that either and haven't found any resources within my textbooks. I was hoping for a pointer in the right direction, or advice if I'm just completely looking at it the wrong way.



selectedProduct.getAssociatedPart().remove(partsinproduct.getSelectionModel().getSelectedItem());









share|improve this question



























    0















    I am fairly new at Java, doing a class project where I need to make a user interface using Java FXML for a company inventory system and then make it workable. I have parts and products that are added, deleted, modified, etc. Each product has a variety of parts and when you modify the product you can add/delete parts. There must be at least one part in the product at all time.



    I have a table that represents all of the parts in a particular product, and I need to modify it to where it will not delete a part if it's the only part in the observable list for that product.



    Currently I'm using the below to delete the parts which works fine, but obviously does not stop me from fully deleting. I was thinking that using the is.Empty method might help because if it's empty, I can pop up an error saying 'you must have at least one part'. However I'm having trouble implementing that. I was also thinking a while or if/then statement could be useful, because I could just put the segment for deleting inside the while or if/then loop with the condition the observable list isn't empty and the error in the case it is, but I'm not totally sure how I would implement that either and haven't found any resources within my textbooks. I was hoping for a pointer in the right direction, or advice if I'm just completely looking at it the wrong way.



    selectedProduct.getAssociatedPart().remove(partsinproduct.getSelectionModel().getSelectedItem());









    share|improve this question

























      0












      0








      0








      I am fairly new at Java, doing a class project where I need to make a user interface using Java FXML for a company inventory system and then make it workable. I have parts and products that are added, deleted, modified, etc. Each product has a variety of parts and when you modify the product you can add/delete parts. There must be at least one part in the product at all time.



      I have a table that represents all of the parts in a particular product, and I need to modify it to where it will not delete a part if it's the only part in the observable list for that product.



      Currently I'm using the below to delete the parts which works fine, but obviously does not stop me from fully deleting. I was thinking that using the is.Empty method might help because if it's empty, I can pop up an error saying 'you must have at least one part'. However I'm having trouble implementing that. I was also thinking a while or if/then statement could be useful, because I could just put the segment for deleting inside the while or if/then loop with the condition the observable list isn't empty and the error in the case it is, but I'm not totally sure how I would implement that either and haven't found any resources within my textbooks. I was hoping for a pointer in the right direction, or advice if I'm just completely looking at it the wrong way.



      selectedProduct.getAssociatedPart().remove(partsinproduct.getSelectionModel().getSelectedItem());









      share|improve this question














      I am fairly new at Java, doing a class project where I need to make a user interface using Java FXML for a company inventory system and then make it workable. I have parts and products that are added, deleted, modified, etc. Each product has a variety of parts and when you modify the product you can add/delete parts. There must be at least one part in the product at all time.



      I have a table that represents all of the parts in a particular product, and I need to modify it to where it will not delete a part if it's the only part in the observable list for that product.



      Currently I'm using the below to delete the parts which works fine, but obviously does not stop me from fully deleting. I was thinking that using the is.Empty method might help because if it's empty, I can pop up an error saying 'you must have at least one part'. However I'm having trouble implementing that. I was also thinking a while or if/then statement could be useful, because I could just put the segment for deleting inside the while or if/then loop with the condition the observable list isn't empty and the error in the case it is, but I'm not totally sure how I would implement that either and haven't found any resources within my textbooks. I was hoping for a pointer in the right direction, or advice if I'm just completely looking at it the wrong way.



      selectedProduct.getAssociatedPart().remove(partsinproduct.getSelectionModel().getSelectedItem());






      java observablelist






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 19:49









      CassieCassie

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I think, a little bit simplified, the problem will look like



          list.remove(elem);



          ... but do it only, if elem is not the only element in the list.



          This is the code for doing it:



          if (list.size() > 1)
          list.remove(elem);


          When there is only one element in the list, you must not remove it. Without the if it would only be removed if it would match. In an empty list you cannot remove the element anyway. When there are two or more elements in the list, you are allowed to remove.






          share|improve this answer
























          • Thanks for your answer! Does list.size work for an observable list as well? I am trying to implement that using the name of my observable list or using observablelist.size but i'm getting errors in running it.

            – Cassie
            Nov 15 '18 at 21:51











          • It depends on what you want to check. I assumed that the list is an object of some class that implements the interface List. If does not depend on what is in the list. But be aware that the elements' equals() method. will be used to identifiy the element to be removed with list.remove. For the list of Observer in an Observable, use its methods countObserversand deleteObserver.

            – Donat
            Nov 15 '18 at 22:28













          • Thank you for your help! I'm going to read read up on the methods you mentioned as they aren't ones I'm super familiar with at this point and I am eager to understand them more.

            – Cassie
            Nov 15 '18 at 23:02











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326933%2fjava-how-can-i-check-an-observable-list-in-java-to-make-sure-it-contains-at-lea%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I think, a little bit simplified, the problem will look like



          list.remove(elem);



          ... but do it only, if elem is not the only element in the list.



          This is the code for doing it:



          if (list.size() > 1)
          list.remove(elem);


          When there is only one element in the list, you must not remove it. Without the if it would only be removed if it would match. In an empty list you cannot remove the element anyway. When there are two or more elements in the list, you are allowed to remove.






          share|improve this answer
























          • Thanks for your answer! Does list.size work for an observable list as well? I am trying to implement that using the name of my observable list or using observablelist.size but i'm getting errors in running it.

            – Cassie
            Nov 15 '18 at 21:51











          • It depends on what you want to check. I assumed that the list is an object of some class that implements the interface List. If does not depend on what is in the list. But be aware that the elements' equals() method. will be used to identifiy the element to be removed with list.remove. For the list of Observer in an Observable, use its methods countObserversand deleteObserver.

            – Donat
            Nov 15 '18 at 22:28













          • Thank you for your help! I'm going to read read up on the methods you mentioned as they aren't ones I'm super familiar with at this point and I am eager to understand them more.

            – Cassie
            Nov 15 '18 at 23:02
















          1














          I think, a little bit simplified, the problem will look like



          list.remove(elem);



          ... but do it only, if elem is not the only element in the list.



          This is the code for doing it:



          if (list.size() > 1)
          list.remove(elem);


          When there is only one element in the list, you must not remove it. Without the if it would only be removed if it would match. In an empty list you cannot remove the element anyway. When there are two or more elements in the list, you are allowed to remove.






          share|improve this answer
























          • Thanks for your answer! Does list.size work for an observable list as well? I am trying to implement that using the name of my observable list or using observablelist.size but i'm getting errors in running it.

            – Cassie
            Nov 15 '18 at 21:51











          • It depends on what you want to check. I assumed that the list is an object of some class that implements the interface List. If does not depend on what is in the list. But be aware that the elements' equals() method. will be used to identifiy the element to be removed with list.remove. For the list of Observer in an Observable, use its methods countObserversand deleteObserver.

            – Donat
            Nov 15 '18 at 22:28













          • Thank you for your help! I'm going to read read up on the methods you mentioned as they aren't ones I'm super familiar with at this point and I am eager to understand them more.

            – Cassie
            Nov 15 '18 at 23:02














          1












          1








          1







          I think, a little bit simplified, the problem will look like



          list.remove(elem);



          ... but do it only, if elem is not the only element in the list.



          This is the code for doing it:



          if (list.size() > 1)
          list.remove(elem);


          When there is only one element in the list, you must not remove it. Without the if it would only be removed if it would match. In an empty list you cannot remove the element anyway. When there are two or more elements in the list, you are allowed to remove.






          share|improve this answer













          I think, a little bit simplified, the problem will look like



          list.remove(elem);



          ... but do it only, if elem is not the only element in the list.



          This is the code for doing it:



          if (list.size() > 1)
          list.remove(elem);


          When there is only one element in the list, you must not remove it. Without the if it would only be removed if it would match. In an empty list you cannot remove the element anyway. When there are two or more elements in the list, you are allowed to remove.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 20:54









          DonatDonat

          848128




          848128













          • Thanks for your answer! Does list.size work for an observable list as well? I am trying to implement that using the name of my observable list or using observablelist.size but i'm getting errors in running it.

            – Cassie
            Nov 15 '18 at 21:51











          • It depends on what you want to check. I assumed that the list is an object of some class that implements the interface List. If does not depend on what is in the list. But be aware that the elements' equals() method. will be used to identifiy the element to be removed with list.remove. For the list of Observer in an Observable, use its methods countObserversand deleteObserver.

            – Donat
            Nov 15 '18 at 22:28













          • Thank you for your help! I'm going to read read up on the methods you mentioned as they aren't ones I'm super familiar with at this point and I am eager to understand them more.

            – Cassie
            Nov 15 '18 at 23:02



















          • Thanks for your answer! Does list.size work for an observable list as well? I am trying to implement that using the name of my observable list or using observablelist.size but i'm getting errors in running it.

            – Cassie
            Nov 15 '18 at 21:51











          • It depends on what you want to check. I assumed that the list is an object of some class that implements the interface List. If does not depend on what is in the list. But be aware that the elements' equals() method. will be used to identifiy the element to be removed with list.remove. For the list of Observer in an Observable, use its methods countObserversand deleteObserver.

            – Donat
            Nov 15 '18 at 22:28













          • Thank you for your help! I'm going to read read up on the methods you mentioned as they aren't ones I'm super familiar with at this point and I am eager to understand them more.

            – Cassie
            Nov 15 '18 at 23:02

















          Thanks for your answer! Does list.size work for an observable list as well? I am trying to implement that using the name of my observable list or using observablelist.size but i'm getting errors in running it.

          – Cassie
          Nov 15 '18 at 21:51





          Thanks for your answer! Does list.size work for an observable list as well? I am trying to implement that using the name of my observable list or using observablelist.size but i'm getting errors in running it.

          – Cassie
          Nov 15 '18 at 21:51













          It depends on what you want to check. I assumed that the list is an object of some class that implements the interface List. If does not depend on what is in the list. But be aware that the elements' equals() method. will be used to identifiy the element to be removed with list.remove. For the list of Observer in an Observable, use its methods countObserversand deleteObserver.

          – Donat
          Nov 15 '18 at 22:28







          It depends on what you want to check. I assumed that the list is an object of some class that implements the interface List. If does not depend on what is in the list. But be aware that the elements' equals() method. will be used to identifiy the element to be removed with list.remove. For the list of Observer in an Observable, use its methods countObserversand deleteObserver.

          – Donat
          Nov 15 '18 at 22:28















          Thank you for your help! I'm going to read read up on the methods you mentioned as they aren't ones I'm super familiar with at this point and I am eager to understand them more.

          – Cassie
          Nov 15 '18 at 23:02





          Thank you for your help! I'm going to read read up on the methods you mentioned as they aren't ones I'm super familiar with at this point and I am eager to understand them more.

          – Cassie
          Nov 15 '18 at 23:02




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326933%2fjava-how-can-i-check-an-observable-list-in-java-to-make-sure-it-contains-at-lea%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python