Solved

ActionListener thing

Posted on 2004-09-07
7
239 Views
Last Modified: 2010-03-31
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();
                           }
                       });
                 }
               });
   
0
Comment
Question by:jtcy
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11995479
Instead of making those vars final, try making them as member variables of the outer class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11995483
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);
}
0
 
LVL 92

Expert Comment

by:objects
ID: 11995504
and check that temp contains the data you expect it to.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 11995517
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);
0
 

Author Comment

by:jtcy
ID: 11995522
I cannot do anything to the code beside adding the listeners for three buttons. :(
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11995528
declare them as class varaibels. so that you can use them within the class as well as outside the class(instance varable)s
0
 
LVL 6

Accepted Solution

by:
expertmb earned 20 total points
ID: 11995891
>>transcripts.setListData(temp); // temp is an array of string
>>transcriptFrame.setVisible(true);
you are displaying the list on same frame.

>>When the user clicks on loadTranscript, the JList should appear in new frame with the content showing the array of string,
hmmmmm you want to open in frame, then create new frame in
loadTranscript.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e1)
                        {
  JFrame fr = new JFrame();
  fr.setSize(400,400);
  fr.getContentPane().add(list);
  fr.setVisible(true);
});
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question