Results 1 to 1 of 1
Thread: Creating JTable from GUI
- 04-01-2014, 05:20 AM #1
Member
- Join Date
- Mar 2014
- Posts
- 7
- Rep Power
- 0
Creating JTable from GUI
Is there a way to create a JTable from a GUI very easily? I created a "Payroll Calculator" code with a GUI and need to populate the data into a row of a JTable, but I can't think of a way to accomplish this easily. The GUI code is below.
Java Code:package payrollCalculator; import java.awt.BorderLayout; 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.JOptionPane; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.JTextField; /** * * @author Antonio School */ public class payrollCalculatorFrame extends JFrame{ private JPanel employeeName; //Employee name panel private JPanel salary; //Employee salary panel private JPanel hours; //Employee hours panel private JComboBox departments; //Departments combo box private JLabel empNameLabel; //employee name label private JLabel empDeptLabel; //employee department label private JLabel salaryLabel; //employee Salary label private JLabel empHourLabel; private JTextField empName; private JTextField selectedDept; //The selected department public JTextField salaryValue; public JTextField hourValue; private JButton calcButton; private double hour; private double salAmt; private final JTable empData; /* Constructor for the GUI */ public payrollCalculatorFrame() { super("Payroll Calculator"); setSize(700, 150); //default close operation setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Layout manager setLayout(new BorderLayout()); //build the panels //create panel employeeName employeeName = new JPanel(); //label for employee text empNameLabel = new JLabel("Employee Name:"); //Text Field to store employee new and last name empName = new JTextField("Employee Last, First Name"); empName.setEditable(true); //label for employee text empDeptLabel = new JLabel("Employee Department:"); //Combo box for department String[] departmentList = {"Accounting", "HR", "IT", "Legal", "Marketing", "Service Center"}; JComboBox departments = new JComboBox(departmentList); //add components to panel employeeName.add(empNameLabel, BorderLayout.WEST); employeeName.add(empName, BorderLayout.EAST); employeeName.add(empDeptLabel, BorderLayout.WEST); employeeName.add(departments, BorderLayout.EAST); //create panel salary = new JPanel(); //label for employee text salaryLabel = new JLabel("Hourly Rate:"); //Text Field to store employee new and last name salaryValue = new JTextField(); salaryValue.setEditable(true); salaryValue.setColumns(15); //add components to panel salary.add(salaryLabel, BorderLayout.WEST); salary.add(salaryValue, BorderLayout.EAST); //create panel hours = new JPanel(); //label for employee hours empHourLabel = new JLabel("Hours Worked:"); //Text Field to store employee new and last name hourValue = new JTextField(); hourValue.setEditable(true); hourValue.setColumns(15); //add calculate button calcButton = new JButton ("Calculate"); //add action listener to button calcButton.addActionListener(new CalcButtonListener()); //add exit button JButton button = new JButton ("Close"); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); //add components to panel hours.add(empHourLabel, BorderLayout.WEST); hours.add(hourValue, BorderLayout.EAST); hours.add(calcButton, BorderLayout.PAGE_END); hours.add(button, BorderLayout.AFTER_LINE_ENDS); //add panels to content pane add(employeeName, BorderLayout.NORTH); add(salary, BorderLayout.CENTER); add(hours, BorderLayout.SOUTH); //create JTable to add the data from the GUI empData = new JTable(); //pack and display pack(); setVisible(true); } private class CalcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { double salary; double overtime; double totalPay; String sal1; String hr1; //Retrieve Salary amt sal1 = salaryValue.getText(); salAmt = Double.parseDouble(sal1); //Retrieve Hours hr1 = hourValue.getText(); hour = Double.parseDouble(hr1); //debugging print message System.out.println("Reading: " + sal1 + "Reading2: " + hr1 ); //calculate salary salary = (salAmt * 40); overtime = (hour - 40) * (salAmt * 1.5); totalPay = salary + overtime; //Create decimal format object DecimalFormat dollar = new DecimalFormat("0.00"); //Display the total JOptionPane.showMessageDialog(null, "Salary: $" + dollar.format(salary) + "\n" + "Overtime: $" + dollar.format(overtime) + "\n" + "Total Pay: $" + dollar.format(totalPay)); } } public static void main(String[] args) { // TODO code application logic here new payrollCalculatorFrame(); } }
Similar Threads
-
Creating a Database using JTable
By joshuakeithlewis in forum New To JavaReplies: 1Last Post: 12-23-2011, 09:00 AM -
Creating Jtable and reading a txt file of numerical data
By techwiz in forum NetBeansReplies: 1Last Post: 02-22-2011, 01:40 PM -
JTable vs JTextField - which to use for creating "Search" screen
By tashimoto in forum AWT / SwingReplies: 6Last Post: 11-19-2010, 07:44 PM -
JTable and AUTO_RESIZE_OFF and column resize creating extra sapce on end of table.
By asifu9 in forum AWT / SwingReplies: 0Last Post: 11-02-2010, 12:23 PM -
creating a Jtable using swing
By hariza in forum AWT / SwingReplies: 6Last Post: 08-25-2010, 03:14 AM
Bookmarks