0

This is what I have done with the actionlistener that was taught to me in my previous question. I would like to apologize first for my very long code. I already have trimmed it a bit.

package testpanel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;

public class TestPanel {

JFrame Card = new JFrame();
GridBagConstraints c = new GridBagConstraints();
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);

JButton up;
JButton up_1;   JButton up_2;   JButton up_3;   JButton up_4;
JButton up_5;   JButton up_6;   JButton up_7;   JButton up_8;
JButton down;
JButton down_1; JButton down_2; JButton down_3; JButton down_4;
JButton down_5; JButton down_6; JButton down_7; JButton down_8;

JTextField number;
JTextField number_1;    JTextField number_2;    JTextField number_3;
JTextField number_4;    JTextField number_5;    JTextField number_6;
JTextField number_7;    JTextField number_8;

int value1000 = 0;
int value500 = 0;
int value200 = 0;
int value100 = 0;
int value50 = 0;
int value20 = 0;
int value10 = 0;
int value5 = 0;
int value1 = 0;
int valuecoins = 0;

ActionListener upaction;
ActionListener downaction;

public TestPanel(){
        Card.setVisible(true);
    Card.setSize(522,500);
    Card.setTitle("Frequency Distribution");
    Card.setResizable(false);

    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();      
    int x=(int)((dimension.getWidth() - Card.getWidth())/4);
    int y=(int)((dimension.getHeight() - Card.getHeight())/2);

    Card.setLocation(x, y);
            Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        JPanel centerpanel = new JPanel();
        centerpanel.setBorder(etch);
        centerpanel.setPreferredSize(new Dimension(0,324));
        centerpanel.setBackground(new Color(0,120,0));
        centerpanel.setLayout(new GridBagLayout());

        JLabel lbl1000 = new JLabel("1,000.00");
        lbl1000.setFont(new Font("Calibri", Font.BOLD, 20));
        lbl1000.setForeground(Color.YELLOW);
        c.fill = GridBagConstraints.WEST;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(lbl1000,c);

    JLabel multiply = new JLabel("X");
    multiply.setFont(new Font("Calibri", Font.BOLD, 20));
    multiply.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 1;
    c.gridy = 0;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(multiply,c);

    final JTextField number = new JTextField();
    number.setText(value100+"");
    number.setColumns(3);
    number.setFont(new Font("Calibri", Font.BOLD, 20));
    number.setForeground(Color.BLACK);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 2;
    c.gridy = 0;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(number,c);

    upaction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == up){
                value100++;
                    if(value100>0)
                    {
                        number.setText(value100+"");
                        down.setEnabled(true);
                    }}
            if(e.getSource() == up_1){
                value500++;
                    if(value500>0)
                    {
                        number_1.setText(value500+"");
                        down_1.setEnabled(true);
                    }}
            if(e.getSource() == up_2){
                value200++;
                    if(value200>0)
                    {
                        number_2.setText(value200+"");
                        down_2.setEnabled(true);
                    }}
            if(e.getSource() == up_3){
                value100++;
                    if(value100>0)
                    {
                        number_3.setText(value100+"");
                        down_3.setEnabled(true);
                    }}
            if(e.getSource() == up_4){
                value50++;
                    if(value50>0)
                    {
                        number_4.setText(value50+"");
                        down_4.setEnabled(true);
                    }}

        }
    };

    downaction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == down){
                value1000--;
                    if(value1000>0)
                    {number.setText(value1000+"");}
                    if(value1000==0)
                    {
                        number.setText(value1000+"");
                        down.setEnabled(false);     
                        value1000=0;
                    }}
            if(e.getSource() == down_1){
                value500--;
                    if(value500>0)
                    {number_1.setText(value500+"");}
                    if(value500==0)
                    {
                        number_1.setText(value500+"");
                        down_1.setEnabled(false);   
                        value500=0;
                    }}

            if(e.getSource() == down_2){
                value200--;
                    if(value200>0)
                    {number_2.setText(value200+"");}
                    if(value200==0)
                    {
                        number_2.setText(value200+"");
                        down_2.setEnabled(false);   
                        value200=0;
                    }}
            if(e.getSource() == down_3){
                value100--;
                    if(value100>0)
                    {number_3.setText(value100+"");}
                    if(value100==0)
                    {
                        number_3.setText(value100+"");
                        down_3.setEnabled(false);   
                        value100=0;
                    }}      
            if(e.getSource() == down_4){
                value50--;
                    if(value50>0)
                    {number_4.setText(value50+"");}
                    if(value50==0)
                    {
                        number_4.setText(value50+"");
                        down_4.setEnabled(false);   
                        value50=0;
                    }}


        }
    };

        up = new JButton("up");
        up.setBackground(new Color(224,223,227));
    up.setPreferredSize(new Dimension(5,15));
    up.addActionListener(upaction);


    down = new JButton("down");
    down.setBackground(new Color(224,223,227));
    down.setPreferredSize(new Dimension(5,15));
    down.setEnabled(false);
        down.addActionListener(downaction);

    JPanel updownpanel = new JPanel();
    updownpanel.setPreferredSize(new Dimension(10,10));
    updownpanel.setLayout(new BorderLayout());
    updownpanel.add(up, BorderLayout.NORTH);
    updownpanel.add(down, BorderLayout.SOUTH);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 3;
    c.gridy = 0;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(updownpanel,c);

    JLabel equals = new JLabel("=");
    equals.setFont(new Font("Calibri", Font.BOLD, 20));
    equals.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 4;
    c.gridy = 0;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(equals,c);

    JLabel total = new JLabel();
    total.setText(value100+".00");
    total.setFont(new Font("Calibri", Font.BOLD, 20));
    total.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.EAST;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 0;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(total,c);

    //-----------------------------------------------------------------------------------------

    JLabel lbl500 = new JLabel("500.00");
    lbl500.setFont(new Font("Calibri", Font.BOLD, 20));
    lbl500.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.WEST;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(lbl500,c);

    JLabel multiply_1 = new JLabel("X");
    multiply_1.setFont(new Font("Calibri", Font.BOLD, 20));
    multiply_1.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 1;
    c.gridy = 1;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(multiply_1,c);

    number_1 = new JTextField();
    number_1.setText(value500+"");
    number_1.setColumns(3);
    number_1.setFont(new Font("Calibri", Font.BOLD, 20));
    number_1.setForeground(Color.BLACK);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 2;
    c.gridy = 1;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(number_1,c);

    up_1 = new JButton("up");
    up_1.setBackground(new Color(224,223,227));
    up_1.setPreferredSize(new Dimension(5,15));
    up_1.addActionListener(upaction);

    down_1 = new JButton("down");
    down_1.setBackground(new Color(224,223,227));
    down_1.setPreferredSize(new Dimension(5,15));
    down_1.setEnabled(false);
    down_1.addActionListener(downaction);

    JPanel updownpanel_1 = new JPanel();
    updownpanel_1.setPreferredSize(new Dimension(10,10));
    updownpanel_1.setLayout(new BorderLayout());
    updownpanel_1.add(up_1, BorderLayout.NORTH);
    updownpanel_1.add(down_1, BorderLayout.SOUTH);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 3;
    c.gridy = 1;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(updownpanel_1,c);

    JLabel equals_1 = new JLabel("=");
    equals_1.setFont(new Font("Calibri", Font.BOLD, 20));
    equals_1.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 4;
    c.gridy = 1;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(equals_1,c);

    JLabel total_1 = new JLabel();
    total_1.setText(value500+".00");
    total_1.setFont(new Font("Calibri", Font.BOLD, 20));
    total_1.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.EAST;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 1;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(total_1,c);

    //-----------------------------------------------------------------------------------------

    JLabel lbl200 = new JLabel("200.00");
    lbl200.setFont(new Font("Calibri", Font.BOLD, 20));
    lbl200.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.WEST;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(lbl200,c);

    JLabel multiply_2 = new JLabel("X");
    multiply_2.setFont(new Font("Calibri", Font.BOLD, 20));
    multiply_2.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 1;
    c.gridy = 2;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(multiply_2,c);

    number_2 = new JTextField();
    number_2.setText(value200+"");
    number_2.setColumns(3);
    number_2.setFont(new Font("Calibri", Font.BOLD, 20));
    number_2.setForeground(Color.BLACK);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 2;
    c.gridy = 2;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(number_2,c);

        up_2 = new JButton("up");
        up_2.setBackground(new Color(224,223,227));
    up_2.setPreferredSize(new Dimension(5,15));
    up_2.addActionListener(upaction);

    down_2 = new JButton("down");
    down_2.setBackground(new Color(224,223,227));
    down_2.setPreferredSize(new Dimension(5,15));
        down_2.setEnabled(false);
    down_2.addActionListener(downaction);

    JPanel updownpanel_2 = new JPanel();
    updownpanel_2.setPreferredSize(new Dimension(10,10));
    updownpanel_2.setLayout(new BorderLayout());
    updownpanel_2.add(up_2, BorderLayout.NORTH);
    updownpanel_2.add(down_2, BorderLayout.SOUTH);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 3;
    c.gridy = 2;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(updownpanel_2,c);

    JLabel equals_2 = new JLabel("=");
    equals_2.setFont(new Font("Calibri", Font.BOLD, 20));
    equals_2.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 4;
    c.gridy = 2;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(equals_2,c);

    JLabel total_2 = new JLabel();
    total_2.setText(value200+".00");
    total_2.setFont(new Font("Calibri", Font.BOLD, 20));
    total_2.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.EAST;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 2;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(total_2,c);

    //-----------------------------------------------------------------------------------------

    JLabel lbl100 = new JLabel("100.00");
    lbl100.setFont(new Font("Calibri", Font.BOLD, 20));
    lbl100.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.WEST;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 3;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(lbl100,c);

    JLabel multiply_3 = new JLabel("X");
    multiply_3.setFont(new Font("Calibri", Font.BOLD, 20));
    multiply_3.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 1;
    c.gridy = 3;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(multiply_3,c);

    number_3 = new JTextField();
    number_3.setText(value100+"");
    number_3.setColumns(3);
    number_3.setFont(new Font("Calibri", Font.BOLD, 20));
    number_3.setForeground(Color.BLACK);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 2;
    c.gridy = 3;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(number_3,c);

    up_3 = new JButton("up");
    up_3.setBackground(new Color(224,223,227));
    up_3.setPreferredSize(new Dimension(5,15));
    up_3.addActionListener(upaction);

    down_3 = new JButton("down");
    down_3.setBackground(new Color(224,223,227));
    down_3.setPreferredSize(new Dimension(5,15));
    down_3.setEnabled(false);
    down_3.addActionListener(downaction);

    JPanel updownpanel_3 = new JPanel();
    updownpanel_3.setPreferredSize(new Dimension(10,10));
    updownpanel_3.setLayout(new BorderLayout());
    updownpanel_3.add(up_3, BorderLayout.NORTH);
    updownpanel_3.add(down_3, BorderLayout.SOUTH);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 3;
    c.gridy = 3;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(updownpanel_3,c);

    JLabel equals_3 = new JLabel("=");
    equals_3.setFont(new Font("Calibri", Font.BOLD, 20));
    equals_3.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 4;
    c.gridy = 3;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(equals_3,c);

    JLabel total_3 = new JLabel();
    total_3.setText(value100+".00");
    total_3.setFont(new Font("Calibri", Font.BOLD, 20));
    total_3.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.EAST;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 3;
    c.insets = new Insets(0,30,0,0);        
    centerpanel.add(total_3,c);

    JLabel coins = new JLabel("Coins:");
    coins.setFont(new Font("Calibri", Font.BOLD, 20));
    coins.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 4;
    c.gridy = 9;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(coins,c);

    JTextField txttotal = new JTextField();
    txttotal.setText(valuecoins+"");
    txttotal.setColumns(6);
    txttotal.setFont(new Font("Calibri", Font.BOLD, 20));
    txttotal.setForeground(Color.BLACK);
    c.fill = GridBagConstraints.EAST;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 9;
    c.insets = new Insets(0,0,0,35);        
    centerpanel.add(txttotal,c);        

    JLabel divider = new JLabel("---------------------");
    divider.setFont(new Font("Calibri", Font.BOLD, 20));
    divider.setForeground(Color.GRAY);
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 10;
    c.insets = new Insets(0,0,0,0);     
    centerpanel.add(divider,c);

    JLabel txtCoinTotal = new JLabel();
    txtCoinTotal.setText(valuecoins+".00");
    txtCoinTotal.setFont(new Font("Calibri", Font.BOLD, 20));
    txtCoinTotal.setForeground(Color.YELLOW);
    c.fill = GridBagConstraints.EAST;
    c.weightx = 1;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 11;
    c.insets = new Insets(0,0,0,35);        
    centerpanel.add(txtCoinTotal,c);        


    Card.add(centerpanel);


}

public static void main(String[] args) {
     EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            TestPanel test = new TestPanel();
        }
    });
  }
}

I have here 4 rows of numbers which has a textfield and an up and down buttons for increment. It's working the way I prefer but if you run the program and clicked the up button once from top to bottom the 4th row will be 2 instead of 1. Even if you add rows still that 4th row always gets 2 instead of 1. If you start clicking from the bottom the first row will be 2. If not the 4th row it is the first one who gets 2. I don't see any problem with my code but it is like that. Also the back button of the first row is not working even though I put the code correctly. I have checked my code more than twice already and I don't see anything that can make it like that.

Community
  • 1
  • 1
user3771102
  • 558
  • 2
  • 8
  • 27
  • `up` and `up_3` are both modifying `value100`. Also don't screw with `setPreferredSize` cause it just makes a complete mess of your UI. I had to remove all the `setPreferredSize` calls to make the UI usable before I could test you code – MadProgrammer Jul 28 '14 at 10:53
  • Why not run the code so you can understand what I'm saying. And I **have** trimmed it already. If I can trim it more I should have done it. – user3771102 Jul 28 '14 at 10:56

1 Answers1

3

If we compare the code for the up actions...

if (e.getSource() == up) {
    value100++;
    if (value100 > 0) {
        number.setText(value100 + "");
        down.setEnabled(true);
    }
}
//...
if (e.getSource() == up_3) {
    value100++;
    if (value100 > 0) {
        number_3.setText(value100 + "");
        down_3.setEnabled(true);
    }
}

With the code of the down actions

if (e.getSource() == down) {
    value1000--;
    if (value1000 > 0) {
        number.setText(value1000 + "");
    }
    if (value1000 == 0) {
        number.setText(value1000 + "");
        down.setEnabled(false);
        value1000 = 0;
    }
}
//...
if (e.getSource() == down_3) {
    value100--;
    if (value100 > 0) {
        number_3.setText(value100 + "");
    }
    if (value100 == 0) {
        number_3.setText(value100 + "");
        down_3.setEnabled(false);
        value100 = 0;
    }
}

We can see a discrepancy...up is modifying value100 where as down is modifying value1000, but up_3 and down_3 are modifying value100...

While I have the moment...

This is what your UI looks like on my PC...

BadScreen

Now, I'm sitting here scratching my head of "..."

Now, if I replace the setSize with pack (called at the end of the constructor) I get...

BadScreen

Now, if I remove all the setPreferredSize calls I get...

BetterScreen

Still needs some work, but is a far (IMHO) better result...

Take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for decisions on the subject...

Oh, I don't know if it's important or not, but you might want to take a look at How to use spinners

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yeah. I see that now. Thank you. I'll try not to use the `setPrefferedsize`. I didn't know cause even though I use a different platform in creating my short ver of my code, the UI doesn't get messed up. I use eclipse and netbeans btw. But Thank you for seeing my simple mistake. I really appreciate it. – user3771102 Jul 28 '14 at 11:04
  • Oh, I don't know if it's important or not, but you might want to take a look at [How to use spinners](http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html) – MadProgrammer Jul 28 '14 at 11:05
  • This is a nit pick on my part, but this one of the reasons I would avoid using `ActionListener` in this way, but that's just me – MadProgrammer Jul 28 '14 at 11:08
  • Ohh. I see. Well your proposed code seems a bit complicated that's why I didn't use it for my code and since I always use `ActionListener` I understand it quickly even though I want to make it using ActionCommand. I only use the preferred size cause it should be icon there and not a `String` up and down. The way it looks like in you pc with it's preferred size is what it really looks like. Since I just need the code I change the `icons` to `String` so that people will know where is the up and down button. :) Thanks anyway! I'll take a look on spinners that you've suggested. – user3771102 Jul 28 '14 at 11:17