Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to make Integer number increment by 1 in a JTextField?

How to make Integer number increment by 1 in a JTextField?

Scheduled Pinned Locked Moved Java
tutorialquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    I have a form in which I have made File no field which hold the Integer value like 1-2-3-4 and so on. I want that ,when I save a form which has file no 1 ,and then after when I want to save the next record it should remember the last file no and increment by 1. I have not coded yet anything because I don't know if it is possible?

    L 1 Reply Last reply
    0
    • C chdboy

      I have a form in which I have made File no field which hold the Integer value like 1-2-3-4 and so on. I want that ,when I save a form which has file no 1 ,and then after when I want to save the next record it should remember the last file no and increment by 1. I have not coded yet anything because I don't know if it is possible?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      chdboy wrote:

      I don't know if it is possible?

      It definitely is possible. You just need to use a variable to hold the value, and use that to display in the field. Every time you save a file, increment the value and refresh the displayed field.

      Veni, vidi, abiit domum

      C 2 Replies Last reply
      0
      • L Lost User

        chdboy wrote:

        I don't know if it is possible?

        It definitely is possible. You just need to use a variable to hold the value, and use that to display in the field. Every time you save a file, increment the value and refresh the displayed field.

        Veni, vidi, abiit domum

        C Offline
        C Offline
        chdboy
        wrote on last edited by
        #3

        thanks for the Idea I'm working on it. EDIT: I managed to do what I wanted BUT... It only happens till the application is open and I'm saving data one after one...as soon as I close to application and opens in again I have see the last file no and enter the next file no manually. with this code

        String filenostr = filenotxtfield.getText();
        statement.setString(1,filenostr);
        int fileno=Integer.parseInt(filenostr);

        fileno++;

        	   String convertno=String.valueOf(fileno);
        	    filenotxtfield.setText(convertno);
        	statement.executeUpdate();
        	
        	int rowsAffected = statement.executeUpdate();
        	if(rowsAffected > 0) 
        	{
        		   JOptionPane.showMessageDialog(null, "Data Inserted successfully!");
        		}
        		else
        		{
        		    JOptionPane.showMessageDialog(null, "Data is not Inserted!", "Not successfully", JOptionPane.ERROR\_MESSAGE);
        		}
        	statement.close();
        	con.close();
        

        Addition to that I'm getting error on line

        int rowsAffected = statement.executeUpdate();

        ERROR:

        com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK__Employer__6F09E6800F975522'. Cannot insert duplicate key in object 'dbo.Employer'.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
        at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)

        But as I see in my Management Studio that the data has been saved.but still getting this error ..why?

        1 Reply Last reply
        0
        • L Lost User

          chdboy wrote:

          I don't know if it is possible?

          It definitely is possible. You just need to use a variable to hold the value, and use that to display in the field. Every time you save a file, increment the value and refresh the displayed field.

          Veni, vidi, abiit domum

          C Offline
          C Offline
          chdboy
          wrote on last edited by
          #4

          I'm able to display the value after data is saved, but it is showing me wrong value with this code.

          int fileno1=Integer.parseInt(Result.getString(2));
          int increment = ++fileno1;
          String convertno1=String.valueOf(increment);
          filenotxtfield.setText(convertno1);

          If 1 and 2 file no already exists after entering 3rd number manually and then save it ,it is showing me 2 not 4.?

          L 1 Reply Last reply
          0
          • C chdboy

            I'm able to display the value after data is saved, but it is showing me wrong value with this code.

            int fileno1=Integer.parseInt(Result.getString(2));
            int increment = ++fileno1;
            String convertno1=String.valueOf(increment);
            filenotxtfield.setText(convertno1);

            If 1 and 2 file no already exists after entering 3rd number manually and then save it ,it is showing me 2 not 4.?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            That code looks fine as it stands, but you are not saving the updated count anywhere.

            Veni, vidi, abiit domum

            C 1 Reply Last reply
            0
            • L Lost User

              That code looks fine as it stands, but you are not saving the updated count anywhere.

              Veni, vidi, abiit domum

              C Offline
              C Offline
              chdboy
              wrote on last edited by
              #6

              Do I have to save it in the database and then retrieve it?

              int increment = ++fileno1;

              I have to save increment in the database?

              L 1 Reply Last reply
              0
              • C chdboy

                Do I have to save it in the database and then retrieve it?

                int increment = ++fileno1;

                I have to save increment in the database?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                It depends on how long you need the value, but you have to save it somewhere. If you only need the value for a single execution of the program, then just keep it in memory. If you want to continue where you left off with another separate execution, then you need to kepp it somewhere permanent, database, property etc.

                Veni, vidi, abiit domum

                C 1 Reply Last reply
                0
                • L Lost User

                  It depends on how long you need the value, but you have to save it somewhere. If you only need the value for a single execution of the program, then just keep it in memory. If you want to continue where you left off with another separate execution, then you need to kepp it somewhere permanent, database, property etc.

                  Veni, vidi, abiit domum

                  C Offline
                  C Offline
                  chdboy
                  wrote on last edited by
                  #8

                  ok right now I have not made is permanent,but I think I will save it in the database so when the application loads it will still remember the last file no ...thanks for the Idea. Right now I have done it with this code.

                  int fileno1=Integer.parseInt(Result.getString(2));
                  int increment = ++fileno1;
                  int increment2 = ++increment;
                  filenotxtfield.setText(Integer.toString(increment2));

                  I have used this code after inserting the data in the database.So now as you suggested I'm working on how to show the next file no upon application starts.

                  N 1 Reply Last reply
                  0
                  • C chdboy

                    ok right now I have not made is permanent,but I think I will save it in the database so when the application loads it will still remember the last file no ...thanks for the Idea. Right now I have done it with this code.

                    int fileno1=Integer.parseInt(Result.getString(2));
                    int increment = ++fileno1;
                    int increment2 = ++increment;
                    filenotxtfield.setText(Integer.toString(increment2));

                    I have used this code after inserting the data in the database.So now as you suggested I'm working on how to show the next file no upon application starts.

                    N Offline
                    N Offline
                    Nicholas Marty
                    wrote on last edited by
                    #9

                    I'm not sure if you're aware of this. But what this code does is essentially:

                    int fileno1=Integer.parseInt(Result.getString(2));
                    fileno1 = fileno1 + 1;
                    int increment = fileno1;
                    increment = increment + 1;
                    int increment2 = increment;
                    filenotxtfield.setText(Integer.toString(increment2));

                    if you just want to increment the value of fileno1 (and don't need those variables increment and increment2) by 2:

                    int fileno1=Integer.parseInt(Result.getString(2));
                    fileno1 = fileno1 + 2;
                    filenotxtfield.setText(Integer.toString(fileno1));

                    C 1 Reply Last reply
                    0
                    • N Nicholas Marty

                      I'm not sure if you're aware of this. But what this code does is essentially:

                      int fileno1=Integer.parseInt(Result.getString(2));
                      fileno1 = fileno1 + 1;
                      int increment = fileno1;
                      increment = increment + 1;
                      int increment2 = increment;
                      filenotxtfield.setText(Integer.toString(increment2));

                      if you just want to increment the value of fileno1 (and don't need those variables increment and increment2) by 2:

                      int fileno1=Integer.parseInt(Result.getString(2));
                      fileno1 = fileno1 + 2;
                      filenotxtfield.setText(Integer.toString(fileno1));

                      C Offline
                      C Offline
                      chdboy
                      wrote on last edited by
                      #10

                      Yes that didn't come to my mind...thank you :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups