Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

swing problem

Status
Not open for further replies.

vasah20

Programmer
Feb 16, 2001
559
US
I'm trying to get this code to run, on linux. I'm using the 1.4 sdk, on Mandrake Linux 8.

What happens is that the textarea doesn't show up at all, only the scrollbars do.

This is my first time using swing, so please be gentile :)

Code:
import javax.swing.*;            //swing
import javax.swing.text.*;       //dairy :)
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.io.IOException;      //because there will always be an error

public class URLCheck extends JApplet implements ActionListener {

         //needs to be global, since the event handler needs em
         protected JButton searchButton;
         protected JTextField txtField;
         protected JTextArea txtArea;
         protected static final String SEARCH = "search";
         private boolean inAnApplet = true;      //this is stupid, IMHO

         //will be used eventually... hold your breath
         URL codeBase;

         /*
         * avoid the system event access check error
         * I dunno... got it from the sun website.
         */
         public URLCheck() {
                         this(true);
         } //end applet constructor

         public URLCheck(boolean inAnApplet) {
                         this.inAnApplet = inAnApplet;
                         if (inAnApplet) {
                                         getRootPane().putClientProperty(
                                                                         "defeatSystemEventQueueCheck",
                                                                         Boolean.TRUE);
                         } //end if(inAnApplet
         } //end URLCheck(boolean

         public Container makeContentPane() {

                 //do everything for the lone button...
                 searchButton = new JButton("Link check now!");
                 searchButton.setVerticalTextPosition(AbstractButton.CENTER);
                 searchButton.setHorizontalTextPosition(AbstractButton.LEFT);
                 searchButton.setMnemonic(KeyEvent.VK_S);
                 searchButton.setActionCommand(SEARCH);
                 searchButton.addActionListener(this);   //useless if we can't listen for actions, right?
                 searchButton.setToolTipText("Check the site!");

                 //do some text field stuff
                 txtField = new JTextField(15);
                 txtField.setActionCommand(SEARCH);
                 txtField.addActionListener(this);

                 //add a text area
                 txtArea = new JTextArea("some random text",5,20);
                 txtArea.setFont(new Font("Sans-serif",Font.ITALIC,10));
                 txtArea.setLineWrap(true);
                 txtArea.setWrapStyleWord(true);
                 JScrollPane sp = new JScrollPane(txtArea);
                 sp.setPreferredSize(new Dimension(100,100));
                 sp.setBorder(
                                 BorderFactory.createCompoundBorder(
                                                 BorderFactory.createCompoundBorder(
                                                                 BorderFactory.createTitledBorder("Link results"),
                                                                 BorderFactory.createEmptyBorder(5,5,5,5)),
                                 sp.getBorder()));

                 //Add Components to JPanel, use default FlowLayout
                 JPanel pane = new JPanel();

                 //create the layout manager
                 GridBagLayout gb = new GridBagLayout();
                 GridBagConstraints c = new GridBagConstraints();
                 pane.setLayout(gb);     //set a layout. make life easy - or maybe harder...

                 //txtArea on pane
                 c.gridx = 0;
                 c.gridy = 0;
                 gb.setConstraints(txtField, c);
                 pane.add(txtField);

                 //button on pane
                 c.gridx = 1;
                 c.gridy = 0;
                 gb.setConstraints(searchButton, c);
                 pane.add(searchButton);

/////////TEXT AREA
                 //textarea on pane
                 c.gridx = 0;
                 c.gridy = 1;
                 c.gridwidth = 2;
                 c.weightx = 1.0;
                 c.weighty = 1.0;
                 c.anchor = GridBagConstraints.SOUTH;//bottom of space
                 c.insets = new Insets(5,0,0,0);                 //padding from top (order - t,l,b,r)
                 //gb.setConstraints(txtArea, c);
                 //pane.add(txtArea);

                 gb.setConstraints(sp, c);      //EY
                 pane.add(sp);                  //EY
///////////END TEXT AREA
                 pane.setPreferredSize(new Dimension(300,300));
                 pane.setBackground(new Color(0,0,128));
                 pane.setBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.black)); //t,l,b,r

                 return pane;
         } //end makeContentPane

         /*
         *event listener
         */
         public void actionPerformed(ActionEvent e) {
                         if(e.getActionCommand().equals(SEARCH)) {
                                         JTextField src = (JTextField)e.getSource();
                                         txtArea.append(src.getText() + "\n");
                         } //end if(e.get..
         } //end actionPerformed

         /*
         *for the applet
         */
         public void init() {
                 setContentPane(makeContentPane());
         } //end init

         /*
         *JOY! I can run from CLI
         */
                 public static void main(String[] args) {
                         JFrame frame = new JFrame("Application version: URLCheck");     //create the window

                         frame.addWindowListener(new WindowAdapter() {   //add the window listener
                                 public void windowClosing(WindowEvent e) {
                                         System.exit(0);
                                 } //end windowClosing
                         }); //end addWindowListener

                         URLCheck applet = new URLCheck(false);  //cannot call non-static from static, so...
                         frame.setContentPane(applet.makeContentPane());
                         frame.pack();
                         frame.setVisible(true);
                 } //end main
} //end URLCheck
 leo

------------
Leo Mendoza
lmendoza@students.depaul.edu
 
Instead of adding the JTextArea on the JScrollPane constructor, try this:

JScrollPane sp = new JScrollPane();
sp.getViewport().add(txtArea);

This is how WebGain generated the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top