Home Answers Viewqa Java-Beginners JTables or JTextAreas

 
 


Jason Ayers
JTables or JTextAreas
0 Answer(s)      11 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---

View Answers









Related Pages:
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
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
EZPanel
and JTextAreas, there is not default automatic expansion for JButtons. widthRem
Java Programming: Section 7.3
. The JTextField and JTextArea Classes JTextFields and JTextAreas are boxes where... and JTextAreas. void setText(String newText); // substitute newText..., with the getText() method. JTextAreas do not generate action events. A JTextArea does
Java Programming: Section 10.3
that the file is a text file. Since JTextAreas are not meant for displaying large

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.