Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to stop adding duplicate rows in java JTable?

  1. #1
    Junior Member
    Join Date
    Jul 2022
    Thanked 0 Times in 0 Posts

    Default How to stop adding duplicate rows in java JTable?

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) /Button Click/

    DefaultTableModel dt=(DefaultTableModel) jTable1.getModel(); /Table Model /

    String tn=jname.getSelectedItem().toString();
    String jf=jfrom.getSelectedItem().toString();
    String jt=jto.getSelectedItem().toString();
    String jm=jtime.getText();

    String Data[]={tn,jf,jt,jm};

    This is my code for pressing ADD Button. when i press add the duplicate values are also added in new row. i want only new values to be stored in new row and want to show the error if the same values are inputted.

    Can someone kindly help me with this? will there be a loop or some function? Please Help!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to stop adding duplicate rows in java JTable?

    Are you asking: How to prevent a user from adding a duplicate value to the table when a button is pressed?
    One way would be to save the previously added values and compare them with the current values. If the same, skip adding them.

    Please edit your post and wrap your code with code tags:


    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Adding a new row automatically with jTable(maybe?) (WindowBuilder)
    By freefora11 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 15th, 2014, 11:44 AM
  2. AbstractTableModel - Adding rows
    By Drakey in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2013, 08:33 AM
  3. Adding column to JTable with JSlider determined header
    By oli_t in forum AWT / Java Swing
    Replies: 3
    Last Post: March 6th, 2013, 03:13 PM
  4. JButton, adding a Listener to stop and start?
    By spunkyk014 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 16th, 2012, 09:30 AM
  5. adding rows to coloumns. Netbeans
    By haygaurav in forum Java IDEs
    Replies: 1
    Last Post: April 1st, 2009, 03:16 PM

Tags for this Thread