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

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