Forum Stats

  • 3,768,738 Users
  • 2,252,843 Discussions
  • 7,874,704 Comments

Discussions

Prob with Removing Elements from JTable

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Sep 3, 2016 2:12PM in New To Java

Hi,

I am using a vector to store vales in the Jtable. I am storing 3 rows in the table. I have written a clean button handler to remove values from the table. Its code is:

rowOne.removeAllElements();
rowTwo.removeAllElements();
rowThree.removeAllElements();
//model.fireTableDataChanged();
model.removeRow(0);-----> exception here

But when i press this button, i am getting exception:


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at javax.swing.JTable.getValueAt(JTable.java:2717)
at javax.swing.JTable.prepareRenderer(JTable.java:5706)
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:683)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:580)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:364)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:275)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

where rowOne, rowTwo and rowThree are vectors.

Some body please guide me how to solve this problem.

Zulfi.

Answers

  • Unknown
    edited Sep 1, 2016 10:53PM
    I am using a vector to store vales in the Jtable. I am storing 3 rows in the table. I have written a clean button handler to remove values from the table. Its code is:

    Some body please guide me how to solve this problem.

    Having reviewed your many threads about basic Java functionality I think you might benefit by reading and trying the MANY trails and working example code in The Java Tutorials

    https://docs.oracle.com/javase/tutorial/uiswing/components/table.html

    There are trails that cover ALL of the basic functionality (the one above is for tables). The trails include working example code that show explain the functionality and show you how to code it.

  • eudriscabrera-JavaNet
    eudriscabrera-JavaNet Member Posts: 214 Bronze Badge
    edited Sep 1, 2016 8:26PM

    The removeAllElements method, remove all elements of your table. When you click the button, the JTable doesn't have any elements. java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 means, you are trying to remove an index that is not present in the Array.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Sep 2, 2016 3:03PM

    Hi,

    Thanks for your reply.

    I have modified:

    rowOne.removeAllElements();

            rowTwo.removeAllElements();

            rowThree.removeAllElements();

            //model.fireTableDataChanged();

            Object obj=model.getValueAt(0, 0);

            model.removeRow(0);

    I am using the debugger to check the value of obj or the expression :

    Object obj=model.getValueAt(0, 0);

    But i am not getting any thing. I am seeing the stuff in my table. How can i get rid of the stuff from table view.

    Some body please guide me.

    Zulfi.

  • eudriscabrera-JavaNet
    eudriscabrera-JavaNet Member Posts: 214 Bronze Badge
    edited Sep 2, 2016 5:10PM
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Sep 3, 2016 2:12PM

    Hi my friend, may be you are right but i dont find that it uses DefaultTableModel. I am also using Vector so its not of any help to me. Any way i thank you for taking interest in my problem. So i would tell you more about my problem:

    I am using NetBeans. My application class contains following code:

    public class FigureInheritanceJFrame extends javax.swing.JFrame {

        DefaultTableModel model = new DefaultTableModel(new Object[]{"FigureName","Dimension 1","Dimension 2", "Dimension 3", "Dimension 4", "Area"},0  );

        //final DefaultTableModel model=null;

        //model = (DefaultTableModel) figTable.getModel();

        Vector<String> rowOne = new Vector<String>();

        Vector<String> rowTwo = new Vector<String>();

        Vector<String> rowThree = new Vector<String>();

       

        String cBoBoxItem="Rectangle";

        private DefaultComboBoxModel modelC;

        /**

         * Creates new form FigureInheritanceJFrame

         */

        public FigureInheritanceJFrame() {//Application Class Constrcutor populating comboBox

           

            initComponents();

            modelC = new DefaultComboBoxModel ( );

           

    modelC.addElement("Rectangle" );

    modelC.addElement("Circle" );

    modelC.addElement("Triangle" );

            cBoBox.setModel(modelC);

           

        }

    //Button Handler code to insert the data into table

    private void btnADTActionPerformed(java.awt.event.ActionEvent evt) {                                      

            // TODO add your handling code here:

           double areaC1=0, areaC2 =0, areaC3=0;

           double areaR1=0, areaR2= 0, areaR3=0;

           double areaT1=0, areaT2= 0, areaT3=0;

    JOptionPane.showMessageDialog(null," "+ cBoBoxItem );

           int result1=-1; int result2=-1; int result3=-1;

            result1 = cBoBoxItem.compareTo("Circle");

            result2 = cBoBoxItem.compareTo("Rectangle");

            result3 = cBoBoxItem.compareTo("Triangle");

    JOptionPane.showMessageDialog(null," "+ cBoBoxItem + " res1= "+ result1 + " res2 "+ result2 + " res3= "+ result3);

           if(result1==0){

               /*rowOne.remove(0);

               rowTwo.remove(0);

               rowThree.remove(0);

               model.getDataVector().removeAllElements();

    model.fireTableDataChanged();*/

              

               Circle circle1 = new Circle(10);

               areaC1= circle1.area();

               Circle circle2 = new Circle(11);

               areaC2= circle2.area();

               Circle circle3 = new Circle(12);

               areaC3= circle3.area();

    rowOne.add("Circle 1");

               rowOne.add(" " + circle1.getDim1());

               rowOne.add(" ");

               rowOne.add(" ");

               rowOne.add(" ");

               rowOne.add(" " + areaC1);

               model.addRow(rowOne);

    figTable.setModel(model);

    rowTwo.add("Circle 2");

               rowTwo.add(" " + circle2.getDim1());

               rowTwo.add(" ");

               rowTwo.add(" ");

               rowTwo.add(" ");

               rowTwo.add(" " + areaC2);

               model.addRow(rowTwo);

    figTable.setModel(model);

    rowThree.add("Circle 3");

               rowThree.add(" " + circle1.getDim1());

               rowThree.add(" ");

               rowThree.add(" ");

               rowThree.add(" ");

               rowThree.add(" " + areaC1);

    model.addRow(rowThree);

    figTable.setModel(model);

              

               /*rowOne.remove(0);

               rowTwo.remove(0);

               rowThree.remove(0);*/

               result1=-1;

           }

           else if (result2==0){

        //code to add rectangle data into table

    }

    else if(result3==0){

    //code to add triangle data into table

    }

    Then there is comboBox handler which retrieves the item selected from ComboBox:

    private void cBoBoxActionPerformed(java.awt.event.ActionEvent evt) {                                      

            // TODO add your handling code here:

            cBoBoxItem = cBoBox.getSelectedItem().toString();

        } 

    Following is the code for clear button handler:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

            // TODO add your handling code here:

    rowOne.removeAllElements();

            rowTwo.removeAllElements();

    rowThree.removeAllElements();

    //model.fireTableDataChanged();

            Object obj=model.getValueAt(0, 0);

            model.removeRow(0);

            //model.removeRow(1);

            //model.removeRow(2);

    //figTable.setModel(model);*/

           

        }       

    Okay now I would tell you my problem. My comboBox contain 3 items: Rectangle, Triangle & Circle. If I select circle then it would store data of 3 Circle objects in the table. It works fine. Now I select Rectangle then instead of storing Rectangle data in the table, it stores Circle data into the table again. This is problem. I think because I am not clearing the Vector (maybe). So I wrote the code for clear button which I would call after storing data of any figure in the table so that Vector becomes empty & then it wont store redundant data in the data i.e the data of previously selected item from comboBox into the table . That is first I would select circle from ComboBox. Then I would press add button to store circle data in combo Box & then I would press clear & I would store the data of any other figure like Triangle or Rectangle so that the table should not be populated with redundant data.

    This is my problem that my program is storing the data of first selected figure in the table again and again even if select a different figure from combobox.

    Some body please guide me about the clear button.

    Zulfi.

This discussion has been closed.