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
Jul 31 '07
23 1992 JosAH 11,448
Recognized Expert MVP
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 ...
That implies that the variable 'currCD' doesn't point to the CD your text fields
are showing. I'd suggest you always repopulate those text fields with the values
of the CD that 'currCD' is pointing at.
kind regards,
Jos
And that exception tells you a line number that you should check in your code. Remember that how you display your data is usually different from how you store it.
I see. Playing with my app, trying to debug I have discovered some kind of currCD problem.
My delete button deletes the last cd put in the list, not what I think should be the currCD.
When I hist FIRST for example, the first cd in the list populates my fields ... is that not set up to be my currCD?
The way I read it, it is. Why then would my Delete button not delete that element?
This confuses me.
JosAH 11,448
Recognized Expert MVP
I see. Playing with my app, trying to debug I have discovered some kind of currCD problem.
My delete button deletes the last cd put in the list, not what I think should be the currCD.
When I hist FIRST for example, the first cd in the list populates my fields ... is that not set up to be my currCD?
Yes it should but you didn't set currrCD= 0 in your 'first' method.
kind regards,
Jos
That makes perfect sense now doesn't it!?
Of course I should then have to set it for my LAST button as well, which I thought I did, but same problem.
Is this not the equivilent to the last element? Is listModel.size? - CdwArtist newCD = (CdwArtist) listModel.lastElement();
-
currCD = listModel.size();
-
-
-
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
JosAH 11,448
Recognized Expert MVP
That makes perfect sense now doesn't it!?
Of course I should then have to set it for my LAST button as well, which I thought I did, but same problem.
Is this not the equivilent to the last element? Is listModel.size? - CdwArtist newCD = (CdwArtist) listModel.lastElement();
-
currCD = listModel.size();
-
-
-
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
I'd make that listmodel.size( )-1 if I were you because that's the index of the last
CD in the list.
kind regards,
Jos
perfect. I think I should have known that.
Of course, this leaves me at my original problem.
When I pull in a cd to modify, it performs step one and deletes the current element, but goes all crazy without adding the new cd.
It looks as if it does not like the formatting of my fields, but I cannot figure out why.
This is exactly how I ADD cds to begin with, why would it not work this way?
Here are my buttons, so you can see what I have done.
And here are the erros. I think I know what they mean, I just do not understand why, or how to fix it. Exception in thread "AWT-EventQueue-0" java.lang.Numbe rFormatExceptio n: For input string: "$2.00"
at sun.misc.Floati ngDecimal.readJ avaFormatString (FloatingDecima l.java:1224)
at java.lang.Float .parseFloat(Flo at.java:422)
at Inventory2.btnM odifyActionPerf ormed(Inventory 2.java:374)
at Inventory2.acce ss$500(Inventor y2.java:11)
at Inventory2$7.ac tionPerformed(I nventory2.java: 218)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:1995)
at javax.swing.Abs tractButton$Han dler.actionPerf ormed(AbstractB utton.java:2318 )
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel.java: 387)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:242)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonListene r.java:236)
at java.awt.Compon ent.processMous eEvent(Componen t.java:6038)
at javax.swing.JCo mponent.process MouseEvent(JCom ponent.java:326 0)
at java.awt.Compon ent.processEven t(Component.jav a:5803)
at java.awt.Contai ner.processEven t(Container.jav a:2058)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:4410)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2116)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:4322)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3986)
at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3916)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2102)
at java.awt.Window .dispatchEventI mpl(Window.java :2429)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:599)
at java.awt.EventD ispatchThread.p umpOneEventForF ilters(EventDis patchThread.jav a:273)
at java.awt.EventD ispatchThread.p umpEventsForFil ter(EventDispat chThread.java:1 83)
at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThread.jav a:173)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:168)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:160)
at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:12 1)
I've already said something about that one.
JosAH 11,448
Recognized Expert MVP
java.lang.Numbe rFormatExceptio n: For input string: "$2.00"
Where does that dollar sign come from? Do you anticipate for that?
kind regards,
Jos
I know it has been commented on earlier in the thread, but I did not understand the solution.
I know the list does not like the information I am trying to pass to it.
The $ comes from the decimal formatter I have put in. That was the issue. I took the $ out of the formatter and just left it at 0.00, put the $ into the label instead, and all is well.
You guys are great.
Of course that was the simpler of my two problems. I have some debugging left to try on the Search button I am trying to implement.
Thanks again for your help.
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |