Link to home
Start Free TrialLog in
Avatar of jtcy
jtcy

asked on

ActionListener thing

I have: a frame called transcriptFrame which contains three buttons: getTranscriptNames, loadTranscript and cancel.

When the user clicks on loadTranscript, the JList should appear in new frame with the content showing the array of string, but my problem is, it doesnt show up:


Transcripts.addActionListener(new ActionListener()
           {
                 public void actionPerformed(ActionEvent e)
                {
                     final JFrame transcriptFrame = new JFrame("Load Transcripts");
                     final JButton getTranscriptNames = new JButton("Update Transcript List");
                     final JList transcripts = new JList();
                     final JButton loadTranscript = new JButton("Load Transcript");
                     final JButton cancel = new JButton("Cancel");
   
                     transcriptFrame.getContentPane().setLayout(new GridBagLayout());
   
                     transcriptFrame.getContentPane().add(transcripts, constraints_start);
                     transcriptFrame.getContentPane().add(getTranscriptNames, constraints_end);
                     transcriptFrame.getContentPane().add(loadTranscript, constraints_start);
                     transcriptFrame.getContentPane().add(cancel, constraints_end);
   
                     transcriptFrame.setVisible(true);
                     fixUpSize(transcriptFrame);
                     transcriptFrame.show();
   
                     getTranscriptNames.addActionListener(new ActionListener()
                     {
                           public void actionPerformed(ActionEvent e1)
                           {
                                 
                                 
                           }
                       });
   
                    loadTranscript.addActionListener(new ActionListener() {
                           public void actionPerformed(ActionEvent e1)
                           {
                                 
                                 try
                                        {
                                     transcripts.setListData(temp); // temp is an array of string
                                     transcriptFrame.setVisible(true);
                                  }
                                 catch (Exception e3)
                                  {
                                        System.out.println("Trouble: " + e3);
                                  }
                                 
                           }
                       });
   
                     cancel.addActionListener(new ActionListener() {
                           public void actionPerformed(ActionEvent e1)
                           {
                             transcriptFrame.setVisible(false);
                             transcriptFrame.dispose();
                           }
                       });
                 }
               });
   
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Instead of making those vars final, try making them as member variables of the outer class.
You shouldn't create these components in the event handler. Create them first in your ctor and then all you need to do is

if (e.getSource() == loadTranscripts) {
    transcriptFrame.setVisible(true);
}
and check that temp contains the data you expect it to.
Also check your layout that the list is actually being displayed.

you may also want to include your list in a scroll pane:

 transcriptFrame.getContentPane().add(new JScrollPane(transcripts), constraints_start);
Avatar of jtcy
jtcy

ASKER

I cannot do anything to the code beside adding the listeners for three buttons. :(
declare them as class varaibels. so that you can use them within the class as well as outside the class(instance varable)s
ASKER CERTIFIED SOLUTION
Avatar of expertmb
expertmb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial