my dog learned polymorphism*
The moose likes Beginning Java and the fly likes java unable to delete from array list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java unable to delete from array list" Watch "java unable to delete from array list" New topic
Author

java unable to delete from array list

Anu Preethi
Greenhorn

Joined: Dec 09, 2008
Posts: 3
Hi,

I am new to forums. This is my first forum post ever.
I am new to java and i am creating a desktop application.
I have a table in ManageJPanel and when i click on add button, it goe to addJPanel, i can add item and go back to Manageitem jpanel.To view the item, i highlight the selected item and click on View button. It will take me to viewjpanel. Here i can choose to remove the item by clicking the remove button.
All the items are in a class. Since there will be many items, there is a array list of the items created in item catalog class.

My proble: i am unable to remove the item from the table on the click on remove button.

manage jpanel code:

package UserInterface.Marketing;

import Business.Business;
import UserInterface.Util.*;
import Business.SystemAdmin.MarketList;
import java.util.ArrayList;
import javax.swing.table.*;
import Business.SystemAdmin.Market;
import Business.SystemAdmin.PersonDirectory;
import Business.SystemAdmin.UserAccount;

public class ManageMarketJPanel extends javax.swing.JPanel {
private MarketList marketListRefVar;
private Market selectedmarket;
private PersonDirectory pdRefVar;
private Business business;
private UserAccount uaRefVar;
private Market marketRefVar;

public ManageMarketJPanel(Business z, MarketList a, PersonDirectory b)
{
initComponents();
marketListRefVar = a;
pdRefVar= b;
business = z;
refresh();
}

private void refresh()
{
int rowcount = marketListTable.getRowCount();
int i = 0;
for(i = rowcount -1 ; i > 0; i--)
{
((DefaultTableModel)marketListTable.getModel()).removeRow(i);
}
ArrayList<Market> markets = marketListRefVar.getmarketList();
{
for(Market m: markets)
{
Object[] market_row = new Object[2];
market_row [0] = m;
market_row [1] = m.getincome();
((DefaultTableModel)marketListTable.getModel()).addRow(market_row);
}
}

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

TitleLabel = new javax.swing.JLabel();
jButton12 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
marketListTable = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();

setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

TitleLabel.setFont(new java.awt.Font("Lucida Grande", 1, 24));
TitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
TitleLabel.setText("Manage Markets");
add(TitleLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 20, 210, 40));

jButton12.setText("Back");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
add(jButton12, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 470, -1, -1));

marketListTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"Market Name", "Income"
}
) {
boolean[] canEdit = new boolean [] {
false, false
};

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(marketListTable);

add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 720, 350));

jButton1.setText("Add Market");
jButton1.setToolTipText("Create Business");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 470, 120, -1));

jButton3.setText("Refresh");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(750, 40, -1, -1));

jButton7.setText("View Detail");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(640, 470, -1, -1));
}// </editor-fold>

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int rr = marketListTable.getSelectionModel().getLeadSelectionIndex();
if (rr < 0 || rr >= marketListTable.getRowCount())
{return;}

selectedmarket = (Market) marketListTable.getValueAt(rr,0);

ViewMarketJPanel amjp = new ViewMarketJPanel(selectedmarket, marketListRefVar);
CardLayoutUtil.getCardLayoutJPanel().addAndMoveToNextJPanel("ViewMarketJPanel", amjp);
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: REFRESH
refresh();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: ADD

AddMarketJPanel amjp = new AddMarketJPanel(business, marketListRefVar);
CardLayoutUtil.getCardLayoutJPanel().addAndMoveToNextJPanel("AddMarketJPanel", amjp);
}

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: BACK BUTTON
CardLayoutUtil.getCardLayoutJPanel().removeAndMoveToPreviousJPanel(this);
}


// Variables declaration - do not modify
private javax.swing.JLabel TitleLabel;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable marketListTable;
// End of variables declaration

}

view jpanel code:
package UserInterface.Marketing;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import UserInterface.Util.*;
import Business.SystemAdmin.Market;
import Business.SystemAdmin.MarketList;
import Business.SystemAdmin.Person;
import Business.SystemAdmin.PersonDirectory;

public class ViewMarketJPanel extends javax.swing.JPanel {
private Market marketRefVar;
private MarketList marketListRefVar;
private PersonDirectory pdRefVar;

public ViewMarketJPanel(Market a, MarketList b)
{
initComponents();
marketRefVar = a;
marketListRefVar = b;
pdRefVar = new PersonDirectory();

//displayPersonByRole(Person.Role.CUSTOMER);

marketNameTextField.setText(marketRefVar.getmarketName());
incomeTextField.setText(marketRefVar.getincome());
descriptionList.setListData(marketRefVar.getdescription().toArray());
customerList.setListData(marketRefVar.getpersonInMarket().toArray());
}

// private void displayPersonByRole(Person.Role role)
// {
// ArrayList<Person> personList = pdRefVar.getPersonsByRole(role);
// personComboBox.removeAllItems();
// for (Person p : personList)
// {personComboBox.addItem(p);}
// }

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
marketNameTextField = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
incomeTextField = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
descriptionList = new javax.swing.JList();
RemoveMarket = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
customerList = new javax.swing.JList();

setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 24));
jLabel1.setText("Market Details");
add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 30, -1, -1));

jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13));
jLabel2.setText("Market Name:");
add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 110, -1, -1));

jButton2.setText("Back");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 400, -1, -1));

marketNameTextField.setBackground(new java.awt.Color(204, 204, 204));
marketNameTextField.setEditable(false);
add(marketNameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 110, 180, -1));

jLabel7.setFont(new java.awt.Font("Lucida Grande", 1, 13));
jLabel7.setText("Income: $");
add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 150, -1, -1));

incomeTextField.setBackground(new java.awt.Color(204, 204, 204));
incomeTextField.setEditable(false);
add(incomeTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 150, 90, -1));

jLabel8.setFont(new java.awt.Font("Lucida Grande", 1, 13));
jLabel8.setText("Description:");
add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 190, -1, 20));

jScrollPane1.setViewportView(descriptionList);

add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 200, 300, -1));

RemoveMarket.setText("Remove Market");
RemoveMarket.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RemoveMarketActionPerformed(evt);
}
});
add(RemoveMarket, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 400, -1, -1));

jButton5.setText("Update");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 400, -1, -1));

jScrollPane2.setViewportView(customerList);

add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 200, 210, -1));
}// </editor-fold>

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

UpdateMarketJPanel umjp = new UpdateMarketJPanel(marketRefVar);
CardLayoutUtil.getCardLayoutJPanel().addAndMoveToNextJPanel("UpdateMarketJPanel", umjp);
}

private void RemoveMarketActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null, "This market will be deleted from the list.");
ArrayList <Market> marketList = marketListRefVar.getmarketList();
System.out.println("after the array");
if (marketList.contains(marketRefVar))
marketList.remove(marketRefVar);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: CANCEL
CardLayoutUtil.getCardLayoutJPanel().removeAndMoveToPreviousJPanel(this);
}


// Variables declaration - do not modify
private javax.swing.JButton RemoveMarket;
private javax.swing.JList customerList;
private javax.swing.JList descriptionList;
private javax.swing.JTextField incomeTextField;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField marketNameTextField;
// End of variables declaration

}

on the click on remove, it wont go inot the code in emove button, no errors either.

can someone please guide me here.

Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
I am about to send you an important administrative private message; please act on it. CR
 
Don't get me started about those stupid light bulbs.
 
subject: java unable to delete from array list
 
Similar Threads
JTable Problem with netbeans
Pressing a button to open a new window
How to create hot key?
adding and manipulating elements of a CCombo
to print output in textarea