Share on Google+Share on Google+

Jason Ayers
JTables or JTextAreas
0 Answer(s)      4 years and 9 months ago
Posted in : Java Beginners

I've written code for a retail calculator. Now I need to have the user input(item name, department, original price, and sale price) transferred to a JTable once my code does its calculation...How can I make this happen?

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Wk2RetailCalc extends JFrame implements ActionListener
{
    //create objects
    JLabel product;
    JLabel department;
    JLabel originalPrice;
    JLabel discount;
    JLabel finalPrice;
    JTextField productName;
    JComboBox<String> departments;
    JTextField oPrice;
    JTextField discPercent;
    JTextField salePrice;
    JButton calc;
    JButton clear;
    JButton exit;

    private String[] _titles = new String[] {"Item Name", "Department","Original Price","Sale Price"};
    private String[][] _data = new String[][] {{"", "","",""},{"","","",""}};


    public Wk2RetailCalc()
    {
        //Frame
        super("Retail Calculator");
        setSize(800, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.CENTER));


        //Labels and Fields
        product = new JLabel("Product Name");
        productName = new JTextField(10);//allows user to enter item name 
        department = new JLabel("Department");
        departments = new JComboBox<String>();
           departments.addItem("-Select One-");//allows user to choose items department
           departments.addItem("Mens");
           departments.addItem("Womens");
           departments.addItem("Electronics");
           departments.addItem("Hardware");
           departments.addItem("Shoes");

        originalPrice = new JLabel("Original Price");
        oPrice = new JTextField(10);//allows user to enter original price 
        discount = new JLabel("Discount %");
        discPercent = new JTextField(10);//allows user to enter percent of discount 
        finalPrice = new JLabel("Sale Price");
        salePrice = new JTextField(10);//shows user the calculated sale price 
        salePrice.setEditable(false);

             //Buttons
            calc = new JButton("Calculate");//calculates user input 
            clear = new JButton("Clear");//clears user input
            exit = new JButton("Exit");//exits application




    //add items to Frame
        add(product);
        add(productName);
        add(department);
        add(departments);
        add(originalPrice);
        add(oPrice);
        add(discount);
        add(discPercent);
        add(finalPrice);
        add(salePrice);
        add(calc);
        add(clear);
        add(exit);

        //Table for results(found example that helped at- http://www.gammelsaeter.com/programming/simple-jframe-and-jtable-example-in-java/)
        JTable results = new JTable(_data, _titles);
        JScrollPane scrollPane = new JScrollPane(results);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        setVisible(true);


        calc.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent retail) {
                String input1;
                String input2;
                double origPrice;
                double percOff;
                double clearance;

                input1 = oPrice.getText();
                input2 = discPercent.getText();
                origPrice = Double.parseDouble(input1);
                percOff = Double.parseDouble(input2)/100;
                clearance = origPrice - (origPrice * percOff);
                DecimalFormat df = new DecimalFormat("$#,###.##");                     
                df.format(clearance);                       
                salePrice.setText(df.format(clearance));
            }
        });

        clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event){
                productName.setText(null);
                departments.setSelectedIndex(0);
                oPrice.setText(null);
                discPercent.setText(null);
                salePrice.setText(null);
            }
        });

        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                System.exit(0);
            }
        });





        setVisible(true);
    }

    public static void main(String[] args) 
       {
            new Wk2RetailCalc();
        }


        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
    }

}

Thanks in advance for any help!!! ---J---

Ads

View Answers









Related Tutorials/Questions & Answers:
JTables or JTextAreas
JTables or JTextAreas  I've written code for a retail calculator. Now I need to have the user input(item name, department, original price, and sale price) transferred to a JTable once my code does its calculation...How can I make
table
table  Hi.. I am doing a project on Inventory Management System for which i created the user interfaces.. I have a user interface in which i have used JTables.. Now my problem is I dont know how to show the Jtable which takes
Advertisements
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container below that row itself and show the required details of the row
REPORT WITH JTABLE
REPORT WITH JTABLE  i have data in backend(oracle10g,spl+).i want the data into front end(java jdk) with the help of jtables thnx in advance   Hi Friend, We have used MySQL database.Try this: import java.io.*; import
Connecting JTable to database - JDBC
Connecting JTable to database  Hi.. I am doing a project on Project Management System for which i created the user interfaces.. I have a user interface in which i have used JTables.. Now my problem is I dont know how to how
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads