Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Getting items from a table/checkboxes

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting items from a table/checkboxes

    Hi,
    i have a Java computer shop assignment, a user selects the components he/she requires for a laptop or desktop and then presses the submit button where the total price is calculated and displayed to the user.

    I have hard coded the prices for each component in a table, but am unsure as to how to "get" the users selections and how to add the prices up? or how to put that into code rather.

    [code]

    package ecsecomputers;

    import javax.swing.JLabel;
    import javax.swing.JComboBox;
    import javax.swing.JButton;

    /**
    *
    * @author mandy
    */
    public class ShopFrame extends javax.swing.JFrame {

    /** Creates new form ShopFrame */
    public ShopFrame() {
    initComponents();
    }

    // @SuppressWarnings("unchecked")Netbeans GENERATED CODE HERE

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    {"Intel", "Core II Duo", "2200", "54.99", "Intel ", "Centrino", "1800", "149,99"},
    {"Intel", "Quad Core", "3000", "179.99", "Intel ", "Centrino II", "2000", "179.99"},
    {"Intel", "Pentium", "1800", "47.99", "Intel", "Core i3", "2200", "165.99"},
    {"Intel", "Celeron", "1800", "34.99", "Intel ", "Core i5", "2200", "176.99"},
    {"Intel", "Atom", "1400", "68.99", "Intel ", "Core i7", "2800", "199.99"},
    {"AMD", "Phenom", "3000", "123.99", "AMD", "Turion II Dual Core", "2000", "145.99"},
    {"AMD", "Athlon", "3000", "99.95", "AMD", "Athlon II Dual Core", "2400", "148.95"},
    {"AMD", "Sempron", "2000", "38.99", "AMD", "V Series", "1800", "165.99"},
    {"HARDDISKS"},
    {"Samsung ", "500GB Serial ATA", "7200 rpm", "55.00"},
    {"Hitachi ", "1TB External", "7200 rpm", "123.99"},
    {"MEMORY", "", null, null, null, null, null, null},
    {"Kingston", "1GB DDRII", "1066", "25.99"},
    {"Corsair", "1GB DDRII", "1333", "23.99"l},
    {"Crucial", "1GB DDR3", "1333", "30.99"},
    {"MOTHERBOARDS"},
    {"Gigabyte", "Intel LGA775", "", "78.99"},
    {"Asus", "AMD AM3", "", "84.99"},
    {"DVD-REWRITER"},
    {"Samsung", "Internal", null, "24.99"},
    {"Sony", "External", null, "60.99"},
    {"PSU"},
    {"Antec", "650w", null, "73.99"},
    {"CASES"},
    {"Antec", "ATX", null, "56.99"},
    {"Coolermaster", "ATX", null, "76.99"},
    {"GRAPHICS CARDS"},
    {"Asus", "ATI ", "", "105.00"},
    {"Pny", "Nvidia Geforce", null, "159.99"},
    {"SOUND CARDS"},
    {"Creative ", "Soundblaster 5.1 vx", null, "15.99"},
    {"Creative ", "Soundblaster XFI", null, "38.99"},
    {"HEATSINKS"},
    {"Thermaltake", "Intel LGA755", "", "25.95"},
    {"Coolermaster", "AMD AM2 AM3", null, "23.99},
    },
    new String [] { "Brand", "Type", "Speed GHz", "Price £", "Brand", "Type", "Speed GHz", "Price £" }
    ));
    jTable1.setName("jTable1");
    jScrollPane1.setViewportView(jTable1);


    private void bSubmitActionPerformed(java.awt.event.ActionEvent evt) {
    //TO DO CODE HERE


    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
    new ShopFrame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    javax.swing.JLabel TitleLB;
    javax.swing.JButton bSubmit;
    javax.swing.JComboBox cbDeskprocessor;
    javax.swing.JComboBox cbDesktopspeed;
    javax.swing.JComboBox cbLaptopcpu;
    javax.swing.JComboBox cbLaptopspeed;
    javax.swing.JFormattedTextField jFormattedTextField1;
    javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel2;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JScrollPane jScrollPane2;
    javax.swing.JSeparator jSeparator1;
    javax.swing.JTable jTable1;
    javax.swing.JTextArea jTextArea1;
    javax.swing.JLabel lbDesktopcpu;
    javax.swing.JLabel lbLaptopcpu;
    javax.swing.JLabel lbProcessorspeed1;
    javax.swing.JLabel lbProcessorspeed2;
    javax.swing.JMenuBar menuBar;
    javax.swing.JMenu muAboutus;
    javax.swing.JMenu muComponents;
    javax.swing.JMenu muContact;
    javax.swing.JMenu muDesktop;
    javax.swing.JMenu muHelp;
    javax.swing.JMenu muHome;
    javax.swing.JMenu muIndex;
    javax.swing.JMenu muLaptop;
    javax.swing.JMenu muShoppingcart;
    javax.swing.JPanel pCpupanel;
    javax.swing.JRadioButtonMenuItem rbDeskcase;
    javax.swing.JRadioButtonMenuItem rbDeskchipset;
    javax.swing.JRadioButtonMenuItem rbDeskcpu;
    javax.swing.JRadioButtonMenuItem rbDeskdvdrewriter;
    javax.swing.JRadioButtonMenuItem rbDeskgraphics;
    javax.swing.JRadioButtonMenuItem rbDeskheatsink;
    javax.swing.JRadioButtonMenuItem rbDeskmemory;
    javax.swing.JRadioButtonMenuItem rbDeskmonitor;
    javax.swing.JRadioButtonMenuItem rbDeskmotherboard;
    javax.swing.JRadioButtonMenuItem rbDeskpsu;
    javax.swing.JRadioButtonMenuItem rbDesksound;
    javax.swing.JRadioButtonMenuItem rbLapbattery;
    javax.swing.JRadioButtonMenuItem rbLapcase;
    javax.swing.JRadioButtonMenuItem rbLapchassis;
    javax.swing.JRadioButtonMenuItem rbLapchipset;
    javax.swing.JRadioButtonMenuItem rbLapcpu;
    javax.swing.JRadioButtonMenuItem rbLapdvdrewriter;
    javax.swing.JRadioButtonMenuItem rbLapgraphicscard;
    javax.swing.JRadioButtonMenuItem rbLapharddisk;
    javax.swing.JRadioButtonMenuItem rbLapheatsink;
    javax.swing.JRadioButtonMenuItem rbLapkeyboard;
    javax.swing.JRadioButtonMenuItem rbLapmotherboard;
    javax.swing.JRadioButtonMenuItem rbLapscreen;
    javax.swing.JRadioButtonMenuItem rbLapsoundcard;
    // End of variables declaration

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting items from a table/checkboxes

    Sorry, does that compile? You seem to be doing stuff outside of methods. I can't really tell, as you haven't used the code tags correctly, and I have trouble reading unformatted code.

    Why are you storing your prices in a JTable, but using JRadioButtons for the selection of the items?

    Give this a read: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    And come back with an SSCCE (that's a link) if you still have questions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting items from a table/checkboxes

    Hi sorry for the confusion, i did use
     
    tags?

    I have attached the files in a zip....i am using netbeans so there is a whole lot of code in the middle of the program that is just design code generated by the program so i just posted the relevant bits.

    private void bSubmitActionPerformed(java.awt.event.ActionEvent evt) {

    TO DO CODE HERE //getValueAt() (for the table?)

    }

    I have set up a computer shop that the user selects components for their desired computer i hard coded all the details in a JTable which contains the price of each component. Firstly they select whether they want a laptop or desktop from a drop down toolbar menu and then what CPU they want in it from a combobox.

    And then the price is in the table of each component so i need to get all the selected components from the user and then return a total price for them.

    hope that is clearer but i have included the JAVA files built with netbeans....it compiles fine there is just so much code that netbeans generates i was trying to keep it simple but..
    Attached Files Attached Files

Similar Threads

  1. JTable CheckBoxes
    By aussiemcgr in forum AWT / Java Swing
    Replies: 1
    Last Post: August 9th, 2010, 01:04 PM
  2. misalignment of list items
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: March 15th, 2010, 08:33 AM
  3. [SOLVED] adding items to a binary tree
    By vendetta in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 26th, 2010, 09:32 PM
  4. How to get a sum value from items listed in array ?
    By makarov in forum Loops & Control Statements
    Replies: 0
    Last Post: January 6th, 2010, 06:11 PM
  5. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 06:07 AM