Solved

i need applet help bad (and quickly)

Posted on 2001-07-29
3
192 Views
Last Modified: 2010-03-31
Hi everyone,

I am working hard to finish up a relatively simple applet that displays some information in a textarea when you click on certain menuItems or JButtons.

If you run the code, you first start out by making a selection from the 'Contents' menu.  After you select, say man(1), a heading comes up on the very top saying its title "man(1)", and then the "See Also" panel pops up with relevant links that are JButtons.  Then in the text area below, the text for the applicable selection is displayed. So for man(1), the text area below says:

NAME

     man - find and display reference manual pages
     
     blah blah blah
     
The problem is that the menuItems (named content0 thru content4) and the JButtons(b0 thru b4) both call the exact same methods in their listeners, and hence should have the same responses when clicked, right?  But they don't... if you click on any of the menuItems inside the Contents menu, that selection will always show up correctly with the correct 'headline' in the JPanel namePanel, like how i described above with the man(1) selection.  however, if you hit the JButton links that are associated with that selection, the textArea and link buttons will come up correctly, but sometimes the namePanel (the headline on top) is not correct.  For example, if I was looking at the apropos(1) selection and I clicked the whatis Button, then often the whatis textarea and buttons would be correctly displayed, but the headline would still say "apropos(1)"... and it might be stuck there for a little while until some other headline gets stuck up in the namePanel.

Here is the code, and I would be eternally grateful if someone could help me fix it:
http://www.cs.uoregon.edu/~jenni/ee/
(you only really need the .java file in this folder to get it to run)

Thanks so much!
jennifer.

Please post all suggestions as COMMENTS.  thanks!
0
Comment
Question by:jenniferx
  • 2
3 Comments
 
LVL 3

Expert Comment

by:black
ID: 6333030
Hi Jennifer,
I believe it's because you don't refresh your frame after you change it's title, for example if you get the problem just resize the window you'll get the correct title popping up. The values aren't changed unless the frame is repainted.
0
 
LVL 3

Accepted Solution

by:
black earned 150 total points
ID: 6333043
I was able to successfully replicate the problem and fix it here's the modified code:

// start code
/* ManApplet.java

 */

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.border.Border;
import javax.swing.BorderFactory;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;

import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;

  //------------------------------------------------------------------
  //  ManApplet
  //------------------------------------------------------------------

public class ManApplet {

  //-- class variables -----------------------------------------------

  // text area for displaying the manual information
  public static JTextArea textArea  = new JTextArea();

  // one big panel that holds everything
  public static JPanel bigPanel = new JPanel();

  // the applet frame
  public static JFrame frame1 = new JFrame(" Unix Manual");

  // puts name of Man Page on top as headline
  public static JPanel namePanel = new JPanel();

  // See Also Panel that says "See Also:"
  public static JPanel seeAlsoPanel = new JPanel();

  // See Also button holder JPanel
  public static JPanel seeAlsoButtons = new JPanel();

  // labels
  public static JLabel seeAlsoLabel = new JLabel("  See Also: ");
  public static JLabel apropos     = new JLabel(" apropos(1)");
  public static JLabel catman       = new JLabel(" catman(1m)");
  public static JLabel man1         = new JLabel(" man(1)");
  public static JLabel man5         = new JLabel(" man(5)");
  public static JLabel whatis       = new JLabel(" whatis(1)");
  public static JLabel instr        = new JLabel(" Instructions");

  // SEE ALSO buttons
  public static JButton b0 = new JButton("apropos(1)");
  public static JButton b1 = new JButton("catman(1m)");
  public static JButton b2 = new JButton("man(1)");
  public static JButton b3 = new JButton("man(5)");
  public static JButton b4 = new JButton("whatis(1)");

  // blank space panels (necessary?)
  public static JPanel space1 = new JPanel();
  public static JPanel space2 = new JPanel();
  public static JPanel space3 = new JPanel();
  public static JPanel space4 = new JPanel();
  public static JPanel space5 = new JPanel();


  //------------------------------------------------------------------
  //  main
  //------------------------------------------------------------------

  public static void main(String[] args) {

    // set the frame's look
    try {
      UIManager.setLookAndFeel(
                  UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch (Exception e) {
      System.out.println("error, setLookAndFeel failed");
    }

    // set the frame size
    frame1.setSize( 680, 650 );

  //-- menus ---------------------------------------------------------

    // create three menus
    JMenu fileMenu = new JMenu("File");
    JMenu contentsMenu = new JMenu("Contents");
    JMenu helpMenu = new JMenu("Help");

    // create items for the three menus
    JMenuItem file0 = new JMenuItem("Exit");
    JMenuItem content0 = new JMenuItem("apropos(1)");
    JMenuItem content1 = new JMenuItem("catman(1m)");
    JMenuItem content2 = new JMenuItem("man(1)");
    JMenuItem content3 = new JMenuItem("man(5)");
    JMenuItem content4 = new JMenuItem("whatis(1)");
    JMenuItem help0 = new JMenuItem("Instructions");

    // add listeners to the items
    file0.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    System.exit( 0 );
    }
    } );

    content0.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    content0Action();
    }
    } );

    content1.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    content1Action();
    }
    } );

    content2.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    content2Action();
    }
    } );

    content3.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    content3Action();
    }
    } );

    content4.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    content4Action();
    }
    } );

    help0.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
      helpAction();
    }
    } );

    // add the items to the three menus
    fileMenu.add(file0);
    contentsMenu.add(content0);
    contentsMenu.add(content1);
    contentsMenu.add(content2);
    contentsMenu.add(content3);
    contentsMenu.add(content4);
    helpMenu.add(help0);

    // add the menus to the menubar and add the menubar to the frame
    JMenuBar mb = new JMenuBar();
    mb.add(fileMenu);
    mb.add(contentsMenu);
    mb.add(helpMenu);
    frame1.setJMenuBar(mb);

  //-- SEE ALSO buttons event handling -------------------------------

  b0.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      content0Action();
    }
  });

  b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      content1Action();
    }
  });

  b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      content2Action();
    }
  });

  b3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      content3Action();
    }
  });

  b4.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      content4Action();
    }
  });

  //-- panel components ----------------------------------------------


    space1.setPreferredSize(new Dimension(120,22));
    space2.setPreferredSize(new Dimension(120,22));
    space3.setPreferredSize(new Dimension(120,22));
    space4.setPreferredSize(new Dimension(120,22));
    space5.setPreferredSize(new Dimension(120,22));


    b0.setPreferredSize(new Dimension(120,22));
    b1.setPreferredSize(new Dimension(120,22));
    b2.setPreferredSize(new Dimension(120,22));
    b3.setPreferredSize(new Dimension(120,22));
    b4.setPreferredSize(new Dimension(120,22));

    apropos.setFont(new Font("Arial", Font.BOLD, 15));
    catman.setFont(new Font("Arial", Font.BOLD, 15));
    man1.setFont(new Font("Arial", Font.BOLD, 15));
    man5.setFont(new Font("Arial", Font.BOLD, 15));
    whatis.setFont(new Font("Arial", Font.BOLD, 15));
    instr.setFont(new Font("Arial", Font.BOLD, 15));


  // border for buttons
  Border buttonBorder = BorderFactory.createBevelBorder(1, Color.gray, Color.lightGray);

   b0.setBorder(buttonBorder);
   b1.setBorder(buttonBorder);
   b2.setBorder(buttonBorder);
   b3.setBorder(buttonBorder);
   b4.setBorder(buttonBorder);



    namePanel.setLayout(new GridLayout(1,1));
    namePanel.add(space1);

    seeAlsoPanel.setLayout(new GridLayout(1,1));

    seeAlsoPanel.add(space2);

    //set up panel to be 1 down, 4 across (holds See Also buttons)
    seeAlsoButtons.setLayout(new GridLayout(1,6));
    seeAlsoButtons.add(space3);


    bigPanel.setLayout(new BoxLayout(bigPanel, BoxLayout.Y_AXIS));

    bigPanel.add(namePanel);
    bigPanel.add(seeAlsoPanel);
    bigPanel.add(seeAlsoButtons);

    textArea.setEditable(false);


    // the text area
    textArea.setFont(new Font("Courier",Font.PLAIN,14));
    // set up and install the text area
    // margin's go; top, left, bottom, right
    textArea.setMargin(new Insets(15,15,0,0));
    textArea.setFont(new Font("Courier",Font.PLAIN,14));
    textArea.setEditable(false);

    JScrollPane scrollPane = new JScrollPane(textArea,
                            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setPreferredSize(new Dimension(0,515));  //0,350

    bigPanel.add(scrollPane);
    frame1.getContentPane().add(bigPanel);


    // set applet to help screen to start
    helpAction();

    // set up and install
    // add a listener to the frame and display it
    frame1.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    frame1.setVisible(true);

  }//main

  //------------------------------------------------------------------
  //  helpAction - display method
  //------------------------------------------------------------------
  public static void helpAction() {
    textArea.setText("  Use the contents menu above to choose the "+
                    "Unix man (manual) page to view.\n Link "+
                    "buttons will appear for any related man pages.");

    // update the frame
    namePanel.removeAll();
    namePanel.add(instr);
    seeAlsoPanel.removeAll();
    seeAlsoButtons.removeAll();
    seeAlsoPanel.add(space1);
    seeAlsoButtons.add(space2);
    namePanel.validate();
    frame1.validate();
    namePanel.validate();

  }//helpAction

  //------------------------------------------------------------------
  //  content0Action - display method
  //------------------------------------------------------------------
  public static void content0Action() {
    getText("apropos1.txt");     // display new man page

    // add label and buttons to pane
    namePanel.removeAll();
    System.out.println(namePanel.getComponentCount());
    namePanel.add(apropos);
    System.out.println(namePanel.getComponentCount());
    seeAlsoPanel.removeAll();
    seeAlsoPanel.add(seeAlsoLabel);
    seeAlsoButtons.removeAll();
    seeAlsoButtons.add(b1);    // catman(1m) do this for each!!!!!
    seeAlsoButtons.add(b2);
    seeAlsoButtons.add(b4);
    seeAlsoButtons.add(space1);
    seeAlsoButtons.add(space2);
    seeAlsoButtons.add(space3);

    namePanel.validate();
    // add the pane JPanel to the frame and update
    frame1.validate();
    namePanel.validate();
    frame1.repaint();

  }//content0Action

  //------------------------------------------------------------------
  //  content1Action - display method
  //------------------------------------------------------------------
  public static void content1Action() {
    getText( "catman1m.txt" );

    // add label and buttons to pane
    namePanel.removeAll();
    System.out.println(namePanel.getComponentCount());
    namePanel.add(catman);
    System.out.println(namePanel.getComponentCount());

    seeAlsoPanel.removeAll();
    seeAlsoPanel.add(seeAlsoLabel);

    seeAlsoButtons.removeAll();
    seeAlsoButtons.add(b0);
    seeAlsoButtons.add(b2);
    seeAlsoButtons.add(b3);
    seeAlsoButtons.add(b4);
    seeAlsoButtons.add(space1);
    seeAlsoButtons.add(space2);


    // add the pane JPanel to the frame and update
    namePanel.validate();
    frame1.validate();
    namePanel.validate();
    frame1.repaint();

  }//content1Action

  //------------------------------------------------------------------
  //  content2Action - display method
  //------------------------------------------------------------------
  public static void content2Action() {
    getText( "man1.txt" );

    // add label and buttons to pane
    namePanel.removeAll();
    System.out.println(namePanel.getComponentCount());
    namePanel.add(man1);
    System.out.println(namePanel.getComponentCount());

    seeAlsoPanel.removeAll();

    seeAlsoPanel.add(seeAlsoLabel);
    seeAlsoButtons.removeAll();
    seeAlsoButtons.add(b0);
    seeAlsoButtons.add(b1);
    seeAlsoButtons.add(b3);
    seeAlsoButtons.add(b4);
    seeAlsoButtons.add(space1);
    seeAlsoButtons.add(space2);



    // add the pane JPanel to the frame and update
    namePanel.validate();
    frame1.validate();
    namePanel.validate();
    frame1.repaint();

  }//content2Action

  //------------------------------------------------------------------
  //  content3Action - display method
  //------------------------------------------------------------------
  public static void content3Action() {
    getText( "man5.txt" );

    // add label and buttons to pane
    namePanel.removeAll();
    System.out.println(namePanel.getComponentCount());
    namePanel.add(man5);
    System.out.println(namePanel.getComponentCount());

    seeAlsoPanel.removeAll();
    seeAlsoPanel.add(seeAlsoLabel);
    seeAlsoButtons.removeAll();
    seeAlsoButtons.add(b2);
    seeAlsoButtons.add(b4);
    seeAlsoButtons.add(space1);
    seeAlsoButtons.add(space2);
    seeAlsoButtons.add(space3);
    seeAlsoButtons.add(space4);


    // add the pane JPanel to the frame and update
    namePanel.validate();
    frame1.validate();
    namePanel.validate();
    frame1.repaint();

  }//content3Action

  //------------------------------------------------------------------
  //  content4Action - display method
  //------------------------------------------------------------------
  public static void content4Action() {
    getText( "whatis1.txt" );

    // add label and buttons to pane
    namePanel.removeAll();
    System.out.println(namePanel.getComponentCount());
    namePanel.add(whatis);
    System.out.println(namePanel.getComponentCount());
    seeAlsoPanel.removeAll();
    seeAlsoPanel.add(seeAlsoLabel);
    seeAlsoButtons.removeAll();
    seeAlsoButtons.add(b0);
    seeAlsoButtons.add(b1);
    seeAlsoButtons.add(b2);
    seeAlsoButtons.add(space1);
    seeAlsoButtons.add(space2);
    seeAlsoButtons.add(space3);


    // add the pane JPanel to the frame and update
    namePanel.validate();
    frame1.validate();
    namePanel.validate();
    frame1.repaint();

  }//content4Action

  //-- helper methods ------------------------------------------------

  //------------------------------------------------------------------
  //  getText
  //------------------------------------------------------------------

  public static void getText( String file ) {

    textArea.setText("");  // clear text area

    try {
      BufferedReader in = new BufferedReader(new FileReader(file));

      // add new lines to text area
      while (in.ready()) {
        textArea.append(in.readLine() + "\n");
      }
      in.close();
    }
    catch (Exception exception) {
      textArea.setText("error reading from man file: " + file);
    }

    textArea.setCaretPosition(0); // scroll to top of text area

  }//getText



}//class ManApplet
//end code
I have just added the following line to each of your action methods:
    frame1.repaint();
0
 

Author Comment

by:jenniferx
ID: 6333052
Thank you so much, black!  You are my new hero and that's no joke.  I had given up on repaint() earlier because it wasn't working for something else... but i'm happy to see it again.

thanks again,
Jennifer.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now