?
Solved

jtable: add empty row and delete row with values

Posted on 2003-03-29
7
Medium Priority
?
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

771 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