I do not even know how to correctly ask this question.
I have an item field in the code I am about to post. Simple intger meant to be an item number for a cd. The user enters this number.
Over the last week I have played with Add buttons, Del buttons, Modify and everything else and it brought to light a problem I want to address today.
I have always just used 1 for the first item, 2 for the second and so on ...
well after I am done will all the button nonsence the item numbers are all over the place.
How could I make it so that the item number field would be restructured and stay in ascending order no matter what was added or deleted?
If I delete cd number 4, then make 5 number 4, 6 number 5 and so on ... the next cd added should just take the next highest number ...
Does that make sense?
Here is my primary class. I can post the others if needed. - 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 logo = new ShapesJPanel();
-
logo.setPreferredSize(new Dimension(200,200));
-
getContentPane().add( logo );
-
-
// 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);
-
currCD = 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();
-
currCD = listModel.size()-1;
-
-
-
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);
-
-
// 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 Modify
-
-
// Search the Name Field
-
cdNameField.addCaretListener(new CaretListener()
-
{
-
public void caretUpdate(CaretEvent e)
-
{
-
JTextField f = (JtextField)e.getSource();
-
final String s = f.getText();
-
-
-
// run it
-
public static void main(String args[])
-
{
-
JFrame frame = new JFrame( "CD Inventory Logo" );
-
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
-
java.awt.EventQueue.invokeLater(new Runnable()
-
{
-
public void run()
-
{
-
new Inventory2().setVisible(true);
-
}
-
});
-
}
-
-
} // close class
21 2924 JosAH 11,448
Recognized Expert MVP
Don't allow the user to enter that (integer) key him/herself. Users are stupid
beings that make mistakes all the time. Have you had a look at the Bitset
class yet? I might come in handy.
kind regards,
Jos
I did. It is what I thought of useing, but the API just confuses me.
I do not yet understand how to use the API properly, so I have been scanning the internet for code examples of one that is actually being used, but I cannot find a model to try and use to set mine up.
JosAH 11,448
Recognized Expert MVP
I did. It is what I thought of useing, but the API just confuses me.
It just allows you to set bits or reset them; either single bits or a range of them.
You can also go fancy and 'or', 'and', 'xor' and whatever with two entire bit sets.
You just need the simple stuff: get the first bit that wasn't set to true and set
a bit to true or false. Compare it with a boolean[] array except that a BitSet is
more clever than that.
kind regards,
Jos
wow. Now I feel dumb. I know nothing about arrays, and boolean is just a term to me at this point for true or false.
I understand the basic concept, but have no idea how to code it.
Am I reformatting the field? Am I creating a whole new field? Am I creating this bit and then applying it to my itemno field?
I am lost.
JosAH 11,448
Recognized Expert MVP
wow. Now I feel dumb. I know nothing about arrays, and boolean is just a term to me at this point for true or false.
I understand the basic concept, but have no idea how to code it.
Am I reformatting the field? Am I creating a whole new field? Am I creating this bit and then applying it to my itemno field?
I am lost.
Read all about that BitSet class and judge for yourself whether you can use it
or not; maybe my suggestion was all wrong. You won't know until you've understood
what that set is all about ;-)
kind regards,
Jos
I have read it, and I do think it will help, but reading about it does not show me how to code it in to my existing program. That is what I do not understand.
I understand that you do not wish to just hand out code,
but could you show code examples of what I might try? I have never seen this implemented anywhere, it is hard for me to see how I would put it in action with what I already have.
JosAH 11,448
Recognized Expert MVP
I have read it, and I do think it will help, but reading about it does not show me how to code it in to my existing program. That is what I do not understand.
I understand that you do not wish to just hand out code,
but could you show code examples of what I might try? I have never seen this implemented anywhere, it is hard for me to see how I would put it in action with what I already have.
There's a saying: "code to the interface, not to the implementation" . You want a
'thing' that gives you a unique integer number when you ask for it and you can
return a number (previously given to you by that 'thing') telling it that it can be
used again in the future if you ask for another number again.
Those numbers are your primary keys for your CD collection.
Here goes: -
public interface PrimaryKeyGenerator {
-
public int give();
-
public void take();
-
}
-
If you pass a PrimaryKeyGener ator (an interface!) to your CD collection at
construction time (store it as a member variable), you can use it all over the
place, i.e. when you insert a new CD you ask it to give() you a number; when
you delete a CD you let that object take() it back.
You should be able to program and compile your CD collection class using this
interface; of course it won't run (yet) because there is no implementation of this
interface yet.
Here's a stupid implementation: i.e. it generates the numbers 0, 1, 2 ... and
it doesn't do anything when you make it take() a number back again. You
obviously don't want this implementation as a final implementation but it is
enough to get you started. We'll implement a clever PrimaryKeyGener ator
later when you understand that BitSet API. Here's the stupid interface implementation: -
public class StupidPrimaryKeyGenerator implements PrimaryKeyGenerator {
-
private int key;
-
public synchronized int give() { return key++; }
-
public void take() { }
-
}
-
kind regards,
Jos
OK, that is an explaination I THINK I understand.
So, I do not want the user entering anything in to my itemField, I should probably just set editable to false and be done with it right off the bat.
I should then impliment the public int give(); every time a cd is added, which stupidkeygenera tor will give the next available number?
Is that about the long and short of it, or am I twisted?
If all that is right, my only remaining question (at the moment) would be how does int give() know to give its number to itemField?
should I not also have to change the way the field is built and initialize?
currently I have this: - private JTextField itemField;
-
and
-
itemField = new JTextField(4);
-
itemField.setEditable(true);
-
and
-
newCD.setItemno(Integer.parseInt(itemField.getText()));
-
when a cd is added
If I am to code the interface, and not the implementation, will not most of that need to change also?
OK, that is an explaination I THINK I understand.
So, I do not want the user entering anything in to my itemField, I should probably just set editable to false and be done with it right off the bat.
I should then impliment the public int give(); every time a cd is added, which stupidkeygenera tor will give the next available number?
Is that about the long and short of it, or am I twisted?
If all that is right, my only remaining question (at the moment) would be how does int give() know to give its number to itemField?
should I not also have to change the way the field is built and initialize?
currently I have this: - private JTextField itemField;
-
and
-
itemField = new JTextField(4);
-
itemField.setEditable(true);
-
and
-
newCD.setItemno(Integer.parseInt(itemField.getText()));
-
when a cd is added
If I am to code the interface, and not the implementation, will not most of that need to change also?
But now in that class you have a KeyGenerator object, so instead of setting itemNo with value entered by the user, you simply call the give method and use the number that it has generated as the itemNo
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Roman Thurnherr |
last post by:
Hi all
I'm using two JLists with the same MouseListener in a JFrame. How can I find
out on which JList the MouseEvent was released?
Is it generally possible or should I use a MouseListener for each JList?
Thanx for your help
Roman
|
by: Prakash |
last post by:
Hi !
I have a field "sub_tran_no" in my form in continuous view.
When the user presses a button "Re-Number", I'd like to:
1) Save the current record pointer position
2) Save the current field which has focus
3) go to the top of the recordset & start re-numbering
the "sub_tran_no" field with the values 1,2,3, until eof.
|
by: Salha |
last post by:
Hello there,
Can you help me in following problem:
Am working on a chemistry dictionary. I want to have all the chemistry terms in a JList and whenever i select a term from a JList, it displays the meaning in a text area (as any encyclopedia) and the term in a textfield. My questions:
1. How can i load the chemistry terms in to the JList and meanings in the textarea and store them?
2. How can i select a term and diplays it in the...
|
by: paoparaopao |
last post by:
Hi,
I want to drag rows up/down in a JList; for example, drag 3rd row to 1st row.
I have setDragEnabled(true) for JList, but it doesn't work!
How do I drag rows in JList?
Thanks!
|
by: Nepomuk |
last post by:
Hi!
I'm trying to use a JList. Here's the code:
String languages = {
"deutsch", // german
"english", // english
"castellano", // castilian
"français", // french
"italiano", // italian
"日本語", // japanese
| |
by: sukatoa |
last post by:
I came from List in awt....
Im currently having an experiment about JList....
say like 140,280 ( h,w )
i have 4 values that have been set on JList
using addElement from DefaultListModel....
|
by: wishbone34 |
last post by:
Hi, here is some of my code
public class ControlIL extends JFrame implements ActionListener, ListSelectionListener {
private JList Items;
//builds the UI
public ControlIL(){
DefaultListModel model = new DefaultListModel();
|
by: Gangreen |
last post by:
Hi all,
I've bumped into an odd problem creating a GUI in swing. As you will see in the screenshots, I have a tabbed layout. Each tab is a Jpanel. On this panel I have put a JList, containing a series of objects. My intention is to have a single column of objects in the list, and have it to scroll when the objects exceed the vertical space.
My code is this:
private JComponent createListBox() {
Object test = {"Thingy 1","Thingy...
|
by: Gangreen |
last post by:
hi,
I have an arrayList in my program that I want to display in a Jlist. I need the JList to update his contents when the elements in the arraylist change/ an element is added/deleted/...
I'm lost on how to do this. Passing the arrayList as an array into the Jlistconstructor will just display the elements, but no updates will be made according to the API.
Apparently ListDataListeners should be used, and I don't seem to be able to find a...
|
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: 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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |