How To Pass data from one GUI to another in java swing

How To Pass data from one GUI to another in java swing

I'm new to java and part of our assignment is to build a GUI and display a result set from data input. I'm stuck at how to get the user's input from JTextFields and combobox selection and display those values to a table. My code is something like this. Thanks in advance.

import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.GridLayout;

public class RetailCalculatorProject extends JFrame { //components JPanel panel1 = new JPanel(); JLabel iName = new JLabel ("Item Name");

JPanel panel2 = new JPanel(); JLabel iOrigPrice = new JLabel("Original Price $ "); JPanel panel3 = new JPanel(); JLabel percentD = new JLabel ("Percent Discount "); JPanel panel4 = new JPanel(); JLabel selectDepartmentLabel = new JLabel("Select Department"); JPanel panel5 = new JPanel(); String[] departments = {"Bath" , "Bedroom" , "Garden/Patio", "Kitchen", "Living Room"}; JComboBox option = new JComboBox(departments); JPanel buttonsPanel = new JPanel(); JButton calculateButton = new JButton("Calculate"); JButton exitButton = new JButton("Exit"); CalculateButton cb = new CalculateButton(); ExitButton eb = new ExitButton(); public RetailCalculatorProject() { super ("Retail Calculator"); setSize(550, 300); setLocationRelativeTo(null); GridLayout layout = new GridLayout(7, 1, 3, 3); setLayout(layout); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setLookAndFeel(); } private void setLookAndFeel() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception exc) { //ignore error } } { FlowLayout row1 = new FlowLayout (FlowLayout.LEFT, 10, 10); setLayout(row1); panel1.add(iName); JTextField itemName = new JTextField(20); panel1.add(itemName); add(panel1); } { FlowLayout row2 = new FlowLayout(FlowLayout.LEFT, 10, 10); setLayout(row2); panel2.add(iOrigPrice); JTextField oP = new JTextField (4); panel2.add(oP); add(panel2); } { panel3.add(percentD); JTextField discount = new JTextField (3); panel3.add(discount); add(panel3); } { panel4.add(selectDepartmentLabel); add(panel4); } { panel5.add(option); add(panel5); } { buttonsPanel.add(calculateButton); calculateButton.addActionListener(cb); buttonsPanel.add(exitButton); exitButton.addActionListener(eb); add(buttonsPanel); } public static void main(String[] args) { //create an instance of the RetailCalculatorProject to apply members RetailCalculatorProject assignment = new RetailCalculatorProject(); } }
View Answers

September 17, 2012 at 5:54 PM

Here is a code that accepts the user input and save it to jtable.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;

public class RetailCalculatorProject extends JFrame
{
JLabel iName = new JLabel ("Item Name");
JLabel iOrigPrice = new JLabel("Original Price $ ");
JLabel percentD = new JLabel ("Percent Discount ");
JLabel selectDepartmentLabel = new JLabel("Select Department");
String[] departments = {"Bath" , "Bedroom" , "Garden/Patio", "Kitchen", "Living Room"};
JComboBox option = new JComboBox(departments);

JButton calculateButton = new JButton("Calculate");
JButton exitButton = new JButton("Exit");
JTextField itemName = new JTextField(20);
JTextField oP = new JTextField (4);
 JTextField discount = new JTextField (3);



public RetailCalculatorProject()
{
    super ("Retail Calculator");


    setLayout(new GridLayout(5,2,5,5));

    add(iName);
    add(itemName);
    add(iOrigPrice);
    add(oP);
    add(percentD);
    add(discount);
    add(selectDepartmentLabel);
    add(option);
    add(calculateButton);

    calculateButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
  String v1=itemName.getText();
  String v2=oP.getText();
  String v3=discount.getText();
  String v4=option.getSelectedItem().toString(); 


  JFrame f = new JFrame();
  String data[][] = {{}};
  String col[] = {"Item Name","Price","Discount(%)","Department"};
  DefaultTableModel model = new DefaultTableModel(data,col);
  JTable table = new JTable(model);
  model.insertRow(0,new Object[]{v1,v2,v3,v4});
  f.add(table);
  f.setSize(300,200);
  f.setVisible(true);
        }   
    });
    setSize(550, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}


public static void main(String[] args) {
    RetailCalculatorProject assignment = new RetailCalculatorProject();

}
}









Related Tutorials/Questions & Answers:
How To Pass data from one GUI to another in java swing
How To Pass data from one GUI to another in java swing  I'm new to java and part of our assignment is to build a GUI and display a result set from data input. I'm stuck at how to get the user's input from JTextFields and combobox
How to pass variable from one class to another in java
How to pass variable from one class to another in java  How to pass variable from one class to another in java?   Example: public class...; } } //Another class Class2. public class Class2 { public static void main(String
Advertisements
how to pass a string from one class to another
how to pass a string from one class to another  hi all, Good morning. I am trying to develop a coding pass a string from one class and declare in other class. the string which i need to pass is the data from an file and i want
how to pass arraylist data from one jsp to another jsp - JSP-Servlet
how to pass arraylist data from one jsp to another jsp  hi to all iam not able to pass the data which is stroed in arraylist, actually i need... the following >XXX/ ? is required to send a value from one jsp to another
how to pass an array from one jsp to another jsp - JSP-Servlet
how to pass an array from one jsp to another jsp  hi friedns, can any One tell me how to send an array from one jsp to another jsp,pls any one send the code for this.also porvid the code how to retrive the arry in another jsp
problem in sending data from one ip to another ip in JAVA
problem in sending data from one ip to another ip in JAVA  Hi guys . this is naveen kumar...i need a help. i want to connect one ip to another ip... device in JAVA. i know how to ping but don't how to send the msg's to another IP
Pass cookie from one site to another within the same domain
Pass cookie from one site to another within the same domain  Hi, I want to pass a cookie from one website to another within the same domain. This is just to identify whether the same user is logged in or not. Can anyone help me
passing data from one jframe to another
passing data from one jframe to another  I have two JFrame,built by using the GUI Editor netbeans 6.9.i have to pass a data from a Jtextfield in the first Jframe to another JLabel in the other JFrame.Can you please help me,how
how to copy file from one directory to another in java with examples
how to copy file from one directory to another in java with examples  how to copy file from one directory to another in java with examples? I want... copy from one directory to another in java. Thanks
copying data from one table to another
copying data from one table to another  i need to copy data from one table to another table.. i.e the 1st eid of employee table must be copied to the 1st eid in appusers table, 2nd eid of employee to the 2nd eid of appusers
to bring checked data from one page to another
to bring checked data from one page to another  thanks for your help... fetching data from the database on second jsp page with checkbox corresponding each... there is one jsp page , on it there is a link, on click of which it is opening second
Copy data from one JTextField to another JTextField
, you will learn how to copy the data from one JTextField into another... Copy data from one JTextField to another JTextField... and then paste into another JTextField. JTextField is used in the swing
how can we pass parameters from jsp href to another servlet
how can we pass parameters from jsp href to another servlet  how can we pass parameters from jsp href to another servlet
how to scrape the data from one website another website using html dom parsing in php
how to scrape the data from one website another website using html dom parsing in php  " how to scrape the data from one website another website using html dom parsing in php" i'm trying this one .please provide code for me
navigating the value of JTextField from one JFrame to another - Swing AWT
navigating the value of JTextField from one JFrame to another  hello I m doing one project where I m retriving the data from databse in JTextField...=conn.createStatement(); ResultSet rs=st.executeQuery("Select * from data"); String
how to go from one frame to another frame in swings - Java Beginners
how to go from one frame to another frame in swings  Hi, My task is to go from one frame to another.......... let us think that iam having two... this task in swings or applet......... i have gone through one example of login
navigating the value of JTextField from one JFrame to another - Swing AWT
navigating the value of JTextField from one JFrame to another  Hello..."); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("Select * from data... is working also.. but only first record is comming... how can I fetch all
How to forward the control from one jsp to another?
How to forward the control from one jsp to another?  Hi! This is Prasad Jandrajupalli. I have the 3 JSP's, but I want communicate with each... is not communicate with the Third JSP. I want forward the control from first jsp to second
take data from one table and insert same in another
take data from one table and insert same in another  I want to know when a new field update in table and the same data has to update in another table
File copy from one directory to another in java
for copying the file from one directory to another directory in Java...File copy from one directory to another in java  I am trying to find very good code example for copying a file from one directory to another
How to navigate from one jsf to another in eclipse
How to navigate from one jsf to another in eclipse  Hi there is my jsf code... <%@ page language="java" contentType="text/html; charset... submitting the form it navigate to the following page :- <%@ page language="java
pass data from java frame to file
pass data from java frame to file  i want to write the data to file, and data must be copy from java frame java frame contains 7-8 labels n below that 1 table. i want store all this data to 1 file. plz help me
retrieving of data from one jsp to another jsp - JSP-Servlet
retrieving of data from one jsp to another jsp  using jsp i m displaying a table ,in table i m displaying a radio button then values like id,name etc ,if i click on an radio button i have to get a corresponding id value
swings:how to link from one form to another form
swings:how to link from one form to another form  how to link from one form to another form
Pass values from form to form
Pass values from form to form       Java program to pass values from one form to another form... to only pass values from one page to another consecutive page which is being
How to copy files from one folder to another based on lastmodified date - Java Beginners
Copy files from one folder to another  How to copy files from one folder to another based on lastmodified date
Data Conversion from int to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from int primitive type to another data type like String, boolean and char etc
Data Conversion from String to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from String primitive type to another data type like int, boolean and char etc
Data Conversion from short to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from short primitive type to another data type like String, boolean and char etc
Data Conversion from byte to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from byte primitive type to another data type like String, boolean and char etc
Data Conversion from float to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from float primitive type to another data type like String, boolean and char etc
Data Conversion from double to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from double primitive type to another data type like String, boolean and char etc
Data Conversion from long to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from long primitive type to another data type like String, boolean and char etc
how to write function of copy from one to another location in this code
how to write function of copy from one to another location in this code  I need to write a code to copy a file from one location to another using... to add code for copy using swing!! thanks in advance.** /* For Browse button
How to copy files from one folder to another based on lastmodified date - Java Beginners
How to copy files from one folder to another based on lastmodified date  I have to make a simple utility to copy files from a folder to another...); } }}-----------------------read for more information.http://www.roseindia.net/java
How to Pass data from jTextField1 in jFrame1 to jLabel1 in jFrame2
How to Pass data from jTextField1 in jFrame1 to jLabel1 in jFrame2  I have a Form login_pass (jFrame) which has a jTextField1 to take input (employee id) from the user. I want that the value of employee id given by user
File copy from one drive to another. - Java Beginners
File copy from one drive to another.  I want to copy a .mp3 file from one drive(let d: drive)to another drive(let e: drive) in my Windows XP. How can I do
how to use one form out of multiple form from one jsp to another jsp
how to use one form out of multiple form from one jsp to another jsp  Hi All. please give me your valuable advise for below requirement.. I have... one form from abc.jsp as it is to another .jsp(say def.jsp).Your advise
How to export data from database to excel sheet by using java swing in standalone project - Java Beginners
How to export data from database to excel sheet by using java swing... from u to solve my problem. my problem is i don know how to export data from database to excel sheet by using java swing in standalone project.I get solution from
calling one jsp from another jsp page
calling one jsp from another jsp page  need coding for calling one jsp from another jsp including the xml file.Its urgent
Flex 3 data binding process and example
Data binding in Flex 3:- Data binding is a process to pass data from one object to another object and is a process to pass data between different layer... can see how to bind data with the help of <mx:Bindung> tag. Example
how to pass class variable to another class
how to pass class variable to another class  i need to access a variable from a class to another class currently running at the same time. using Classname.variable i got the value. But when i change the value second time i got
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length  how to move curosr from one text field to another automatically when the first textfield reaches its maximum
update one table from another table
update one table from another table  hello, i need to update one table fields by using another table fields. I have to tables Products.... for example TotalQuantity field from Products table should be updated if i
I'm trying to pass counter data to another HTML page.
I'm trying to pass counter data to another HTML page.  ... cannot call the same value in my another HTML page it starts from 12 again... see 10 in my another HTML quiz page. Is there any way to do
Moving file or directory from one directory to another
Moving file or directory from one directory to another... directory is moved from one directory to another directory. This program... from one directory to another. In this section an example is given
Java - Copying one file to another
Java - Copying one file to another       This example illustrates how to copy contents from one... which copies the contents from one specified file to another specified file
how to insert the bulk data into the data base from the table of jsp page to another jsp page
how to insert the bulk data into the data base from the table of jsp page to another jsp page  pls help i'm doing the project called centralized... to insert the marks details into the data base i have retrive the rollno and name
How to save data - Swing AWT
then in jList or or Jtable many data viewer in one button,then another button must save data from jList and Jtable(in jList or jTable data will be many).Thank's...How to save data  Hi, I have a problem about how to save data
Retrieve data from database in swing application
Retrieve data from database in swing application  I want to retrive data(doctor name,specilization,date) from my sql database...(); ResultSet rs=st.executeQuery("select * from data where id=1"); while

Ads