| | |
Impementing First GUI into Current App
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I did have concerns about the toString, but again fall into unfamiliar territory with that function.
I do understand what you say about the list being empty, which goes back to an overall question about how the GUI pulls information, and from where.
I think I am way over my head here, I think I probably need to scrap the JLIST and just go back to the original plan and insert everything into a new gui class that uses my Compactdisk and Cdwartist classes.
Right now I am trying to build on a foundation I have not properly laid yet.
1st thing I need to do is get a QUI screen with some fields in it where I can enter information into my existing array.
Then I will worry about sorting, adding and all that other stuff.
Would you suggest I take everything from Inventory and just re-write it into my new class, so that I do not use Inventory anymore?
I do understand what you say about the list being empty, which goes back to an overall question about how the GUI pulls information, and from where.
I think I am way over my head here, I think I probably need to scrap the JLIST and just go back to the original plan and insert everything into a new gui class that uses my Compactdisk and Cdwartist classes.
Right now I am trying to build on a foundation I have not properly laid yet.
1st thing I need to do is get a QUI screen with some fields in it where I can enter information into my existing array.
Then I will worry about sorting, adding and all that other stuff.
Would you suggest I take everything from Inventory and just re-write it into my new class, so that I do not use Inventory anymore?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
You could, if you feel that is easier for you to logically work with. JList has it's own container (the list model) that you can put CDs into. The part from your loop in inventory will largely be replaced by code in a button that creates a new CD, reads the info from your text fields and sets the properties on the CD, and then adds it to the list model.
That is a lot of my problem right there, at this point I am not even sure what I should do. This is my first attempt at GUI so I wanted to stay as basic as possible, and keep as much of my existing code as possible because I understand it as it is (for the most part.)
I have never used JList either, so between that and GUI both at once I am just getting confused as to what is changing by implimenting one, and what changes because of the other.
I am looking at the code I have attempted today, some for GUI frames, some for buttons, some for JList ... all of it new, and none of it clear.
I think I am going to delete everything I have tried up to this point, and just start over. :o(
I have never used JList either, so between that and GUI both at once I am just getting confused as to what is changing by implimenting one, and what changes because of the other.
I am looking at the code I have attempted today, some for GUI frames, some for buttons, some for JList ... all of it new, and none of it clear.
I think I am going to delete everything I have tried up to this point, and just start over. :o(
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
Yes, GUI code can be a little complex to jump into. You just have to start simple and keep at it. Perhaps this demo will help. It's just a tiny form I knocked up that let's you add a CD with artist and title to a JList.
Java Syntax (Toggle Plain Text)
import javax.swing.DefaultListModel; public class ListDemo extends javax.swing.JFrame { private javax.swing.JButton btnAdd; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblArtist; private javax.swing.JLabel lblTitle; private javax.swing.JList lstInventory; private javax.swing.JTextField txtArtist; private javax.swing.JTextField txtTitle; private DefaultListModel listModel; public ListDemo() { initComponents(); } private void initComponents() { lblArtist = new javax.swing.JLabel(); txtArtist = new javax.swing.JTextField(); lblTitle = new javax.swing.JLabel(); txtTitle = new javax.swing.JTextField(); btnAdd = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); lstInventory = new javax.swing.JList(); getContentPane().setLayout(new java.awt.FlowLayout()); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); lblArtist.setText("Artist"); getContentPane().add(lblArtist); txtArtist.setMinimumSize(new java.awt.Dimension(75, 19)); txtArtist.setPreferredSize(new java.awt.Dimension(75, 19)); getContentPane().add(txtArtist); lblTitle.setText("Title"); getContentPane().add(lblTitle); txtTitle.setMinimumSize(new java.awt.Dimension(75, 19)); txtTitle.setPreferredSize(new java.awt.Dimension(75, 19)); getContentPane().add(txtTitle); btnAdd.setText("Add"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); getContentPane().add(btnAdd); listModel = new DefaultListModel(); lstInventory.setModel(listModel); jScrollPane1.setViewportView(lstInventory); getContentPane().add(jScrollPane1); pack(); } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { // Create cd and set title CD cd = new CD(txtArtist.getText()); cd.setTitle(txtTitle.getText()); // Add the cd to list listModel.addElement(cd); // Clear the text fields txtArtist.setText(null); txtTitle.setText(null); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ListDemo().setVisible(true); } }); } /** Small CD class to hold our data */ class CD { private String artist; private String title; public CD(String artist){ this.artist = artist; } public void setTitle(String title){ this.title = title; } public String toString(){ return artist + " - " + title; } } }
Just so you know I was not just sitting around waiting on code, here is what I had come up with over the afternoon ... looking at what you hve done I think I was at least headed in the right direction. I was rewriting my entire Inventory class from the ground up. I am not exactly sure where I was going with it, but I kept the idea stream flowing and this is where it took me. :o)
Java Syntax (Toggle Plain Text)
import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; 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 JLabel valueLabel; // value of that item label private JLabel rstkLabel; // cost to restock label private JLabel totalLabel; // total value of inventory 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 JFormattedTextField valueField; // value of that item display private JFormattedTextField rstkField; // cost to restock display private NumberFormat nstockFormat; // format field and parse numbers private NumberFormat priceFormat; // format field and parse numbers private NumberFormat valueFormat; // format field and parse numbers private NumberFormat rstkFormat; // format field and parse numbers private JButton firstBtn; // first button private JButton prevBtn; // previous button private JButton nextBtn; // next button private JButton lastBtn; // last 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; // current product display to use for button action private int i; // iterator private double total = 0; // variable for total inventory 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:"); valueLabel = new JLabel("Value of Item Inventory:"); rstkLabel = new JLabel("Cost to Re-Stock Item:"); totalLabel = new JLabel("Total Value of Inventory:$"+total); // create button names firstBtn = new JButton("First"); prevBtn = new JButton("Previous"); nextBtn = new JButton("Next"); lastBtn = new JButton("Last"); // create textFields and set uneditable cdNameField = new JTextField(15); cdNameField.setEditable(false); artistField = new JTextField(15); artistField.setEditable(false); ; nstockField = new JFormattedTextField(nstockFormat); nstockField.setEditable(false); nstockField.setColumns(10); priceField = new JFormattedTextField(priceFormat); priceField.setEditable(false); priceField.setColumns(10); itemField = new JTextField(15); itemField.setEditable(false); valueField = new JFormattedTextField(valueFormat); valueField.setEditable(false); valueField.setColumns(10); rstkField = new JFormattedTextField(rstkFormat); rstkField.setEditable(false); rstkField.setColumns(10); // add buttons to panel JPanel buttons = new JPanel(); // set up panel buttons.setLayout( new GridLayout(1, 4)); //set layout // add buttons to buttons buttons.add(firstBtn); buttons.add(prevBtn); buttons.add(nextBtn); buttons.add(lastBtn); // create cd Array CdwArtist[] cds = new CdwArtist[100]; for(int i = 0; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++) cds[i] = new CdwArtist(); cds[i].setName(nameInput); //total += cdProduct[currCD].calcValue(); // calculate total inventory cost // add values to textFields cdNameField.setText(cds[i].getProdName()); artistField.setValue(new Integer(cds[i].getArtist())); nstockField.setValue(new Integer(cds[i].getUnits())); priceField.setValue("$"+new Double(cds[i].getPrice())); itemField.setText(cds[i].getFeature()); valueField.setValue("$"+new Double(cds[i].calcValue())); rstkField.setValue("$"+new Double(cds[i].calcValueRstk()))
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
I did not understand it at first, but after sitting down and comparing it to what I was writing I could see what I was trying to do, and it cleared up in my head.
Then I started coding, and as I created everything I could see the logic behind what was being built, how the buttons, fields, and JList all work together.
I do have a few questions of course, if you have a moment,
Here is everything, from your original work, to what I have added:
That is the first class I have written where I understood what I was doing from start to finish. I do have a few details I need to pick your brain about.
first: CD cd = new CD(artistField.getText());
I know what this is doing, but I am getting the error cannot find symbol pointing at CD.
I even understand the error, I do not see where CD is defined anywhere, nor do I know where it would be put.
second: I also plan to go in an modify my previous Compactdisk and CdwArtist classes for use with this class. How does this class know to use those two?
Then I started coding, and as I created everything I could see the logic behind what was being built, how the buttons, fields, and JList all work together.
I do have a few questions of course, if you have a moment,
Here is everything, from your original work, to what I have added:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;
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 JLabel valueLabel; // value of that item label
private JLabel rstkLabel; // cost to restock label
private JLabel totalLabel; // total value of inventory 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 JFormattedTextField valueField; // value of that item display
private JFormattedTextField rstkField; // cost to restock display
private JFormattedTextField totalField; // value of all inventory combine
private NumberFormat nstockFormat; // format field and parse numbers
private NumberFormat priceFormat; // format field and parse numbers
private NumberFormat valueFormat; // format field and parse numbers
private NumberFormat rstkFormat; // 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 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; // current product display to use for button action
private int i; // iterator
private double total = 0; // variable for total inventory
private JList Inventorylist; // JList to take place of old array
private DefaultListModel listModel;
private JScrollPane jScrollPanel;
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:");
valueLabel = new JLabel("Value of Item Inventory:");
rstkLabel = new JLabel("Cost to Re-Stock Item:");
totalLabel = new JLabel("Total Value of Inventory:$"+total);
// JList
jScrollPanel = new JScrollPane();
Inventorylist = new JList();
// buttons
btnAdd = new JButton();
btnNext = new JButton();
btnPrev = new JButton();
btnDel = new JButton();
getContentPane().setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// place textFields and labels
//artist
artistLabel.setText("Artist");
getContentPane().add(artistLabel);
artistField.setMinimumSize(new Dimension(75,19));
artistField.setPreferredSize(new Dimension(75,19));
getContentPane().add(artistField);
// cd name
cdNameLabel.setText("CD Name");
getContentPane().add(cdNameLabel);
cdNameField.setMinimumSize(new Dimension(75,19));
cdNameField.setPreferredSize(new Dimension(75,19));
getContentPane().add(cdNameField);
// copies in stock
nstockLabel.setText("Copies In Stock");
getContentPane().add(nstockLabel);
nstockField.setMinimumSize(new Dimension(75,19));
nstockField.setPreferredSize(new Dimension(75,19));
getContentPane().add(nstockField);
//price of cd
priceLabel.setText("Price");
getContentPane().add(priceLabel);
priceField.setMinimumSize(new Dimension(75,19));
priceField.setPreferredSize(new Dimension(75,19));
getContentPane().add(priceField);
//item number of cd
itemLabel.setText("Item Number");
getContentPane().add(itemLabel);
itemField.setMinimumSize(new Dimension(75,19));
itemField.setPreferredSize(new Dimension(75,19));
getContentPane().add(itemField);;
// value of individual cd in stock
valueLabel.setText("Value");
getContentPane().add(valueLabel);
valueField.setMinimumSize(new Dimension(75,19));
valueField.setPreferredSize(new Dimension(75,19));
getContentPane().add(valueField);
// restocking fee
rstkLabel.setText("Restock Fee");
getContentPane().add(rstkLabel);
rstkField.setMinimumSize(new Dimension(75,19));
rstkField.setPreferredSize(new Dimension(75,19));
getContentPane().add(rstkField);
// total value of inventory
totalLabel.setText("Total Inventory Value");
getContentPane().add(totalLabel);
totalField.setMinimumSize(new Dimension(75,19));
totalField.setPreferredSize(new Dimension(75,19));
getContentPane().add(totalField);
// add buttons
//ADD
btnAdd.setText("Add");
Java Syntax (Toggle Plain Text) - btnAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnAdd);
// PREVIOUS
btnPrev.setText("Previous");
Java Syntax (Toggle Plain Text) - btnPrev.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnPrev);
// NEXT
btnNext.setText("Next"); Java Syntax (Toggle Plain Text) - btnNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnNext);
// DELETE
btnDel.setText("Delete");
Java Syntax (Toggle Plain Text) - btnDel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(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
CD cd = new CD(artistField.getText());
cd.setcdName(cdNameField.getText());
cd.setitem(itemField.getText());
cd.setnstock(nstockField.getText());
cd.setprice(priceField.getText());
cd.setvalue(valueField.getText());
cd.settotal(totalField.getText());
cd.setrstk(rstkField.getText());
// Add cd to list
listModel.addElement(cd);
// Clear the text fields after add
artistField.setText(null);
cdNameField.setText(null);
itemField.setText(null);
nstockField.setText(null);
priceField.setText(null);
valueField.setText(null);
rstkField.setText(null);
totalField.setText(null);
}// end ADD
// run it
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Inventory2().setVisible(true);
}
});
}
} // close classThat is the first class I have written where I understood what I was doing from start to finish. I do have a few details I need to pick your brain about.
first: CD cd = new CD(artistField.getText());
I know what this is doing, but I am getting the error cannot find symbol pointing at CD.
I even understand the error, I do not see where CD is defined anywhere, nor do I know where it would be put.
second: I also plan to go in an modify my previous Compactdisk and CdwArtist classes for use with this class. How does this class know to use those two?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
•
•
first: CD cd = new CD(artistField.getText());
I know what this is doing, but I am getting the error cannot find symbol pointing at CD.
I even understand the error, I do not see where CD is defined anywhere, nor do I know where it would be put.
second: I also plan to go in an modify my previous Compactdisk and CdwArtist classes for use with this class. How does this class know to use those two?
I feel like a genious (about the CdwArtist class), I had just gotten that to work, and have my sets going to that class ...
here is what it looks like now
It must be working because it sees that some of those fields (Itemno for example) is defined as INT in the other class.
It is erroring telling me that those parameters cannot be applied to java.lang.String
I "kind of" understand what it is saying, I have numbers in these fields, some of which I will be doing calculations as, but it is expecting a String. Right? Where would I alter that?
I am going to look at the toString right now and see if I can figure out what you are telling me there.
Thanks again for your help!
here is what it looks like now
Java Syntax (Toggle Plain Text)
// Create cd to add CdwArtist cd = new CdwArtist(artistField.getText()); cd.setName(cdNameField.getText()); cd.setItemno(itemField.getText()); cd.setNstock(nstockField.getText()); cd.setPrice(priceField.getText()); cd.setValue(valueField.getText()); cd.setTotal(totalField.getText()); cd.setRestock(rstkField.getText());
It must be working because it sees that some of those fields (Itemno for example) is defined as INT in the other class.
It is erroring telling me that those parameters cannot be applied to java.lang.String
I "kind of" understand what it is saying, I have numbers in these fields, some of which I will be doing calculations as, but it is expecting a String. Right? Where would I alter that?
I am going to look at the toString right now and see if I can figure out what you are telling me there.
Thanks again for your help!
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
I believe I have the toString right.
The only thing I am having trouble with, logically, and code wise at the moment is how to solve the String/Int problem in Inventory2.
Do these fields not need to be int, double, or float in order to perform calculations on them?
The only thing I am having trouble with, logically, and code wise at the moment is how to solve the String/Int problem in Inventory2.
Do these fields not need to be int, double, or float in order to perform calculations on them?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
![]() |
Other Threads in the Java Forum
- Previous Thread: How to download a file
- Next Thread: Java image programming question
| Thread Tools | Search this Thread |
Tag cloud for Java
@param android animated api appinventor apple applet application arguments array arrays automation binary bluetooth c++ chat chatprogramusingobjects chooser class classes client code codesnippet compiler component coordinates database doctype draw eclipse editor error event exception file fractal freeze game givemetehcodez graphics gui guidancer health helpwithhomework html ide image input int integer j2me java javaprojects jmf jni jpanel jtable julia linux list login loop map method methods mobile netbeans newbie nonstatic number oracle page plazmic print problem program programming project recursion scanner screen sell server set sharepoint size sms socket sort sourcelabs sql string swing system test testautomation threads time tree windows






