Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pause a method until a button is pressed?
    primarykey
    data
    text
    <p>I am designing a software with multiple components - each with its own actionlistener. For an example, I have a JPanel with a cardlayout that holds 10 cards - each its own JPanel and purpose.</p> <p>On one side, there are multiple buttons, I.E. Login, Logout, Settings, etc.</p> <p>When I click Login, it will switch to the card using the Login() method to the Login JPanel object where I want it to wait for a button to click I.E. Login, New User, or Cancel before continuing the Login() method and setting the current user.</p> <p>Is there a method to pause the program until one of the buttons are clicked to retrieve the data from it? (Kind of like how JOptionPane.showInputMessage(null,"INPUT STRING") waits for you)</p> <p>My Code is below: FRAME:</p> <pre><code>/** * Frame design */ public class Frame extends JFrame{ JPanel LeftSide, UpperRightSide; EmployeeAdder employAdd; ArrayList&lt;ServiceView&gt; serviceViewers; ChartViewer viewChart; PayByView viewPayBy; SettingsViewer viewSettings; LoginViewer viewLogin; CategoryView viewCategory; ServiceAdder serviceAdd; Directory directory; Employee currentEmployee; ChargeViewer viewCharge; JButton Login, Logout, Settings; CardLayout LeftCard,RightCard; String currentCard,currentRightCard; ButtonListen listen; public static String CARDCAT = "Category View"; public static String CARDPAY = "Pay By"; public static String CARDCHART = "Chart View"; public static String CARDLOGIN = "Log-in View"; public static String CARDSERVICEADD = "Service Adder"; Frame(){ listen = new ButtonListen(); //-------Current Card-------------------- currentCard = CARDCAT; currentRightCard = "CHARGE"; //-----First Find Directory Folder------- startDirectory(); //-----User Interface-------------------- //-------Left Side----------------------- LeftSide = new JPanel(); LeftCard = new CardLayout(); LeftSide.setLayout(LeftCard); viewPayBy = new PayByView(); viewLogin = new LoginViewer(); viewChart = new ChartViewer(); viewCategory = new CategoryView(); employAdd = new EmployeeAdder(); serviceAdd = new ServiceAdder(); LeftSide.add(viewCategory,"CAT"); LeftSide.add(viewChart, "CHA"); LeftSide.add(viewLogin,"LOG"); LeftSide.add(viewPayBy,"PAY"); LeftSide.add(employAdd,"EMA"); LeftSide.add(serviceAdd,"SEA"); LeftCard.show(LeftSide, "CAT"); viewCategory.setEnabled(false); LeftSide.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK),currentCard)); serviceViewers = new ArrayList&lt;ServiceView&gt;(); //--------Right Side--------------------- JPanel RightSide = new JPanel(); RightSide.setLayout(new BorderLayout()); UpperRightSide = new JPanel(); RightCard = new CardLayout(); UpperRightSide.setLayout(RightCard); viewSettings = new SettingsViewer(); viewCharge = new ChargeViewer(); viewCharge.setEnabled(false); UpperRightSide.add(viewCharge,"CHARGE"); UpperRightSide.add(viewSettings,"SETTINGS"); UpperRightSide.setPreferredSize(new Dimension(350,500)); RightSide.add(UpperRightSide,BorderLayout.NORTH); //--------Buttons at the bottom Panel--- JPanel Buttons = new JPanel(); Buttons.setLayout(new GridLayout(3,1)); Login = new JButton("LOG-IN"); Login.addActionListener(listen); Logout = new JButton("LOG OUT"); Logout.addActionListener(listen); Settings = new JButton("Settings"); Settings.addActionListener(listen); Buttons.add(Login); Buttons.add(Logout); Buttons.add(Settings); Buttons.setPreferredSize(new Dimension(350,150)); RightSide.add(Buttons,BorderLayout.SOUTH); RightSide.setSize(new Dimension(400,200)); //------Other Stuff-------------------------- //-----add Panels---------------------------- setLayout(new BorderLayout()); add(LeftSide,BorderLayout.WEST); add(RightSide,BorderLayout.EAST); } private void Login(){ LeftCard.show(LeftSide, "LOG"); //----I WANT IT TO WAIT HERE FOR AN ACTION------- int clicked = viewLogin.getClicked(); if (clicked==LoginViewer.NEWUSER){ NewUser(); }else if (clicked==LoginViewer.LOGIN){ if (viewLogin.checkPassword()){ currentEmployee = directory.getEmployee(viewLogin.getSelectedName()); viewCategory.setEnabled(true); viewCharge.setEnabled(true); viewCharge.refreshName(currentEmployee.getName()); LeftCard.show(LeftSide, "CAT"); } }else if (clicked==LoginViewer.CANCEL){ LeftCard.show(LeftSide, "CAT"); } } public class ButtonListen implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { if (!viewLogin.isWaiting()){ if (e.getSource()==Login){ if (currentCard.equals(CARDLOGIN)){ LeftCard.show(LeftSide,"CAT"); currentCard = CARDCAT; }else{ Login(); currentCard = CARDLOGIN; } }else{ //Don't change the screen } } } } } </code></pre> <p>My Code for LoginViewer:</p> <pre><code>public class LoginViewer extends JPanel{ JComboBox User; JPasswordField passField; JButton NewUser, Login, Cancel; Hashtable&lt;String,String&gt; namespass; //names and password private int clicked = -1; ButtonListen listen; public static int NEWUSER = 1; public static int LOGIN = 0; public static int CANCEL = 2; boolean waiting; LoginViewer(){ waiting = false; //--------------------------------------- setPreferredSize(new Dimension(100,100)); listen = new ButtonListen(); namespass = new Hashtable&lt;String,String&gt;(); //----------Panel Design------------------- JPanel Center = new JPanel(); Center.setLayout(new GridLayout(3,3)); User = new JComboBox(); passField = new JPasswordField(); NewUser = new JButton("New User"); NewUser.addActionListener(listen); Login = new JButton("Login"); Login.addActionListener(listen); Cancel = new JButton("Cancel"); Cancel.addActionListener(listen); Center.add(new JLabel("Choose User")); Center.add(User); Center.add(new JLabel("")); Center.add(new JLabel("Type Password")); Center.add(passField); Center.add(new JLabel("")); Center.add(Login); Center.add(NewUser); Center.add(Cancel); Center.setPreferredSize(new Dimension(300,300)); Center.setMaximumSize(new Dimension(100,100)); Center.setAlignmentX(CENTER_ALIGNMENT); setAlignmentX(CENTER_ALIGNMENT); setLayout(new BoxLayout(this,BoxLayout.X_AXIS)); add(Box.createHorizontalGlue()); add(Center); add(Box.createHorizontalGlue()); } public void uploadUserNames(Hashtable&lt;String,String&gt; names){ namespass.clear(); namespass.putAll(names); User.removeAllItems(); Enumeration&lt;String&gt; name = names.keys(); while (name.hasMoreElements()){ User.addItem(name.nextElement()); } } public boolean checkPassword(){ boolean value = false; String key = User.getSelectedItem().toString(); if (passField.getPassword().length==4){ if (namespass.get(key).equals(String.valueOf(passField.getPassword()))){ value = true; } } return value; } public String getSelectedName(){ return User.getSelectedItem().toString(); } public boolean isWaiting(){ return waiting; } public int getClicked(){ waiting = true; return clicked; } public class ButtonListen implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { waiting = false; if (e.getSource()==NewUser){ clicked = 1; }else if (e.getSource()==Login){ clicked = 0; }else if (e.getSource()==Cancel){ clicked = 2; } } } } </code></pre> <p>Or is it easier to just use an actionlistener to listen to ALL of the objects' buttons?</p> <p>There are a LOT of buttons...</p> <p>NOTE: Some of the methods are incomplete or test methods until I know how to make it work...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload