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

Ads
 

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();

}
}

Ads









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
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
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
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 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
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
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
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 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
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
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
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
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
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
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
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 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
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
Pass values from form to form
Pass values from form to form       Java program to pass values from one form to another form... values from one page to another consecutive page which is being called then we can
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 run java swing application from jar files .
How to run java swing application from jar files .  Hello Sir, I developed a java swing application .Now i want to execute it as .exe... the main class program will exit" from "java virtual machine". Plz help me
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
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

Ads