user1044585
user1044585

Reputation: 521

Pass events in swing?

im using java swing for my coursework to make a quiz. Below is my main frame class which makes new panels which i have as separate classes. But for example if i have a login panel, and the user hits the login button how can i signal my main tabbedquiz class that someone has logged in?

public class TabbedQuiz {

private JFrame jF;
private JTabbedPane tP;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TabbedQuiz w = new TabbedQuiz();
                w.jF.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    });
}


public TabbedQuiz() {
    initialize();
}


private void initialize() {
    tp= new JFrame();
    tp.setTitle("welcome to ...");
    final JPanel mainPanel= new JPanel();
    final JPanel anotherPanel= new JPanel();
    final JPanel examplePanel = new JPanel();
    final JPanel quizPanel = new JPanel();
    final JPanel examPanel = new JPanel();
    final JPanel viewPerfPanel = new JPanel();
    final JPanel settingsPanel = new JPanel();

//set up the panels

    tF.setBounds(100, 100, 764, 470);
    tF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    tF.getContentPane().setLayout(null);

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(10, 11, 728, 410);
    tF.getContentPane().add(tabbedPane);

    tabbedPane.addTab("Welcome", null, mainPanel, null);

// set up other tabs

Upvotes: 1

Views: 264

Answers (6)

Jonathan
Jonathan

Reputation: 7391

If your goal is to prevent access to the quiz until someone has logged in, hide other panels or frames until a user has logged in.

Here's an example using CardLayout:

final JPanel mainPanel = new JPanel(new CardLayout());
final JTextField textField = new JTextField(10);

Action action = new AbstractAction("Login") {
    public void actionPerformed(ActionEvent e) {
        /* Check user credentials here. */
        boolean b = textField.getText().equals("true");

        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, b ? e.getActionCommand() : "login");
    }
};

JButton loginButton = new JButton(action);
loginButton.setActionCommand("quiz");

JPanel loginPanel = new JPanel();
loginPanel.add(textField);
loginPanel.add(loginButton);

JPanel quizPanel = new JPanel();
quizPanel.add(new JLabel("Quiz"));

mainPanel.add(loginPanel, "login");
mainPanel.add(quizPanel, "quiz");

JFrame f = new JFrame();
f.setContentPane(mainPanel);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});

Upvotes: 1

Johnydep
Johnydep

Reputation: 6277

Not an ideal way, but simply create a static method in other class which you call once the login is successful. In the method, simple set a boolean volatile variable from false to true, and in the same class you should have a thread with while(this flag), which becomes true and you can do whatever you want like showing new GUI etc.

Upvotes: 0

romje
romje

Reputation: 660

using listeners is the standard Swing way to handle such context, previous answers should help you... You could try to find a more complex but more smart way to handle asynchronous notification while using a JMS approach with one of the standard brokers (activemq or any other)... Such approach may bring you a nice code isolation but improves complexity....

HTH jerome

Upvotes: 0

Shawabawa
Shawabawa

Reputation: 2587

I believe you need an action listener.

See the doc here: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

For example:

login_button = new Button('Login')
login_button.addActionListener(loginNotifyListener)

loginNotifyListener would have a reference to the Quiz and could call some function in its actionPerformed method.

edit: You would probably want to create the listener in TabbedQuiz's initialise method, and pass it to the Login panel. eg:

private void initialize() {
    ...
    loginActionListener = new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           TabbedQuiz.this.notifyLogin()
       }
    }
    final JPanel loginPanel = new JPanel();
    ...
    login_button = new Button('Login')
    login_button.addActionListener(loginNotifyListener)
    loginPanel.add(login_button)
    ...
}

Hope that makes sense.

Upvotes: 0

mre
mre

Reputation: 44240

You should read up on the Swing MVC framework.

Hint: when a component generates an event, all registered listeners will be notified.

Upvotes: 2

Sean
Sean

Reputation: 7737

Here are a few brainstorming ideas

Create a class called QuizPanel which extends JPanel. Inside that class have a method which changes the contents of your panel. Your login panel (via listener) could make calls to this class.

Here would be an example

public class QuizPanel extends JPanel{
   private boolean loggedIn = false;

   public QuizPanel() {}

   public void setLoggedIn(boolean x){ 
      loggedIn=x;
      if(loggedIn){
         //Show the questions
      }else{
         //Don't show questions
      }
   }
   public boolean getLoggedIn() {return loggedIn; }
}

Another option would be to use a CardLayout so that you hide the questions, and only show it when the user has logged in.

Upvotes: 0

Related Questions