Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

jtable: add empty row and delete row with values

Posted on 2003-03-29
7
Medium Priority
?
543 Views
Last Modified: 2011-10-03
i'm having a headache trying to add a new row as well as to delete row into my jtable.
here's part of my codes...(some of it are deleted bcos not relevant and to conserve space)

SalesApp.java
----------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class SalesApp extends JPanel {

      String rand;
      
  JButton New = new JButton(); //the button to add row
  JButton Edit = new JButton();
  JButton Cancel = new JButton(); //the button to delete row
  JButton Enter = new JButton();
  JButton Search = new JButton();
 
  NewTableModel model=new NewTableModel();
  JTable Table=new JTable(model);
 
  public SalesApp() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
  void jbInit() throws Exception {
    this.setSize(800,348);
    this.setBackground(Color.white);
    this.setLayout(borderLayout1);
    this.add(SalesTop,  BorderLayout.CENTER);
    this.add(SalesMenu,  BorderLayout.SOUTH);
   
    New.setText("New");
    New.setBounds(new Rectangle(8, 7, 76, 21));
    New.addActionListener(new SalesButton(this));
    Edit.setText("Edit");
    Edit.setBounds(new Rectangle(89, 7, 76, 21));
    Edit.addActionListener(new SalesButton(this));
    Cancel.setText("Cancel");
    Cancel.setBounds(new Rectangle(283, 7, 76, 21));
    Cancel.addActionListener(new SalesButton(this));
    Enter.setText("Enter");
    Enter.setBounds(new Rectangle(201, 7, 76, 21));
    Enter.addActionListener(new SalesButton(this));

    Table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    Table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    TableColumn column = null;
    for (int i = 0; i < 7; i++) {
    column = Table.getColumnModel().getColumn(i);
    if (i == 0)
            column.setPreferredWidth(50);
    else
            column.setPreferredWidth(120);
      }
  }

      class NewTableModel extends AbstractTableModel
      {
            String [] columnNames = {"No","Product ID",
            "Description","Quantity","Discount",
            "Price ( Per Unit )", "Amount"};
      
            Object[][] data = {
                  {"1","apara","stupid stuff","2","20","300","480"},
                  {"","","","","","",""},                        
                  };

            public int getRowCount()
            {
                  return data.length;
            }
            
            public int getColumnCount()
            {
                  return columnNames.length;
            }
            
            public String getColumnName(int col)
            {
                  return columnNames[col];
            }
            
            public Object getValueAt(int row, int col)
            {
                  return data[row][col];
            }
            
            public void setValueAt(Object value, int row, int col)
            {
                  data[row][col]=value;
                  fireTableCellUpdated(row,col);
            }
      }
      
      public String randomize()
      {
            return String.valueOf((int)(Math.random()*100000));
      }
}
----------------------------------------------------------------------------

SalesButton.java
----------------------------------------------------------------------------
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class SalesButton extends DBConnector implements ActionListener, ListSelectionListener
{
      private SalesApp sa;
      TableModel model;
      static int selectedRow;
      
      public SalesButton(SalesApp s1)
      {
            sa=s1;
      }
      
      public void actionPerformed(ActionEvent e)
      {
            if (e.getSource()==sa.New)
            {
                  try //here's where i try to add a new row
                  {
                  }

                  catch(Exception err)
                  {
                        System.out.println("Error: "+ err);
                  }
            }
            
            if (e.getSource()==sa.Cancel)
            {
                  try //here's where i try to delete a row
                  {
                  }

                  catch(Exception err)
                  {
                        System.out.println("Error: "+ err);
                  }
            }
      }
----------------------------------------------------------------------------

how do i do it?? i can't use another table model bcos i already have abstracttablemodel. thanx

gildan2020
0
Comment
Question by:gildan2020
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8230690
You'd save yourself a lot of trouble and work if you ditched your table model and just *used* a DefaultTableModel. This supports addition and deletion of rows out of the box.
0
 

Author Comment

by:gildan2020
ID: 8233108
unfortunately, i wish i could...
is there any codes that add and delete row for AbstractTableModel?? plz gimme the sample codes that might work with my current codes

thanx

gildan2020
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8233552
The best thing would be to look at the source code for DefaultTableModel. Failing that:

Use a collection for your data - not an array. The latter is inherently static. A vector of vectors would be a good idea as this is standard.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:gildan2020
ID: 8233880
can u give me some sample codes that are based on AbstractTableModel?

gildan2020
0
 

Author Comment

by:gildan2020
ID: 8313505
hey, i'm proposing to delete dis question since there hasn't been any post for nearly two weeks

if u hav any objections...plz do comment thanx

(on de instructions of the moderator, SpideyMod)
gildan2020
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8334922
PAQ'd and points refunded.

SpideyMod
Community Support Moderator @Experts Exchange
0
 

Author Comment

by:gildan2020
ID: 8354781
thanx spideymod

gildan2020
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question