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 swingHow 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 javaHow 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 anotherhow 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 JAVAproblem 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
passing data from one jframe to anotherpassing
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 copying data from one table to anothercopying
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 anotherto 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
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
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 eclipseHow 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 filepass 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
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
Data Conversion from int to another typeData 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 typeData 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 typeData 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 typeData 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 typeData 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 typeData 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 typeData 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
Flex 3 data binding process and exampleData 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 classhow 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
update one table from another tableupdate
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
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 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 applicationRetrieve
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