JAVA Swing GUI Invoice - problem with correct actionPerformed

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1299 Views - Last Post: 19 April 2018 - 04:34 PM Rate Topic: -----

#1 cavjav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 18

JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 18 April 2018 - 11:57 PM

Hello.
I am trying to prepare a window program GUI to display invoices from the system add a new invoice (and add invoice items as part of the new invoice), modify the unclosed invoice and view the details of the invoice.
The program should be written in Swing without the use of window editors. (only code). I have problem with buttons to adds new invoice with some positions.

my concept
Posted Image


classes:

public class TestInvoice
package zda;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;

public class TestInvoice extends JFrame
{

	public static void main(String[] args)
	{
		Contractor k1 = new Contractor("Some company \nsome adress, adress", "123-123-123");

		Invoice f1 = new Invoice(k1);
		Invoice f2 = new Invoice(k1);


		f1.addPosition("Kredki 3szt", Invoice.Measure.QTY, 5, 3.2, Invoice.VAT.s23);
		f1.addPosition("Flamastry 6szt", Invoice.Measure.QTY, 5, 4.59, Invoice.VAT.s23);
		f1.addPosition("Plastelina 12 kolorów", Invoice.Measure.QTY, 2, 8.22, Invoice.VAT.s23);
		f1.addPosition("Ołówki 3szt", Invoice.Measure.QTY, 1, 6.00, Invoice.VAT.s23);
		f1.addPosition("Ołówkek HB", Invoice.Measure.QTY, 5, 1.2, Invoice.VAT.s08);

		System.out.println(f1);

		f2.addPosition("Kredki 3szt", Invoice.Measure.QTY, 5, 3.2, Invoice.VAT.s23);
		f2.addPosition("Flamastry 6szt", Invoice.Measure.QTY, 5, 4.59, Invoice.VAT.s23);
		f2.addPosition("Plastelina 12 kolorów", Invoice.Measure.QTY, 2, 8.22, Invoice.VAT.s23);
		f2.close();
		f2.addPosition("Ołówki 3szt", Invoice.Measure.QTY, 1, 6.00, Invoice.VAT.s23);
		f2.addPosition("Ołówkek HB", Invoice.Measure.QTY, 5, 1.2, Invoice.VAT.s08);

		System.out.println(f2);
		System.out.println(Math.round(2022.0000000000002) / 100.0);

		new TestInvoice();

		// ----------------------------------------------------------------------------------------
	}

	private InvoiceManager manager;

	public TestInvoice()
	{
		manager = new InvoiceManager(5);

		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

		JPanel leftPanel = new JPanel();
		leftPanel.setSize(400, 500);

		GroupLayout gl = new GroupLayout(leftPanel);
		gl.setAutoCreateGaps(true);
		gl.setAutoCreateContainerGaps(true);
		leftPanel.setLayout(gl);

		SpringLayout sl = new SpringLayout();
		JPanel rightPanel = new JPanel(sl);
		rightPanel.setSize(400, 500);

		JSplitPane sp = new JSplitPane();
		sp.setRightComponent(rightPanel);
		sp.setLeftComponent(leftPanel);
		sp.setPreferredSize(new Dimension(600, 600));

		JLabel lPositionsName = new JLabel("name");
		JLabel lPositionsMeasure = new JLabel("Measure");
		JLabel lPositionsQty = new JLabel("qty");
		JLabel lPositionsPrice = new JLabel("price");
		JLabel lPositionsTax = new JLabel("podatek");

		JTextField tfName = new JTextField("", 20);
		JFormattedTextField tfMeasure = new JFormattedTextField(NumberFormat.getNumberInstance());
		JFormattedTextField tfQty = new JFormattedTextField(NumberFormat.getNumberInstance());
		JFormattedTextField tfPrice = new JFormattedTextField(NumberFormat.getNumberInstance());
		JFormattedTextField tfTax = new JFormattedTextField(NumberFormat.getNumberInstance());

		gl.setVerticalGroup(gl.createSequentialGroup()
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsName)
						.addComponent(tfName))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsMeasure)
						.addComponent(tfMeasure))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsQty)
						.addComponent(tfQty))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsPrice)
						.addComponent(tfPrice))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsTax)
						.addComponent(tfTax)));

		gl.setHorizontalGroup(gl.createSequentialGroup()
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(lPositionsName)
						.addComponent(lPositionsMeasure).addComponent(lPositionsQty).addComponent(lPositionsPrice)
						.addComponent(lPositionsTax))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(tfName)
						.addComponent(tfMeasure).addComponent(tfQty).addComponent(tfPrice).addComponent(tfTax)));

		JList<Invoice> list = new JList<Invoice>();
		list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		JScrollPane listScroller = new JScrollPane(list);
		listScroller.setPreferredSize(new Dimension(250, 80));
		rightPanel.add(listScroller);

		JLabel error = new JLabel("");
		error.setForeground(Color.red);

		JButton b = new JButton("save");
		b.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				String name = tfName.getText();

				int Measure = ((Number) tfMeasure.getValue()).intValue();
				int qty = ((Number) tfQty.getValue()).intValue();
				int price = ((Number) tfPrice.getValue()).intValue();
				int tax = ((Number) tfTax.getValue()).intValue();

			}

		});

		JButton o = new JButton("read");
		o.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				list.setListData(manager.getList());
			}
		});

		JButton u = new JButton("delete");
		u.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				List<Invoice> listt = list.getSelectedValuesList();

				for (Invoice w : listt)
				{
					manager.removeInvoice(w);
				}
				list.setListData(manager.getList());
			}
		});

		getContentPane().add(sp);
		getContentPane().add(error);
		getContentPane().add(B)/>/>;
		getContentPane().add(o);
		getContentPane().add(u);

		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
}






public class Invoice
package zda;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;


public class Invoice
{

	private Contractor client;
	private LocalDate dateOfIssue;

	private String nr;

	private static class Identifier
	{
		static int currentNumber = 0;
		static int currentMonth = 0;
		static int currentYear = 0;

		static String generateNr()
		{
			LocalDate ld = LocalDate.now();

			int year = ld.getYear();
			int month = ld.getMonthValue();
			if (year > currentYear)
			{
				currentYear = year;
				currentMonth = ld.getMonthValue();
				currentNumber = 0;
			} else if (month > currentMonth)
			{
				currentMonth = month;
				currentNumber = 0;
			}

			return (++currentNumber) + "/" + currentMonth + "/" + currentYear;

		}
	}

	public enum Measure
	{
		QTY, M, L, KG, M2
	}

	public enum VAT
	{
		s23(.23), s08(.08), s05(.05), s00(.0);

		public double rate;
		public String str;

		VAT(double rate)
		{
			this.rate = rate;
			this.str = String.format("%.0f%%", 100 * rate);
		}
	}

	private class Position
	{
		private int position;
		private String name;
		private Measure mmasure;
		private double quantity;
		private double unitPriceWithoutTax;
		private VAT tax;

		public Position(String name, Measure mmasure, double quantity, double price, VAT tax)
		{
			this.name = name;
			this.mmasure = mmasure;
			this.quantity = quantity;
			this.unitPriceWithoutTax = price;
			this.tax = tax;
			this.position = positions.size() + 1;
		}

		public double getValue()
		{
			return quantity * unitPriceWithoutTax;
		}

		public double getTax()
		{
			double d = quantity * unitPriceWithoutTax * tax.rate;
			d = Math.round(d * 100) / 100.;
			return d;
		}

		@Override
		public String toString()
		{
			return String.format("%5d | %30s | %10s | %10s | %10s | %10s | %10.2f", position, name, mmasure, quantity,
					unitPriceWithoutTax, tax.str, getValue() + getTax());
		}
	}

	public static String heading()
	{
		return String.format("%5s | %30s | %10s | %10s | %10s | %10s | %10s", "LP.", "name", "mmasure", "quantity",
				"Unit price.", "tax", "price with tax");
	}

	private ArrayList<Position> positions = new ArrayList<Position>();

	public void addPosition(String name, Measure m, double quantity, double price, VAT tax)
	{
		if (closed)
			return;

		Position p = new Position(name, m, quantity, price, tax);
		positions.add(p);
		double d = subtotals.get(tax);
		d += p.getTax();
		subtotals.put(tax, d);
		sumWithTax += p.getValue();
		sumPayment += p.getValue() + p.getTax();
	}

	private boolean closed = false;

	public void close()
	{
		closed = true;
	}

	private HashMap<VAT, Double> subtotals = new HashMap<>();
	private double sumWithTax = 0;
	private double sumPayment = 0;

	public Invoice(Contractor c)
	{
		nr = Identifier.generateNr();
		client = c;
		dateOfIssue = LocalDate.now();

		for (VAT v : VAT.values())
		{
			subtotals.put(v, 0.);
		}
	}

	@Override
	public String toString()
	{
		String invoice = "---------------------------------------------\n";
		invoice += (closed ? "C-" : "O-") + "invoice no." + nr + "\n";
		invoice += "from date " + dateOfIssue.format(DateTimeFormatter.ofPattern("d-M-y")) + "\n";
		invoice += "\nFor: \n" + client + "\n\n";
		invoice += heading() + "\n";
		for (Position p : positions)
		{
			invoice += p.toString() + "\n";
		}
		invoice += "\n";
		invoice += String.format("%-15s: %10.2f\n", "Sum without tax", sumWithTax);
		for (HashMap.Entry<VAT, Double> e : subtotals.entrySet())
		{
			if (e.getValue() > 0)
				invoice += String.format("%-15s: %10.2f\n", "SUM " + e.getKey().str, e.getValue());
		}
		invoice += String.format("%27s\n", "+ ----------");
		invoice += String.format("%-15s: %10.2f\n", "sum to pay", sumPayment);
		invoice += "---------------------------------------------\n";
		return invoice;
	}

}




public class InvoiceManager
package zda;

import java.util.ArrayList;

public class InvoiceManager
{

	private int maxNumber = 10;
	private ArrayList<Invoice> listOfInvoices = new ArrayList<Invoice>();
	
	public void addInvoice(Invoice w) throws IllegalArgumentException{
		if(listOfInvoices.size()< maxNumber)
			listOfInvoices.add(w);
		else 
			throw new IllegalArgumentException("No space for a new invoice");
	}
	public void removeInvoice(Invoice w){
		listOfInvoices.remove(w);
	}
	public Invoice[] getList(){
		return (Invoice[]) listOfInvoices.toArray(new Invoice[listOfInvoices.size()]);
	}
	
	public InvoiceManager(int max){
		maxNumber = max;
	}
	
}




public class Contractor
package zda;

public class Contractor
{
	String data;
	String id;

	public Contractor(String data, String id)
	{
		this.data = data;
		this.id = id;
	}

	@Override
	public String toString()
	{
		return data + "\n" + "NIP: " + id;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: JAVA Swing GUI Invoice - problem with correct actionPerformed

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 828
  • View blog
  • Posts: 3,350
  • Joined: 13-June 14

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 12:17 AM

What problem are you having and what have you tried to resolve it? Are there compiler errors? If so, what are they and what do you think they mean?
Was This Post Helpful? 1
  • +
  • -

#3 wseng92   User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 435
  • Joined: 23-September 15

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 12:34 AM

Also posted here
https://coderanch.co...Invoice-correct

This post has been edited by wseng92: 19 April 2018 - 12:36 AM

Was This Post Helpful? 1
  • +
  • -

#4 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 01:24 AM

And also posted here

https://www.codeproj...orrect-act.aspx
Was This Post Helpful? 1
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 03:45 AM

Quote

I have problem with buttons to adds new invoice with some positions.

So you're looking for a free software enhancement / feature addition?

This post has been edited by g00se: 19 April 2018 - 03:46 AM

Was This Post Helpful? 1
  • +
  • -

#6 cavjav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 18

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 03:52 AM

I apologize for my previous post that it was not precisely specified.
I have a program that displays invoices f1 and f2 of the client in the console and I want to extend it with a GUI graphic layout, which will allow adding a new invoice f3 (f4, f5 ...) along with the items using the button.
And I do not know how to do it.

I can see the GUI as follows:
Posted Image


These are not compiler errors, I just do not know how to write a code to get in gui: adding invoices, adding items to the invoice, editing invoice notifications and presenting the entire invoice.
I think about it for a few days and I do not know how to write it.


Other problem:
I have problem with swing connected with enum (tax) and calculations. Please look below.

public enum VAT // different tax values
 
 { 
 s23(0.23), s08(0.08), s05(0.05), s00(0.0);}
 
 
public class Window extends JFrame
{...
{...}
 
JFormattedTextField tfQty = new JFormattedTextField(NumberFormat.getNumberInstance());
 JFormattedTextField tfPriceNet = new JFormattedTextField(NumberFormat.getNumberInstance());
JComboBox<VAT> cbTax = new JComboBox<>();
cbTax.setModel(new DefaultComboBoxModel<>(VAT.values()));
JFormattedTextField tfPriceGross = new JFormattedTextField(NumberFormat.getNumberInstance());
 
}


And then after run user will input qty, pricenet and tax, program should then get these values, change into double, count PriceGross but it doesn't work. What is the reason? What I do wrong?
JButton p = new JButton("Calculate the value of the gross payment");
 p.addActionListener(new ActionListener()
 {
 
 public void actionPerformed(ActionEvent e)
 {
 qty= Double.parseDouble(tfIlosc.getText());
 priceNet = Double.parseDouble(tfPriceNEt.getText());
 tax= Double.parseDouble(cbTax.getText());
 priceG = qty*priceNet *tax;
 tfPriceGross .setText(String.valueOf(priceG ));
 }
 }); 



snoopy11, wseng92 I'm desperate ;)
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 828
  • View blog
  • Posts: 3,350
  • Joined: 13-June 14

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 04:38 AM

View Postcavjav, on 19 April 2018 - 11:52 AM, said:

I apologize for my previous post that it was not precisely specified.
I have a program that displays invoices f1 and f2 of the client in the console and I want to extend it with a GUI graphic layout, which will allow adding a new invoice f3 (f4, f5 ...) along with the items using the button.
And I do not know how to do it.


How have you thought about breaking down the problem, then? You need to break it down into smaller problems that you can either do or work out how to do. Is the problem that you don't know what features Swing provides, or something else?
Was This Post Helpful? 1
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 05:05 AM

Quote

it doesn't work.

What does it do that is not working?
How are you trying to debug the code to see what it is doing?
Add some print statements that print out when different methods are executed and show the values of variables as they are changed.
Was This Post Helpful? 1
  • +
  • -

#9 cavjav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 18

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 03:24 PM

View PostNormR, on 19 April 2018 - 05:05 AM, said:

Quote

it doesn't work.

What does it do that is not working?
How are you trying to debug the code to see what it is doing?
Add some print statements that print out when different methods are executed and show the values of variables as they are changed.


again code:

enum:
	public enum VAT{
		s23(.23), s08(.08), s05(.05), s00(.0);

...........
}


I created JComboBox with enum VAT:

JFormattedTextField tfQty = new JFormattedTextField(NumberFormat.getNumberInstance());
JFormattedTextField tfPriceNet = new JFormattedTextField(NumberFormat.getNumberInstance());

JComboBox<VAT> cbTax = new JComboBox<>();
cbTax.setModel(new DefaultComboBoxModel<>(VAT.values()));

JFormattedTextField tfPriceGross = new JFormattedTextField(NumberFormat.getNumberInstance());




Posted Image


		JButton p = new JButton("Calculate the value of the gross payment");
		p.addActionListener(new ActionListener(){
			
		public void actionPerformed(ActionEvent e)
		{	
			qty= Double.parseDouble(tfIlosc.getText());
			priceNet = Double.parseDouble(tfPriceNEt.getText());
	
			tax = Double.parseDouble(cbTax.getText());
			priceG = qty * priceNet * tax;
			tfPriceGross.setText(String.valueOf(priceG));
		}});	

In line 9(.getText()) I have error*:
*The method getText() is undefined for the type JComboBox<Invoice.VAT>

I dont know how to get double values from enum and calculate the priceG.


Compile Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
	at home.Window$2.actionPerformed(window.java:176)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


I apologize for the chaotic posts.
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 03:30 PM

Quote

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at home.Window$2.actionPerformed(window.java:176)

There was a null value when the code at line 176 was executed. Look at line 176 and find the null value (use a print statement to help find what is null).
Then backtrack in the code to see why that value was null and not a valid value.
Was This Post Helpful? 1
  • +
  • -

#11 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 03:30 PM

You need something more like

tax = Double.parseDouble(cbTax.getSelectedItem().toString());

This post has been edited by g00se: 19 April 2018 - 03:32 PM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#12 cavjav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 18

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 03:58 PM

View Postg00se, on 19 April 2018 - 03:30 PM, said:

You need something more like

tax = Double.parseDouble(cbTax.getSelectedItem().toString());


Error in line 9 disappeared but when compile:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "s23"
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
	at homework.Window$2.actionPerformed(window.java:166)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)



Line 166 is:
[code]tax = Double.parseDouble(cbTax.getSelectedItem().toString());

[/code]
Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 04:19 PM

Sorry - i was forgetting the values aren't numbers. Can you please post the full VAT definition? (That is the enum)

This post has been edited by g00se: 19 April 2018 - 04:20 PM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#14 cavjav   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 18

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 04:21 PM

I added constructor in enum, but I dont know how to "connect" double values with JComboBox, actionPerformed...

public enum VAT //
	{ 
		s23(0.23), s08(0.08), s05(0.05), s00(0.0);

		public double staw;
		public String str;
		
		VAT(double staw){ 
			this.staw = staw;
			this.str = String.format("%.0f%%",100*staw);
		}
	}

Was This Post Helpful? 0
  • +
  • -

#15 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: JAVA Swing GUI Invoice - problem with correct actionPerformed

Posted 19 April 2018 - 04:26 PM

You need to add a getStaw() method to that enum. Then:

int selectedIndex = cbTax.getSelectedIndex();
tax = cbTax.getItemAt(selectedIndex).getStaw();
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2