简体   繁体   中英

How to make int variable visible to other class from GUI JButton

I'm trying to access the int myInt in this GUI form class from another class. The method does pass the input variable to myInt properly but I have not been able to make it visible to the other java file. I have been reading about scope and class declarations and have been able to muddle along up until now, but I am doing something wrong here. Nothing I have tried has worked.

package com.jdividend.platform.allocation;

import com.ib.client.*;
import com.jdividend.platform.model.*;
import com.jdividend.platform.trader.*;
import com.jdividend.platform.util.*;

import javax.swing.*;

import java.awt.*;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Dialog to show the account size allowed info.
 */
public class AllocationDialog extends JDialog {

    /* inner class to define the "about" model */
    public static class AllocationTableModel extends TableDataModel {

        private AllocationTableModel() {
            String[] aboutSchema = {"Property", "Value"};
            setSchema(aboutSchema);
        }

    }

    public AllocationDialog(JFrame parent) {
        super(parent);
        init();
        pack();
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    public void init() {
        setModal(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setTitle("Allocation - JDividend");

        JPanel contentPanel = new JPanel();
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new BorderLayout(0, 0));
        {
            JPanel panel = new JPanel();
            contentPanel.add(panel, BorderLayout.CENTER);
            panel.setLayout(null);

            JPanel panel_1 = new JPanel();
            panel_1.setBounds(84, 146, 274, 19);
            panel.add(panel_1);

            JLabel lblNewLabel = new JLabel("Total account cash and margin that");
            panel_1.add(lblNewLabel);

            JPanel panel_2 = new JPanel();
            panel_2.setBounds(84, 163, 274, 19);
            panel.add(panel_2);

            JLabel lblJdividendIsAllowed = new JLabel("JDividend is allowed to trade with.");
            panel_2.add(lblJdividendIsAllowed);

            JFormattedTextField formattedTextField = new JFormattedTextField();
            formattedTextField.setBounds(172, 189, 100, 20);
            panel.add(formattedTextField);

            JButton btnOk = new JButton("OK");
            btnOk.setBounds(121, 221, 89, 23);
            panel.add(btnOk);
            btnOk.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        int myInt = Integer.parseInt(formattedTextField.getText());
                        System.out.println(myInt);
                        //do some stuff
                    }
                    catch (NumberFormatException ex) {
                        System.out.println("Not a number");
                        //do you want
                    }

                    JButton btnCancel = new JButton("Cancel");
                    btnCancel.setBounds(238, 220, 89, 23);
                    panel.add(btnCancel);

                    formattedTextField.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                            JButton button = new JButton("OK");
                        }
                    });

                }
            });
        }

        String serverVersion = "Disconnected from server";
        Trader trader = Dispatcher.getInstance().getTrader();
        if (trader != null) {
            int version = trader.getAssistant().getServerVersion();
            if (version != 0) {
                serverVersion = String.valueOf(version);
            }
        }

        TableDataModel aboutModel = new AllocationTableModel();

        getContentPane().setPreferredSize(new Dimension(450, 400));

        Properties properties = System.getProperties();
        Enumeration<?> propNames = properties.propertyNames();

        while (propNames.hasMoreElements()) {
            String key = (String) propNames.nextElement();
            String value = properties.getProperty(key);
            String[] row = {key, value};
            aboutModel.addRow(row);
        }
    }   
}

myInt should be declared inside the class, but outside of any method implementations:

public class AllocationDialog extends JDialog {

    public int myInt;

    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM