Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For some reason I can't get the column names to show up in my JTable. Help! **Code in bold** Also, how can I format the

For some reason I can't get the column names to show up in my JTable. Help! **Code in bold**

Also, how can I format the output of pay amount and hourly pay as currency?

import java.text.DecimalFormat; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.text.NumberFormat; import java.util.Vector; import javax.swing.*; import javax.swing.JRadioButton; import javax.swing.JTable; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; import javax.swing.table.DefaultTableModel;

public class EmployeeWages implements PropertyChangeListener, ActionListener { //Creating and naming private variables private JFrame frame; private int ID = 0; private double amount; private double hours; private JRadioButton raise5,raise2, raise1, reset; private ButtonGroup group; private DefaultTableModel model; private DefaultTableModel modelBackup; private JFormattedTextField idField,payField,hoursField; private String[] columnNames; private JButton Calcbtn;

private NumberFormat amountFormat;

public EmployeeWages() { frame = new JFrame("Employee Wages"); makeFrame(); }

private void makeFrame() { // Creating main contentPane wiht a BorderLayout Container mainContainer = frame.getContentPane(); mainContainer.setLayout(new BorderLayout()); mainContainer.add(createTopPanel(), BorderLayout.NORTH); mainContainer.add(createCentralPanel(), BorderLayout.CENTER); mainContainer.add(createRightPanel(), BorderLayout.EAST); frame.setSize(450, 425); frame.setVisible(true); }

private JPanel createTopPanel() { JPanel topPane = new JPanel(); topPane.setLayout(new GridLayout(4, 1)); // Creating and setting alignment of panels JPanel idPane = new JPanel(); idPane.setLayout(new FlowLayout(FlowLayout.LEFT)); JPanel payPane = new JPanel(); payPane.setLayout(new FlowLayout(FlowLayout.LEFT)); JPanel hoursPane = new JPanel(); hoursPane.setLayout(new FlowLayout(FlowLayout.LEFT)); JPanel addPane = new JPanel(); addPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); //Creating "add" button Calcbtn = new JButton("Add"); Calcbtn.setBackground(new java.awt.Color(153, 204, 255)); Calcbtn.setFont(new java.awt.Font("Copperplate", 0, 13)); Calcbtn.addActionListener(this);

idField = new JFormattedTextField(); idField.setName("ID"); idField.setValue(new Integer(ID)); idField.setColumns(10); idField.addPropertyChangeListener(this);

amountFormat = NumberFormat.getNumberInstance(); payField = new JFormattedTextField(amountFormat); payField.setName("payField"); payField.setValue(new Double(amount)); payField.setColumns(10); payField.addPropertyChangeListener(this);

hoursField = new JFormattedTextField(amountFormat); hoursField.setName("hoursField"); hoursField.setValue(new Double(hours)); hoursField.setColumns(10); hoursField.addPropertyChangeListener(this);

idPane.add(new JLabel(" Employee #: ")); idPane.add(idField);

payPane.add(new JLabel(" Hourly Pay: ")); payPane.add(payField);

hoursPane.add(new JLabel(" Hours Worked: ")); hoursPane.add(hoursField);

addPane.add(Calcbtn);

topPane.add(idPane); topPane.add(payPane); topPane.add(hoursPane); topPane.add(addPane); return topPane; }

@Override public void propertyChange(PropertyChangeEvent evt) { JFormattedTextField source = (JFormattedTextField) evt.getSource(); if (source.getName().equals("ID")) { // capture the change in employee id ID = ((Number) source.getValue()).intValue(); } else if (source.getName().equals("payField")) { // capture the change in pay field amount = ((Number) source.getValue()).doubleValue(); } else if (source.getName().equals("hoursField")) { // capture the change in hours field hours = ((Number) source.getValue()).doubleValue(); } }

private JPanel createCentralPanel() { JPanel centralPane = new JPanel(); centralPane.setLayout(new GridLayout(1, 0));

model = new DefaultTableModel(); model.addColumn("Employees"); model.addColumn("Hourly Pay"); model.addColumn("Hours Worked"); model.addColumn("Pay Amount");

modelBackup = new DefaultTableModel(null, new String[] {"Pay Amount"});

JTable table = new JTable(model);

centralPane.add(table); return centralPane;

}

@Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source instanceof JButton && ((AbstractButton) source).getText().equals("Add")) { int empId = ID; double hourlyPay = amount; double hour = hours; double payAmt; if (hour > 40) { payAmt = 40 * hourlyPay + 1.5 * hourlyPay * (hour - 40); } else { payAmt = hour * hourlyPay; } model.addRow(new Object[] {empId, hourlyPay, hour, payAmt});

modelBackup.addRow(new Object[] {payAmt}); }

if (source instanceof JRadioButton) { JRadioButton radio = (JRadioButton) source; if ("reset".equals(radio.getActionCommand())) { raise5.setSelected(false); raise2.setSelected(false); raise1.setSelected(false);

restorePayAmt(); } if ("5%".equals(radio.getActionCommand())) {

// raise double raise = 1.05; raise(raise);

} if ("2%".equals(radio.getActionCommand())) { double raise = 1.02; raise(raise); } if ("1%".equals(radio.getActionCommand())) { double raise = 1.01; raise(raise); }

}

}

private void restorePayAmt() { int rows = model.getRowCount();

for (int i = 0; i < rows; i++) { Vector row = (Vector) model.getDataVector().get(i); Vector rowBackup = (Vector) modelBackup.getDataVector().get(i); double backupPay = (double) rowBackup.get(0); row.set(3, backupPay); }

model.fireTableDataChanged();

}

private void raise(double raise) { int rows = model.getRowCount();

for (int i = 0; i < rows; i++) { Vector row = (Vector) model.getDataVector().get(i); double hourlyPay = (double) row.get(1); double hour = (double) row.get(2); double payAmt; if (hour > 40) { payAmt = 40 * (hourlyPay * raise) + 1.5 * (hourlyPay * raise) * (hour - 40); } else { payAmt = hour * hourlyPay * raise; }

row.set(3, payAmt);

}

model.fireTableDataChanged(); };

private JPanel createRightPanel() { JPanel rightPanel = new JPanel(); rightPanel.setLayout(new GridLayout(4,1)); raise5 = new JRadioButton("1%"); raise5.setActionCommand("1%"); raise2 = new JRadioButton("2%"); raise2.setActionCommand("2%"); raise1 = new JRadioButton("5%"); raise1.setActionCommand("5%"); reset = new JRadioButton("Reset"); reset.setActionCommand("reset"); group = new ButtonGroup(); group.add(raise5); group.add(raise2); group.add(raise1); group.add(reset); rightPanel.add(raise5); rightPanel.add(raise2); rightPanel.add(raise1); rightPanel.add(reset);

reset.addActionListener(this); raise5.addActionListener(this); raise2.addActionListener(this); raise1.addActionListener(this);

rightPanel.setBorder(new TitledBorder(new EtchedBorder(), "Raises")); return rightPanel;

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() { new EmployeeWages().makeFrame(); }

}); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 1 Lncs 8055

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013 Edition

3642402844, 978-3642402845

More Books

Students also viewed these Databases questions