Refresh JTable using Vectors strings - java

I'm trying to refresh my table but it won't refresh. I tried using the fireTableDataChanged() as well as creating a new model but my Table will not budge. I can get the initial values in but I can't figure out how to refresh it.
HS = new Vector(Arrays.asList(finalHSArr));
SF = new Vector(Arrays.asList(finalFlagsArr));
I have two separate JTables that I am trying to refresh and these are the new values above. I've been trying for 6 hours now.
public class TemplateGui extends JFrame {
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField textField;
private static String [] sortedRoles_Flags,finalFlagsArr,finalHSArr;
private static String finalFlags="",finalHS="",columnToConvert="Result";
private Vector<String> SF,HS,column;
private JTable hotelSecurityTable,securityFlagsTable;
private DefaultTableModel hsTableModel,sfTableModel;
public TemplateGui(){
super("Galaxy Template Generator V1.0");
//column name
column = new Vector(Arrays.asList(columnToConvert));
getContentPane().setForeground(new Color(0, 0, 0));
getContentPane().setLayout(null);
getContentPane().setBackground(new Color(51, 51, 51));
//radio buttons
JRadioButton rdbtnNewRadioButton = new JRadioButton("Central User ");
rdbtnNewRadioButton.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnNewRadioButton.setBackground(Color.WHITE);
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(222, 75, 127, 36);
getContentPane().add(rdbtnNewRadioButton);
final JRadioButton rdbtnPropertyUser = new JRadioButton("Property User");
rdbtnPropertyUser.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnPropertyUser.setBackground(Color.WHITE);
buttonGroup.add(rdbtnPropertyUser);
rdbtnPropertyUser.setBounds(222, 38, 127, 34);
getContentPane().add(rdbtnPropertyUser);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 18));
textField.setBounds(10, 35, 53, 34);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblHotelSecurity = new JLabel("Hotel Security (H S)");
lblHotelSecurity.setHorizontalAlignment(SwingConstants.CENTER);
lblHotelSecurity.setFont(new Font("Tahoma", Font.BOLD, 13));
lblHotelSecurity.setBounds(10, 144, 189, 23);
lblHotelSecurity.setBackground(new Color(204, 204, 204));
lblHotelSecurity.setOpaque(true);
getContentPane().add(lblHotelSecurity);
JLabel label = new JLabel("Security Flags (S F)");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Tahoma", Font.BOLD, 13));
label.setBounds(222, 144, 372, 23);
label.setBackground(new Color(204, 204, 204));
label.setOpaque(true);
getContentPane().add(label);
JLabel lblEnterTemplateCode = new JLabel("ENTER TEMPLATE CODE");
lblEnterTemplateCode.setForeground(new Color(255, 255, 255));
lblEnterTemplateCode.setFont(new Font("Tahoma", Font.BOLD, 14));
lblEnterTemplateCode.setBounds(10, 9, 175, 23);
getContentPane().add(lblEnterTemplateCode);
JLabel lblSelectUserRole = new JLabel("SELECT USER ROLE LEVEL");
lblSelectUserRole.setForeground(new Color(255, 255, 255));
lblSelectUserRole.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSelectUserRole.setBounds(222, 13, 195, 14);
getContentPane().add(lblSelectUserRole);
//Submit button action
Button button = new Button("Generate Template");
button.setFont(new Font("Dialog", Font.BOLD, 12));
button.setBackground(new Color(102, 255, 102));
button.setForeground(Color.BLACK);
button.setBounds(467, 83, 127, 41);
getContentPane().add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Query excell = new Query();
//get template text
String template = textField.getText().toUpperCase();
System.out.println(template);
if(rdbtnPropertyUser.isSelected()){
try {
//property user was selected
excell.runProcess(1);
System.out.println("you selected Property user");
} catch (IOException e) {
e.printStackTrace();
}
}
else{
try {
//Central User was selected
excell.runProcess(2);
System.out.println("you selected central user");
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("NOW WERE HERE");
//get static variables from Excel Query
for(int i = 0; i< Query.sortedGF.length; i++)
{
if(Query.sortedGF[i].contains(template)){
sortedRoles_Flags =Query.sortedGF[i].split(" ");
System.out.println("THIS RAN"+" :"+i);
break;
}
}
System.out.println("NOW WERE HERE 103 " +Query.securityFlags.length);
//add data to table
int j=0;
int sizeOfFlags = Query.securityFlags.length;
//Add HS to FinalHS Variable only if Yes
for(int i=0;i< sortedRoles_Flags.length-sizeOfFlags;i++)
{
if(sortedRoles_Flags[i].matches("Y|y|Y\\?|\\?Y|y\\?|\\?y"))
{
System.out.println("Hotel security:"+" "+sortedRoles_Flags[i]+" HS Added: "+Query.hotelSecurity[i]);
finalHS += Query.hotelSecurity[i]+" ";
System.out.println("Hotel security:"+" "+finalHS);
}
}
//add Security Flags to Final Flags
for(int i=(sortedRoles_Flags.length-sizeOfFlags);i< sortedRoles_Flags.length;i++)
{
finalFlags += Query.securityFlags[j]+": "+ sortedRoles_Flags[i]+" + ";
j++;
}
//Leave open just incase they would prefer a text file for template in which case we just write it
System.out.println(finalFlags);
System.out.println(finalHS);
//Convert to String Arrays in order to add to our JTable
finalFlagsArr= finalFlags.split("\\+");
finalHSArr = finalHS.split(" ");
//convert to vectors
HS = new Vector<String>(Arrays.asList(finalHSArr));
SF = new Vector<String>(Arrays.asList(finalFlagsArr));
System.out.print(HS);
hsTableModel.fireTableDataChanged();
sfTableModel.fireTableDataChanged();
}
});
//scroll panes for flags
JScrollPane scrollPaneHS = new JScrollPane();
scrollPaneHS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneHS.setBounds(10, 170, 189, 185);
getContentPane().add(scrollPaneHS);
JScrollPane scrollPaneSF = new JScrollPane();
scrollPaneSF.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneSF.setBounds(222, 170, 372, 187);
getContentPane().add(scrollPaneSF);
//tables for updates
hsTableModel = new DefaultTableModel(HS,column);
hotelSecurityTable = new JTable(hsTableModel);
scrollPaneHS.setViewportView(hotelSecurityTable);
sfTableModel = new DefaultTableModel(SF,column);
securityFlagsTable = new JTable(sfTableModel);
scrollPaneSF.setViewportView(securityFlagsTable);
}
}

When you do
HS = new Vector<String>(Arrays.asList(finalHSArr));
SF = new Vector<String>(Arrays.asList(finalFlagsArr));
You change what HS and SF where point at, however, it does not change what, internally, the TableModel is pointing at.
You could use DefaultTableModel#setDataVector, but i might simpler to simply create new TableModels and set them to the JTables instead.
hsTableModel = new DefaultTableModel(HS, column);
hotelSecurityTable.setModel(hsTableModel);
sfTableModel = new DefaultTableModel(HS, column);
securityFlagsTable.setModel(sfTableModel);
As it's pretty much the same thing...
Updated
DefaultTableModel is expecting a Vector of Vectors, but you're supplying a Vector of Strings.
Try something like...
hsTableModel.setRowCount(0);
for (String row : HS) {
hsTableModel.addRow(new Object[]{row});
}
Instead...

Related

How to get value from a text field?

I am creating a program where I can add and delete items. When added, I want the TOTAL panel to get values from itemprice_textfield when the button ADD is clicked.
private JFrame frame;
private JTextField itemname_textfield;
private JTextField itemprice_textfield;
private JButton add_button;
private JComboBox comboBox;
private JButton del_button;
private JLabel itemprice_label;
private JLabel itemname_label;
private JLabel lblNewLabel_3;
private JLabel totalamount_label;
private JTextField textField_2;
private double amount;
private JLabel bigger_itemname_label;
private JLabel bigger_itemprice_label;
private ArrayList <String> itemnames = new ArrayList();
private ArrayList <Double> itemprices = new ArrayList();
private JTable table;
//DefaultTableModel dtm;
//String header [] = new String [] {itemname_textfield, itemprice_textfield};
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EntapaCCE103LabExam1_Frame1 window = new EntapaCCE103LabExam1_Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class lists{ // user defined method lists class
public String itemname;
public double price;
public lists(String itemname, double price)
{
this.itemname = itemname;
this.price = price;
}
public lists(JTextField textField) {
// TODO Auto-generated constructor stub
}
public lists(JTextField textField, JTextField textField_1) {
// TODO Auto-generated constructor stub
}
} // end
public EntapaCCE103LabExam1_Frame1() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(null);
JLabel big_title = new JLabel("MY ORDERS");
big_title.setFont(new Font("Times New Roman", Font.PLAIN, 18));
big_title.setBounds(245, 11, 116, 52);
frame.getContentPane().add(big_title);
itemname_textfield = new JTextField(10); // name text
itemname_textfield.setBounds(412, 268, 109, 20);
frame.getContentPane().add(itemname_textfield);
itemname_textfield.setColumns(10);
itemprice_textfield = new JTextField(); // price text
itemprice_textfield.setBounds(412, 330, 109, 20);
frame.getContentPane().add(itemprice_textfield);
itemprice_textfield.setColumns(10);
add_button = new JButton("ADD ITEM");
add_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String getname = itemname_textfield.getText(); // used to get the input string from user to transfer to the combo box
comboBox.addItem(getname); // used to display the received string inputs
}
});
add_button.setFont(new Font("Serif", Font.PLAIN, 13));
add_button.setBounds(412, 361, 109, 23);
frame.getContentPane().add(add_button);
comboBox = new JComboBox();
comboBox.setBounds(412, 479, 109, 22);
frame.getContentPane().add(comboBox);
del_button = new JButton("DELETE ITEM");
del_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboBox.removeItem(comboBox.getSelectedItem()); // used to delete the selected index in combo box.
}
});
del_button.setFont(new Font("Serif", Font.PLAIN, 11));
del_button.setBounds(412, 512, 109, 23);
frame.getContentPane().add(del_button);
itemprice_label = new JLabel("Enter Item Price:"); // label for item price
itemprice_label.setBounds(412, 311, 109, 14);
frame.getContentPane().add(itemprice_label); // used to display the text from label
itemname_label = new JLabel("Enter Item Name:"); // label for item name
itemname_label.setBounds(412, 249, 102, 14);
frame.getContentPane().add(itemname_label); // used to display the text from label
lblNewLabel_3 = new JLabel("OPERATION");
lblNewLabel_3.setFont(new Font("Sitka Subheading", Font.PLAIN, 17));
lblNewLabel_3.setBounds(419, 185, 102, 32);
frame.getContentPane().add(lblNewLabel_3);
String getname = itemname_textfield.getText();
totalamount_label = new JLabel("TOTAL: " + amount); // amount here
totalamount_label.setFont(new Font("Serif", Font.PLAIN, 13));
totalamount_label.setBounds(412, 130, 136, 20);
frame.getContentPane().add(totalamount_label);
bigger_itemname_label = new JLabel("ITEM NAME");
bigger_itemname_label.setFont(new Font("Serif", Font.PLAIN, 14));
bigger_itemname_label.setBounds(84, 190, 96, 14);
frame.getContentPane().add(bigger_itemname_label);
bigger_itemprice_label = new JLabel("PRICE");
bigger_itemprice_label.setFont(new Font("Serif", Font.PLAIN, 14));
bigger_itemprice_label.setBounds(245, 192, 46, 14);
frame.getContentPane().add(bigger_itemprice_label);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(39, 225, 322, 285);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
table.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
}
}
But I don't know how or what to do since I am new to Java GUI.
Also, please tell me if there's something weird in my code so that I can make improvement.

Java Swing: Change JButton to an image button

I have been trying this for hours. I want to change each of the JButton components to image buttons (They were radio buttons that's why the variable names don't make sense). The for loop inside is just displaying all the images under the button's. Each image is called Option1, Option2, etc. So what I have been trying to do is get rid of the for loop and just have 10 buttons with images on them that you can click.
If someone could help me figure that out that would be great. All I've been able to do is have the button created with an image but the button appears on a completely different window.
public class JDialog2 extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Create the dialog.
*/
public JDialog2(Quiz quiz) {
setBounds(100, 100, 450, 600);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("2、Favourite subject at Hogwarts");
lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 14));
lblNewLabel_1.setBounds(94, 10, 248, 15);
contentPanel.add(lblNewLabel_1);
ButtonGroup btGroup = new ButtonGroup();
for(int i=1; i<=7; i++) {
ImageIcon image1 = new ImageIcon("Option" + i + ".jpg");
image1.setImage(image1.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
JLabel imageLablel1=new JLabel(image1);
contentPanel.add(imageLablel1);
imageLablel1.setBounds(29, 75 * i - 25, 50, 50);
}
JButton radioButton_1 = new JButton("1.Care of Magical Creatures");
radioButton_1.setBounds(29, 25, 226, 23);
contentPanel.add(radioButton_1);
JButton radioButton_2 = new JButton("2.Charms");
radioButton_2.setBounds(29, 50, 158, 23);
contentPanel.add(radioButton_2);
JButton radioButton_3 = new JButton("3.Defense Against the Dark Arts");
radioButton_3.setBounds(29, 75, 255, 23);
contentPanel.add(radioButton_3);
JButton radioButton_4 = new JButton("4.Divination");
radioButton_4.setBounds(29, 100, 121, 23);
contentPanel.add(radioButton_4);
JButton radioButton_5 = new JButton("5.Herbology");
radioButton_5.setBounds(29, 125, 179, 23);
contentPanel.add(radioButton_5);
JButton radioButton_6 = new JButton("6.History of Magic");
radioButton_6.setBounds(29, 150, 158, 23);
contentPanel.add(radioButton_6);
JButton radioButton_7 = new JButton("7.Muggle Studies");
radioButton_7.setBounds(29, 175, 121, 23);
contentPanel.add(radioButton_7);
JButton radioButton_8 = new JButton("8.Potions");
radioButton_8.setBounds(29, 200, 121, 23);
contentPanel.add(radioButton_8);
JButton radioButton_9 = new JButton("9.Study of Ancient Runes");
radioButton_9.setBounds(29, 220, 255, 23);
contentPanel.add(radioButton_9);
JButton radioButton_10 = new JButton("10.Transfiguration");
radioButton_10.setBounds(29, 245, 179, 23);
contentPanel.add(radioButton_10);
btGroup.add(radioButton_1);
btGroup.add(radioButton_2);
btGroup.add(radioButton_3);
btGroup.add(radioButton_4);
btGroup.add(radioButton_5);
btGroup.add(radioButton_6);
btGroup.add(radioButton_7);
btGroup.add(radioButton_8);
btGroup.add(radioButton_9);
btGroup.add(radioButton_10);
JButton btnCommit = new JButton("Commit");
btnCommit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Question question = quiz.getNextQuestion();
int choice = 0;
Enumeration<AbstractButton> en = btGroup.getElements();
while (en.hasMoreElements()) {
AbstractButton ab = en.nextElement();
choice++;
if (ab.isSelected()) {
break;
}
}
question.setSelectedAnswer(choice - 1);
JDialog3 dialog3 = new JDialog3(quiz);
dialog3.setVisible(true);
exit();
}
});
btnCommit.setBounds(300, 138, 93, 23);
contentPanel.add(btnCommit);
}
public void exit(){
this.setVisible(false);
}
}

how to save data to database from JCombobox and JTextarea in eclipse juno

I'm using eclipse Juno to write my code. I would like to input my data to database but when I click the save button I got error on JTextarea and JCombobox.
Here's the error:
Cannot refer to a non-final variable textALAMAT inside an inner class
defined in a different method Cannot refer to a non-final variable
cbJURUSAN inside an inner class defined in a different method
and here's my code:
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(35, 282, 475, 106);
contentPane.add(scrollPane);
tabelmodel = new DefaultTableModel(null,header);
tabel = new JTable();
tabel.setModel(tabelmodel);
scrollPane.setViewportView(tabel);
JLabel lblNim = new JLabel("NIM");
lblNim.setBounds(35, 24, 46, 14);
contentPane.add(lblNim);
JLabel lblNama = new JLabel("NAMA");
lblNama.setBounds(35, 59, 46, 14);
contentPane.add(lblNama);
JLabel lblJurusan = new JLabel("JURUSAN");
lblJurusan.setBounds(35, 97, 46, 14);
contentPane.add(lblJurusan);
JLabel lblAlamat = new JLabel("ALAMAT");
lblAlamat.setBounds(35, 138, 46, 14);
contentPane.add(lblAlamat);
textNIM = new JTextField();
textNIM.setBounds(119, 18, 230, 27);
contentPane.add(textNIM);
textNIM.setColumns(10);
textNAMA = new JTextField();
textNAMA.setColumns(10);
textNAMA.setBounds(119, 53, 230, 27);
contentPane.add(textNAMA);
JComboBox cbJURUSAN = new JComboBox();
cbJURUSAN.setModel(new DefaultComboBoxModel(new String[] {"Teknik Informatika", "Teknik Komputer", "Sistem Informasi", "Sastra Inggris"}));
cbJURUSAN.setBounds(118, 91, 231, 27);
contentPane.add(cbJURUSAN);
final JTextArea textALAMAT = new JTextArea();
textALAMAT.setBounds(119, 133, 354, 90);
contentPane.add(textALAMAT);
JButton btnSave = new JButton("SAVE");
btnSimpan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
String jurusan = "";
if(cbJURUSAN.getSelectedIndex() == 0)
{
jurusan = "Teknik Informatika";
}
else if(cbJURUSAN.getSelectedIndex() == 1)
{
jurusan = "Teknik Komputer";
}
else if(cbJURUSAN.getSelectedIndex() == 2)
{
jurusan = "Sistem Informasi";
}
else if(cbJURUSAN.getSelectedIndex() == 3)
{
jurusan = "Sastra Inggris";
}
try
{
Connection konek = koneksi.getKoneksi();
String query = "INSERT INTO mahasiswa VALUES (?,?,?,?)";
PreparedStatement prepare = konek.prepareStatement(query);
prepare.setInt(1,Integer.parseInt(textNIM.getText()));
prepare.setString(2, textNAMA.getText());
prepare.setString(3, jurusan);
prepare.setString(4, textALAMAT.getText());
prepare.executeUpdate();
JOptionPane.showMessageDialog(null,"Data berhasil ditambahkan ke database");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"Data gagal ditambahkan ke database");
System.out.println(ex);
}
finally
{
getDataTable();
}
}
});
btnSave.setBounds(49, 248, 89, 23);
contentPane.add(btnSimpan);
Can anybody help me to solve this?
The listener requieres you to change the variables textALAMAT, cbJURUSAN to final.
i see you've placed textALAMAT as final, but add it to the later too:
final JComboBox cbJURUSAN = new JComboBox();

Clear button not function

I just started learning about Java programming and have been trying to code a program that calculates the amount of pain that is needed for a certain area of space. The program runs but the clear button is not functioning. I have checked everything and tried everything but still can't find the problem.
Please help!
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
DecimalFormat currency;
JTextField currencyJTextField;
JLabel roomWidthJLabel;
JTextField roomWidthJTextField;
JLabel roomLengthJLabel;
JTextField roomLengthJTextField;
JLabel numberofGallonsJLabel;
JTextField numberofGallonsJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel totalAmountJLabel;
JTextField totalAmountJTextField;
JButton enterJButton;
JButton clearJButton;
double roomWidth;
double roomLength;
double numberofGallons;
double subTotal;
double salesTax;
double totalAmount;
public PaintCalculator()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
// initialize components
// input
roomWidthJLabel = new JLabel ();
roomWidthJLabel.setBounds (50, 20, 150, 20);
roomWidthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJLabel.setText ("Enter Width of Room (Ft):");
roomWidthJLabel.setForeground(black);
roomWidthJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(roomWidthJLabel);
roomWidthJTextField = new JTextField();
roomWidthJTextField.setBounds(230, 20, 100, 20);
roomWidthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomWidthJTextField.setForeground(black);
roomWidthJTextField.setBackground(white);
roomWidthJTextField.setEditable(true);
contentPane.add(roomWidthJTextField);
roomLengthJLabel = new JLabel();
roomLengthJLabel.setBounds(50, 70, 150, 20);
roomLengthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJLabel.setText("Enter Length of Room (Ft):");
roomLengthJLabel.setForeground(black);
roomLengthJLabel.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(roomLengthJLabel);
roomLengthJTextField = new JTextField();
roomLengthJTextField.setBounds(230, 70, 100, 20);
roomLengthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomLengthJTextField.setForeground(black);
roomLengthJTextField.setBackground(white);
roomLengthJTextField.setEditable(true);
contentPane.add(roomLengthJTextField);
// outputs
numberofGallonsJLabel = new JLabel();
numberofGallonsJLabel.setBounds(50, 120, 150, 20);
numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJLabel.setText("Number of Gallons:");
numberofGallonsJLabel.setForeground(black);
numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(numberofGallonsJLabel);
numberofGallonsJTextField = new JTextField();
numberofGallonsJTextField.setBounds(230, 120, 100, 20);
numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
numberofGallonsJTextField.setForeground(black);
numberofGallonsJTextField.setBackground(white);
numberofGallonsJTextField.setEditable(false);
contentPane.add(numberofGallonsJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(50, 170, 150, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("Sub Total:");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setBounds(230, 170, 100, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(false);
contentPane.add(subTotalJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(50, 220, 150, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax (6%):");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
salesTaxJTextField.setBounds(230, 220, 100, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(false);
contentPane.add(salesTaxJTextField);
totalAmountJLabel = new JLabel();
totalAmountJLabel.setBounds(50, 270, 150, 20);
totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJLabel.setText("Total Sales:");
totalAmountJLabel.setForeground(black);
totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(totalAmountJLabel);
totalAmountJTextField = new JTextField();
totalAmountJTextField.setBounds(230, 270, 100, 20);
totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
totalAmountJTextField.setForeground(black);
totalAmountJTextField.setBackground(white);
totalAmountJTextField.setEditable(false);
contentPane.add(totalAmountJTextField);
// control
enterJButton = new JButton();
enterJButton.setBounds(50, 320, 100, 30);
enterJButton.setFont(new Font("Default", Font.BOLD, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(230, 320, 100, 30);
clearJButton.setFont(new Font("Default", Font.BOLD, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
// set properties of application’s window
setTitle("Paint Calculator"); // set title
setSize( 400, 400 ); // set window size
setVisible(true); // display window
}
// main method
public static void main(String[] args)
{
PaintCalculator application = new PaintCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
getRoomWidthInput();
}
/* Try-Catch Method */
public void getRoomWidthInput()
{
try
{
roomWidth = Double.parseDouble(roomWidthJTextField.getText());
getRoomLengthInput();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Width!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomWidthJTextField.setText("");
roomWidthJTextField.requestFocusInWindow();
}
}
public void getRoomLengthInput()
{
try
{
roomLength = Double.parseDouble(roomLengthJTextField.getText());
calculateNumberofGallons();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Length!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomLengthJTextField.setText("");
roomLengthJTextField.requestFocusInWindow();
}
}
public void calculateNumberofGallons()
{ //process data
final double Tax_Rate = .07;
final double priceperGallon = 24.95;
numberofGallons = (roomWidth * roomLength)/ 400;
subTotal = numberofGallons * priceperGallon;
salesTax = subTotal * Tax_Rate;
totalAmount = subTotal + salesTax;
//display results
currency = new DecimalFormat("$0.00");
numberofGallonsJTextField.setText("" + numberofGallons);
subTotalJTextField.setText("" + currency.format(subTotal));
salesTaxJTextField.setText("" + currency.format(salesTax));
totalAmountJTextField.setText("" + currency.format(totalAmount));
}
}

clear jbutton - not clearing my text fields

I have a program to calculate the amount of paint needed to cover certain area. I have a button that that I have coded which is supposed to clear the text field but it is not working. I have used the exact same format in other programs and it has worked before but for some reason it is not working in this particular program. The error says cannot find symbol on the clearjbuttonactionperformed. Please help.
/*
FileName: PaintCalculator
Name: Bhattarai
Date: October.2014
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class PaintCalculator extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
DecimalFormat currency;
JTextField currencyJTextField;
JLabel roomWidthJLabel;
JTextField roomWidthJTextField;
JLabel roomLengthJLabel;
JTextField roomLengthJTextField;
JLabel numberofGallonsJLabel;
JTextField numberofGallonsJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel totalAmountJLabel;
JTextField totalAmountJTextField;
JButton enterJButton;
JButton clearJButton;
double roomWidth;
double roomLength;
double numberofGallons;
double subTotal;
double salesTax;
double totalAmount;
public PaintCalculator()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
// initialize components
// input
roomWidthJLabel = new JLabel ();
roomWidthJLabel.setBounds (50, 20, 150, 20);
roomWidthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJLabel.setText ("Enter Width of Room (Ft):");
roomWidthJLabel.setForeground(black);
roomWidthJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(roomWidthJLabel);
roomWidthJTextField = new JTextField();
roomWidthJTextField.setBounds(230, 20, 100, 20);
roomWidthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomWidthJTextField.setForeground(black);
roomWidthJTextField.setBackground(white);
roomWidthJTextField.setEditable(true);
contentPane.add(roomWidthJTextField);
roomLengthJLabel = new JLabel();
roomLengthJLabel.setBounds(50, 70, 150, 20);
roomLengthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJLabel.setText("Enter Length of Room (Ft):");
roomLengthJLabel.setForeground(black);
roomLengthJLabel.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(roomLengthJLabel);
roomLengthJTextField = new JTextField();
roomLengthJTextField.setBounds(230, 70, 100, 20);
roomLengthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomLengthJTextField.setForeground(black);
roomLengthJTextField.setBackground(white);
roomLengthJTextField.setEditable(true);
contentPane.add(roomLengthJTextField);
// outputs
numberofGallonsJLabel = new JLabel();
numberofGallonsJLabel.setBounds(50, 120, 150, 20);
numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJLabel.setText("Number of Gallons:");
numberofGallonsJLabel.setForeground(black);
numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(numberofGallonsJLabel);
numberofGallonsJTextField = new JTextField();
numberofGallonsJTextField.setBounds(230, 120, 100, 20);
numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
numberofGallonsJTextField.setForeground(black);
numberofGallonsJTextField.setBackground(white);
numberofGallonsJTextField.setEditable(false);
contentPane.add(numberofGallonsJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(50, 170, 150, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("Sub Total:");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setBounds(230, 170, 100, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(false);
contentPane.add(subTotalJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(50, 220, 150, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax (6%):");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
salesTaxJTextField.setBounds(230, 220, 100, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(false);
contentPane.add(salesTaxJTextField);
totalAmountJLabel = new JLabel();
totalAmountJLabel.setBounds(50, 270, 150, 20);
totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJLabel.setText("Total Sales:");
totalAmountJLabel.setForeground(black);
totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(totalAmountJLabel);
totalAmountJTextField = new JTextField();
totalAmountJTextField.setBounds(230, 270, 100, 20);
totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
totalAmountJTextField.setForeground(black);
totalAmountJTextField.setBackground(white);
totalAmountJTextField.setEditable(false);
contentPane.add(totalAmountJTextField);
// control
enterJButton = new JButton();
enterJButton.setBounds(50, 320, 100, 30);
enterJButton.setFont(new Font("Default", Font.BOLD, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(230, 320, 100, 30);
clearJButton.setFont(new Font("Default", Font.BOLD, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
// set properties of application’s window
setTitle("Paint Calculator"); // set title
setSize( 400, 400 ); // set window size
setVisible(true); // display window
}
// main method
public static void main(String[] args)
{
PaintCalculator application = new PaintCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
getRoomWidthInput();
}
/* Try-Catch Method */
public void getRoomWidthInput()
{
try
{
roomWidth = Double.parseDouble(roomWidthJTextField.getText());
getRoomLengthInput();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Width!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomWidthJTextField.setText("");
roomWidthJTextField.requestFocusInWindow();
}
}
public void getRoomLengthInput()
{
try
{
roomLength = Double.parseDouble(roomLengthJTextField.getText());
calculateNumberofGallons();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Length!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomLengthJTextField.setText("");
roomLengthJTextField.requestFocusInWindow();
}
}
public void calculateNumberofGallons()
{ //process data
final double Tax_Rate = .07;
final double priceperGallon = 24.95;
numberofGallons = (roomWidth * roomLength)/ 400;
subTotal = numberofGallons * priceperGallon;
salesTax = subTotal * Tax_Rate;
totalAmount = subTotal + salesTax;
//display results
currency = new DecimalFormat("$0.00");
numberofGallonsJTextField.setText("" + numberofGallons);
subTotalJTextField.setText("" + currency.format(subTotal));
salesTaxJTextField.setText("" + currency.format(salesTax));
totalAmountJTextField.setText("" + currency.format(totalAmount));
}
}

Categories

Resources