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()
        super("Retail Calculator");
        setSize(800, 400);
        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

        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 

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

    //add items to Frame

        //Table for results(found example that helped at-
        JTable results = new JTable(_data, _titles);
        JScrollPane scrollPane = new JScrollPane(results);
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        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("$#,###.##");                     

        clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event){

        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){


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

        public void actionPerformed(ActionEvent arg0) {


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


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  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
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  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*; 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
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? file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code


Advertisement null