I have been adding buttons to my GUI to manipulate list data.
I added a Delete button this morning in case I decide I no longer needed a particular element.
I am now working on a modify button, in case I want to keep the element, but only modify a field or two.
This seemed easy after my Delete, but now I am having issues with it.
Is there a listModel command out there that I am unfamiliar with that will help me with this?
Right now I have modify set up exactly like delete, just to put architecture in place without creating an error.
Any tips on what I should change to make it work correctly? Here is entire class. - import java.util.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
import javax.swing.event.*;
-
import java.text.*;
-
import java.lang.*;
-
import javax.swing.JFrame;
-
-
public class Inventory2 extends JFrame
-
{
-
private JLabel cdNameLabel; // name label
-
private JLabel artistLabel; // item number label
-
private JLabel nstockLabel; // units in stock label
-
private JLabel priceLabel; // price each label
-
private JLabel itemLabel; // item number label
-
private JTextField cdNameField; // name display
-
private JTextField artistField; // artist display
-
private JFormattedTextField nstockField; // units in stock display
-
private JFormattedTextField priceField; // price each display
-
private JTextField itemField; // item number display
-
private NumberFormat nstockFormat; // format field and parse numbers
-
private NumberFormat priceFormat; // format field and parse numbers
-
private JButton btnAdd; // first button
-
private JButton btnPrev; // previous button
-
private JButton btnNext; // next button
-
private JButton btnDel; // last button
-
private JButton btnFirst; // first button
-
private JButton btnLast; // last button
-
private JButton btnModify; // modify button
-
private JButton btnSave; // save button
-
private JButton btnSearch; // search button
-
private JPanel buttonJPanel; // JPanle to hold buttons
-
private JPanel fieldJPanel; // JPanel to hold labels and displays
-
private JPanel fontJPanel; // JPanel to display logo
-
private int currCD;
-
private double total = 0; // variable for total inventory
-
private JList Inventorylist; // JList to take place of old array
-
private DefaultListModel listModel;
-
private JScrollPane jScrollPanel;
-
private float Invtotal = .00f;
-
-
DecimalFormat formatter = new DecimalFormat("$0.00");
-
-
public Inventory2() // create class and method to perform GUI build
-
{
-
initComponents();
-
}
-
-
private void initComponents()
-
{
-
-
// create label names
-
cdNameLabel = new JLabel("CD Name:");
-
artistLabel = new JLabel("Artist:");
-
nstockLabel = new JLabel("In Stock:");
-
priceLabel = new JLabel("Each Item Cost:$");
-
itemLabel = new JLabel("Item Number:");
-
-
-
// initial fields
-
cdNameField = new JTextField(25);
-
cdNameField.setEditable(true);
-
artistField = new JTextField(15);
-
artistField.setEditable(true);
-
nstockField = new JFormattedTextField(nstockFormat);
-
nstockField.setEditable(true);
-
nstockField.setColumns(5);
-
priceField = new JFormattedTextField(priceFormat);
-
priceField.setEditable(true);
-
priceField.setColumns(5);
-
itemField = new JTextField(4);
-
itemField.setEditable(true);
-
-
// JList
-
jScrollPanel = new JScrollPane();
-
Inventorylist = new JList();
-
currCD = 0;
-
-
-
// buttons
-
btnAdd = new JButton();
-
btnNext = new JButton();
-
btnPrev = new JButton();
-
btnDel = new JButton();
-
btnLast = new JButton();
-
btnFirst = new JButton();
-
btnModify = new JButton();
-
btnSave = new JButton();
-
btnSearch = new JButton();
-
-
getContentPane().setLayout(new FlowLayout());
-
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
-
-
// add shapesJPanel to frame
-
ShapesJPanel shapesJPanel = new ShapesJPanel();
-
getContentPane().add( new ShapesJPanel() );
-
-
// place textFields and labels
-
-
//artist
-
artistLabel.setText("Artist");
-
getContentPane().add(artistLabel);
-
-
artistField.setMinimumSize(new Dimension(70,20));
-
artistField.setPreferredSize(new Dimension(70,20));
-
getContentPane().add(artistField);
-
-
// cd name
-
cdNameLabel.setText("CD Name");
-
getContentPane().add(cdNameLabel);
-
-
cdNameField.setMinimumSize(new Dimension(70,20));
-
cdNameField.setPreferredSize(new Dimension(70,20));
-
getContentPane().add(cdNameField);
-
-
// copies in stock
-
nstockLabel.setText("Copies In Stock");
-
getContentPane().add(nstockLabel);
-
-
nstockField.setMinimumSize(new Dimension(5,20));
-
nstockField.setPreferredSize(new Dimension(5,20));
-
getContentPane().add(nstockField);
-
-
//price of cd
-
priceLabel.setText("Price");
-
getContentPane().add(priceLabel);
-
-
priceField.setMinimumSize(new Dimension(20,20));
-
priceField.setPreferredSize(new Dimension(20,20));
-
getContentPane().add(priceField);
-
-
//item number of cd
-
itemLabel.setText("Item Number");
-
getContentPane().add(itemLabel);
-
-
itemField.setMinimumSize(new Dimension(5,20));
-
itemField.setPreferredSize(new Dimension(5,20));
-
getContentPane().add(itemField);
-
-
-
-
// add listeners
-
-
btnAdd.setText("Add");
-
btnAdd.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnAddActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnAdd);
-
-
// PREVIOUS
-
btnPrev.setText("Previous");
-
btnPrev.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnPrevActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnPrev);
-
-
// NEXT
-
btnNext.setText("Next");
-
btnNext.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnNextActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnNext);
-
-
// SEARCH
-
btnSearch.setText("Search");
-
btnSearch.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnAddActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnSearch);
-
-
// FIRST
-
btnFirst.setText("First");
-
btnFirst.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnFirstActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnFirst);
-
-
// LAST
-
btnLast.setText("Last");
-
btnLast.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnLastActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnLast);
-
-
// MODIFY
-
btnModify.setText("Modify");
-
btnModify.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnModifyActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnModify);
-
-
// SAVE
-
btnSave.setText("Save");
-
btnSave.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnAddActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnSave);
-
-
// DELETE
-
btnDel.setText("Delete");
-
btnDel.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnDeleteActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnDel);
-
-
-
// new Jlist model
-
listModel = new DefaultListModel();
-
Inventorylist.setModel(listModel);
-
-
jScrollPanel.setViewportView(Inventorylist);
-
-
getContentPane().add(jScrollPanel);
-
-
pack();
-
}// close
-
-
-
private void btnAddActionPerformed(ActionEvent evt)
-
{
-
// Create cd to add
-
CdwArtist newCD = new CdwArtist();
-
newCD.setArtist(artistField.getText());
-
newCD.setName(cdNameField.getText());
-
newCD.setItemno(Integer.parseInt(itemField.getText()));
-
newCD.setNstock(Integer.parseInt(nstockField.getText()));
-
newCD.setPrice(Float.parseFloat(priceField.getText()));
-
-
// Add cd to list
-
listModel.addElement(newCD);
-
currCD = listModel.size()-1; // sets currCD to added index
-
-
-
// Clear the text fields after add
-
artistField.setText(null);
-
cdNameField.setText(null);
-
itemField.setText(null);
-
nstockField.setText(null);
-
priceField.setText(null);
-
-
}// end ADD
-
-
private void btnPrevActionPerformed(ActionEvent evt)
-
{
-
// Grab Previous cd
-
if (--currCD<0) currCD = listModel.size()-1;
-
CdwArtist newCD = (CdwArtist) listModel.get( currCD );
-
-
-
artistField.setText(newCD.getArtist());
-
cdNameField.setText(newCD.getName());
-
itemField.setText(String.valueOf(newCD.getItemno()));
-
nstockField.setText(String.valueOf(newCD.getNstock()));
-
priceField.setText(formatter.format(newCD.getPrice()));
-
-
-
}// end PREV
-
-
private void btnNextActionPerformed(ActionEvent evt)
-
{
-
// Grab Next cd
-
if (++currCD >= listModel.size()) currCD= 0;
-
CdwArtist newCD = (CdwArtist) listModel.get( currCD );
-
-
-
artistField.setText(newCD.getArtist());
-
cdNameField.setText(newCD.getName());
-
itemField.setText(String.valueOf(newCD.getItemno()));
-
nstockField.setText(String.valueOf(newCD.getNstock()));
-
priceField.setText(formatter.format(newCD.getPrice()));
-
-
-
}// end NEXT
-
-
-
private void btnFirstActionPerformed(ActionEvent evt)
-
{
-
// Grab First cd
-
CdwArtist newCD = (CdwArtist) listModel.get(0);
-
-
-
artistField.setText(newCD.getArtist());
-
cdNameField.setText(newCD.getName());
-
itemField.setText(String.valueOf(newCD.getItemno()));
-
nstockField.setText(String.valueOf(newCD.getNstock()));
-
priceField.setText(formatter.format(newCD.getPrice()));
-
-
-
}// end FIRST
-
-
-
private void btnLastActionPerformed(ActionEvent evt)
-
{
-
// Grab Last cd
-
CdwArtist newCD = (CdwArtist) listModel.lastElement();
-
-
-
artistField.setText(newCD.getArtist());
-
cdNameField.setText(newCD.getName());
-
itemField.setText(String.valueOf(newCD.getItemno()));
-
nstockField.setText(String.valueOf(newCD.getNstock()));
-
priceField.setText(formatter.format(newCD.getPrice()));
-
-
-
}// end LAST
-
-
private void btnDeleteActionPerformed(ActionEvent evt)
-
{
-
// Delete cd
-
listModel.remove(currCD);
-
-
-
// Clear the text fields after delete
-
artistField.setText(null);
-
cdNameField.setText(null);
-
itemField.setText(null);
-
nstockField.setText(null);
-
priceField.setText(null);
-
-
-
}// end DELETE
-
-
private void btnModifyActionPerformed(ActionEvent evt)
-
{
-
// Modify cd
-
listModel.remove(currCD);
-
-
-
// Clear the text fields after delete
-
artistField.setText(null);
-
cdNameField.setText(null);
-
itemField.setText(null);
-
nstockField.setText(null);
-
priceField.setText(null);
-
-
-
}// end Modify
-
-
// run it
-
public static void main(String args[])
-
{
-
//JFrame frame = new JFrame( "CD Inventory Logo" );
-
//frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
-
// create ShapesJPanel
-
//ShapesJPanel shapesJPanel = new ShapesJPanel();
-
//frame.add( shapesJPanel ); // add shapesJPanel to frame
-
//frame.setSize( 125, 200 ); // set frame size
-
//frame.setVisible( true ); // display frame
-
java.awt.EventQueue.invokeLater(new Runnable()
-
{
-
public void run()
-
{
-
new Inventory2().setVisible(true);
-
}
-
});
-
}
-
-
} // close class
23 1980 JosAH 11,448
Recognized Expert MVP
You do keep track of a 'currCD' all the time, so why don't you just delete the
current 'currCD' and insert a new one?
kind regards,
Jos
I have not tested your code to see what you're talking about, but you should model your modify around your add rather than your delete. The difference with add should be that with modify you have a product to display first and when a save is done, you don't create a new product but simply override an existing one.
You do keep track of a 'currCD' all the time, so why don't you just delete the
current 'currCD' and insert a new one?
kind regards,
Jos
I haven't looked at the code but I would think they have a unique key for each product that they want to mantain after an edit.
JosAH 11,448
Recognized Expert MVP
I haven't looked at the code but I would think they have a unique key for each product that they want to mantain after an edit.
No he hasn't; check out the btnAddActionPer formed() logic: it just adds a new
CD to the list no matter what. A delete/insert pair would do fine for an update
here.
kind regards,
Jos
No he hasn't; check out the btnAddActionPer formed() logic: it just adds a new
CD to the list no matter what. A delete/insert pair would do fine for an update
here.
kind regards,
Jos
I just thought even if he had some key, he could then create the new one using that old key anyway ...
That is on the horizon ... sort of.
I will be changing the itemno field to stay in order. For example,
if I have 10 cds, they should be labeled 1-10 in order.
if we delete # 8, the remaining 9 should reorder accordingly ... if we add one, it should automatically take on the next number in line.
I do not think that will affect this current task at all though.
like listmodel.remov e, listmodel.add, listmodel.firss t and all that, I was hoping for a quick listmode.modify ... but if it does not exist I am willing to do a quick delete add.
this is what I have now: - private void btnModifyActionPerformed(ActionEvent evt)
-
{
-
// Modify cd
-
//listModel.addElement(newCD);
-
CdwArtist newCD = (CdwArtist) listModel.get( currCD );
-
newCD.setArtist(artistField.getText());
-
newCD.setName(cdNameField.getText());
-
newCD.setItemno(Integer.parseInt(itemField.getText()));
-
newCD.setNstock(Integer.parseInt(nstockField.getText()));
-
newCD.setPrice(Float.parseFloat(priceField.getText()));
-
-
-
// Clear the text fields after Modify
-
artistField.setText(null);
-
cdNameField.setText(null);
-
itemField.setText(null);
-
nstockField.setText(null);
-
priceField.setText(null);
-
-
-
}// end Modify
I have tried to Delete current, and then Add new cd but it is not working.
Can you see my problem? Logic being, delete whatever cd I am currently looking at, take what is in the fields at that time (modified data) and create a new element. After new element is added, clear fields. - // Modify cd
-
listModel.remove(currCD);
-
-
// Create cd to add
-
CdwArtist newCD = new CdwArtist();
-
newCD.setArtist(artistField.getText());
-
newCD.setName(cdNameField.getText());
-
newCD.setItemno(Integer.parseInt(itemField.getText()));
-
newCD.setNstock(Integer.parseInt(nstockField.getText()));
-
newCD.setPrice(Float.parseFloat(priceField.getText()));
-
-
// Add cd to list
-
listModel.addElement(newCD);
-
currCD = listModel.size()-1; // sets currCD to added index
-
-
-
// Clear the text fields after add
-
artistField.setText(null);
-
cdNameField.setText(null);
-
itemField.setText(null);
-
nstockField.setText(null);
-
priceField.setText(null);
JosAH 11,448
Recognized Expert MVP
I have tried to Delete current, and then Add new cd but it is not working.
You have to elaborate a bit on what 'not working' means. Is the old CD gone?
Isn't the new CD added? Adre they both there in the list? Are none of the two
present in the list?
kind regards,
Jos
You are correct, I am sorry. (new to this)
When I hit Modify ..
The last entered cd into my list is deleted, NOT the one I have in the fields ... then I get these errors - ---jGRASP exec: java Inventory2
-
-
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "$1.00"
-
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
-
at java.lang.Float.parseFloat(Float.java:422)
-
at Inventory2.btnModifyActionPerformed(Inventory2.java:374)
-
at Inventory2.access$500(Inventory2.java:11)
-
at Inventory2$7.actionPerformed(Inventory2.java:219)
-
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
-
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
-
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
-
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
-
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
-
at java.awt.Component.processMouseEvent(Component.java:6038)
-
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
-
at java.awt.Component.processEvent(Component.java:5803)
-
at java.awt.Container.processEvent(Container.java:2058)
-
at java.awt.Component.dispatchEventImpl(Component.java:4410)
-
at java.awt.Container.dispatchEventImpl(Container.java:2116)
-
at java.awt.Component.dispatchEvent(Component.java:4240)
-
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
-
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
-
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
-
at java.awt.Container.dispatchEventImpl(Container.java:2102)
-
at java.awt.Window.dispatchEventImpl(Window.java:2429)
-
at java.awt.Component.dispatchEvent(Component.java:4240)
-
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
-
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
-
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
-
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
-
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
-
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
-
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Franco Fellico' |
last post by:
Hi.
Suppose to have read and displayed (using PHP) a group of row of a DB
table on a dinamyc table on a HTML/PHP page.
The number of row displayed could be from 1 to n. Each row contains
informations and also a cell with a unique ID of the element.
I would like to show at the beginning of each line of the table a
couple of cells containing two different icons (one for delete and one
|
by: Charles Sullivan |
last post by:
I'm working on a program which has a "tree" of command line arguments,
i.e.,
myprogram level1 ]]
such that there can be more than one level2 argument for each level1
argument and more than one level3 argument for each level2 argument, etc.
Suppose I code it similar to this fragment:
int main( int argc, char *argv )
{
|
by: baumann.Pan |
last post by:
when define
char *p = " can not modify";
p ='b' ;is not allowed,
but if you declare p as
char p = "can modify";
p = 'b'; is ok?
why?
|
by: Michael B Allen |
last post by:
Is it legit to modify static data like the following code?
#include <stdlib.h>
#include <stdio.h>
struct tbl {
int i;
char *s;
};
|
by: Martin Bischoff |
last post by:
Hi,
is it possible to modify the values of a SqlDataSource's select parameters
in the code behind before the select command is executed?
Example:
I have an SqlDataSource with a ControlParameter
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:XYZ %>"
| |
by: Maxwell2006 |
last post by:
Hi,
When I run a web service project, ASP.NET shows me a default web method
invoke page.
How can I disable/modify the default test (or method invoke) page of the
ASP.NET web services?
Thank you,
Max
|
by: Bob |
last post by:
Hi,
I have a list of widgets.
I want to iterate through the selected items collection and modify one of
the widgets properties.
I tried
foreach(widget w in lst.SelectedItems)
w.MyProperty = something;
but I get a message saying "Cannot modify members of w because it is a
foreach iteration variable"
What is the point of iteration if you can't modify the objects?
|
by: Kiran |
last post by:
Hi all,
What I am trying to do is to pass a pointer to the first element of
an array to a function, modify it in that function, and then print out
the values of the array (which has been modified by the function) in
main. However, I am getting a segmentation fault. Here is the code:
(Please note, the size is fixed in this code, but in my code where I am
actually going to use this, the size of the array is not known until
you get to the...
|
by: TimEl |
last post by:
Hi.
Using Perl, I want to modify data in an XML file and print out the entire modified file, not just the elements I modify. In CPAN I have found that XPath allows me to pinpoint the elements that I want to modify. But all of the code examples that I have seen assume that I want to assign the targeted elements to variables, modify each element, and then print only the modified elements out to a file. For example, this code is found at...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |