Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

why my applet can't display components like JButton or JTextField, but JLabel is well

Thread view: 
feofao@gmail.com - 23 Dec 2005 05:56 GMT
why my applet can't display components like JButton or JTextField, but
JLabel is well
zero - 23 Dec 2005 18:48 GMT
feofao@gmail.com wrote in news:1135317413.624665.16940
@g44g2000cwa.googlegroups.com:

> why my applet can't display components like JButton or JTextField, but
> JLabel is well

Because your computer has a pixie inside the cd-rom drive which eats
JButtons.  The JTextFields are just afraid of being eaten too, even though
the pixie doesn't like them.

Without code, we can't help you.

Signature

Beware the False Authority Syndrome

ripleyfu@gmail.com - 24 Dec 2005 02:45 GMT
sorry, my code is:

// FileChooserPanel.java
package test.applets;

import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;

//import javax.swing.JLabel;

public class FileChooserPanel extends JPanel
                             implements ActionListener {
   JTextField uploadpath;
   JButton opendirButton;
   JFileChooser fc;

   public FileChooserPanel() {
       fc = new JFileChooser();
       uploadpath = new JTextField(20);
       JPanel uploadpathPanel = new JPanel();
       uploadpathPanel.add(uploadpath);

       opendirButton = new JButton("Open File...");
       opendirButton.addActionListener(this);
       JPanel buttonPanel = new JPanel();
       buttonPanel.add(opendirButton);

       add(uploadpathPanel);
       add(buttonPanel);

       // this will be ok
       //JLabel lab = new JLabel("Hello, World!");
       //add(lab);

   }

   public void actionPerformed(ActionEvent e) {
       if (e.getSource() == opendirButton) {
           fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
           int returnVal = fc.showOpenDialog(FileChooserPanel.this);

           if (returnVal == JFileChooser.APPROVE_OPTION) {
               File file = fc.getSelectedFile();
               uploadpath.setText(file.toString());
           }
           else {
               uploadpath.setText("");
           }
       }
   }

}

// FileChooserApplet.java
package test.applets;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.UIManager;

import test.applets.FileChooserPanel;

public class FileChooserApplet extends JApplet {
       private void createGUI() {
               FileChooserPanel fcp = new FileChooserPanel();
               fcp.setOpaque(true);
               fcp.setBackground(Color.white);
               setContentPane(fcp);
       }

   public void init() {
       try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
           javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                       public void run() {
                               createGUI();
                       }
               });
       }
       catch (Exception e) {
               System.err.println("createGUI didn't successfully
complete");
       }
   }

}
Paulus de Boska - 24 Dec 2005 15:01 GMT
Well, it ran fine, when I tried it under 1.4.2_10, but some comments:
you don't need javax.swing.SwingUtilities.invokeAndWait in init.
There's no painting until well after init has finished, so doing GUI
stuff in init is safe. You only need that in some special cases, as I
explain here (if your browser supports the use of applets) :
http://javalessons.com/cgi-bin/ui/java-swing.cgi?1cd=ths&sid=ao789
Another thing, why not add the components to FileChooserPanel straight
away, instead of putting them in other panels first ?
Hope this helps,

---
Paul Hamaker, SEMM
http://javalessons.com
Thomas Hawtin - 24 Dec 2005 15:35 GMT
> Well, it ran fine, when I tried it under 1.4.2_10, but some comments:
> you don't need javax.swing.SwingUtilities.invokeAndWait in init.
> There's no painting until well after init has finished, so doing GUI
> stuff in init is safe. You only need that in some special cases, as I

Sun's information on this is contradictory. In fact, you do need the
invokeAndWait to be safe, as Swing (particularly text) is booby-trapped
with invokeLaters.

> explain here (if your browser supports the use of applets) :
> http://javalessons.com/cgi-bin/ui/java-swing.cgi?1cd=ths&sid=ao789

Or use appletviewer from the JDK.

Tom  Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Andrew Thompson - 26 Dec 2005 02:24 GMT
> why my applet can't display components like JButton or JTextField, but
> JLabel is well

What Java is your browser running?
<http://www.physci.org/pc/property.jsp?prop=java.version+java.vendor>

What is the exception showing in the Java Console?

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.