0

I wish to add 100 EUR to a JLabel (500 EUR) with a JButton (***). Since I cannot add nor subtract an int value from a String, I don't know what to do.

JButton button;

MyFrame(){
    button = new JButton();
    button.setBounds(200, 400, 250, 100);
    button.addActionListener(e -> );
    button.setText("***");

    JLabel amount = new JLabel("500 EUR");
    amount.setBounds(35, 315, 500, 100);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setSize(1200, 850);
    frame.add(button);
    frame.add(amount);
    frame.setLayout(null);
    frame.setVisible(true);
}

GUI

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
drewwurst
  • 13
  • 2
  • 1
    Please at least show an *attempt* to add code to that ActionListener. There are tutorials galore that can help you learn how to use these, and this is not a code-writing service nor a "please point me in the right direction" site. – Hovercraft Full Of Eels Aug 22 '21 at 17:25
  • 2
    *Since I cannot add nor subtract an int value from a string,* -you need to recreate the String. So you need to keep an int variable with the current int value of the label. Then every time you press the button you 1) increment the value 2) recreate String text to be used for the label and 3) reset the text of the label. – camickr Aug 22 '21 at 17:52
  • 1
    I’m confused as to why this was closed. I feel like the main problem was that OP was not able to convert the String into an int from the JLabel and not really a matter of adding an action listener to the JButton – LuckyBandit74 Aug 22 '21 at 18:01
  • Like @camickr said, or you can alternatively get the text from the label, split the label with the split method with a space “ “ split, get the first element, and then increment it’s value and set it back to the label: amount.setText((Integer.parseInt(amount.getText().split(“ “)[0]) + 100) + “ EUR”); – LuckyBandit74 Aug 22 '21 at 18:04
  • @LuckyBandit74 *I’m confused as to why this was closed* - agreed. The OP posted code for adding the ActionListener, they just don't know what to put in the ActionListener. I reopened it. – camickr Aug 22 '21 at 18:09
  • Store the amount in Euros as an `int euros = 500;` value as a class attribute. When the button is clicked, call `amount.setText(++euros + " EUR");`. Done! – Andrew Thompson Aug 22 '21 at 19:10
  • 1
    `frame.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Aug 22 '21 at 19:33

1 Answers1

1

How to change (add or subtract) the value of a JLabel?
... Since I cannot add nor subtract an int value from a String, I don't know what to do.

This is thinking about the problem in a slightly wrong way. The number of Euros should be held separately as an int value. When an action is performed, increment the value and set it to the string with a suffix of " EUR".

Here is a complete (ready to run) example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class EuroCounter {

    private JComponent ui = null;
    int euros = 1000;
    String suffix = " EUR";
    JLabel amount = new JLabel(euros + suffix, JLabel.CENTER);

    EuroCounter() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        JButton button;

        button = new JButton("+");
        button.setMargin(new Insets(20, 40, 20, 40));
        ActionListener incrementListener = (ActionEvent e) -> {
            amount.setText((euros+=100) + suffix);
        };
        button.addActionListener(incrementListener);
        JPanel leftAlign = new JPanel(new FlowLayout(FlowLayout.LEADING));
        leftAlign.add(button);
        ui.add(leftAlign, BorderLayout.PAGE_START);

        amount.setBorder(new EmptyBorder(30, 150, 30, 150));
        amount.setFont(amount.getFont().deriveFont(50f));
        ui.add(amount, BorderLayout.CENTER);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            EuroCounter o = new EuroCounter();
            
            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433