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 / GUI / 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:59 GMT
why my applet can't display components like JButton or JTextField, but
JLabel is well
IchBin - 23 Dec 2005 07:08 GMT
> why my applet can't display components like JButton or JTextField, but
> JLabel is well

You need to post your code to figure out your problem..
Signature


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

ripleyfu@gmail.com - 23 Dec 2005 07:30 GMT
sorry, my code is:

<pre>
// 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);
       uploadpath.addActionListener(this);
       JPanel uploadpathPanel = new JPanel();
       uploadpathPanel.add(uploadpath);

       opendirButton = new JButton("$(AQ!TqD?B<(B");
       opendirButton.addActionListener(this);
       JPanel buttonPanel = new JPanel();
       buttonPanel.add(opendirButton);

       add(uploadpathPanel);
       add(buttonPanel);

       // this well 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");
       }
   }
}

</pre>
Roedy Green - 23 Dec 2005 12:31 GMT
  >    opendirButton =3D new JButton("$(AQ!TqD?B<(B");

this looks like gibberish. I suggest you encode it with \uffff chars,
or use a ResourceBundle.  If you want to carry on that way, you must
tell Javac which encoding you used to create the program.  see
http://mindprod.com/jgloss/javacexe.html
http://mindprod.com/jgloss/resourcebundle.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 23 Dec 2005 18:12 GMT
[code snipped]

I reformatted your code like this, removed some import statements, place the
two classes in the same file, and other minor fixes, and it seems to work
now.

<code>
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class FileChooserApplet extends JApplet {

 void createGUI() {
   FileChooserPanel fcp = new FileChooserPanel();
   fcp.setOpaque(true);
   fcp.setBackground(Color.white);
   setContentPane(fcp);
 }

 @Override
 public void init() {
   try {
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
       public void run() {
         createGUI();
       }
     });
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
}

class FileChooserPanel extends JPanel implements ActionListener {
 JTextField uploadpath;

 JButton opendirButton;

 JFileChooser fc;

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

   this.opendirButton = new JButton("$(AQ!TqD?B<(B");
   this.opendirButton.addActionListener(this);
   JPanel buttonPanel = new JPanel();
   buttonPanel.add(this.opendirButton);

   add(uploadpathPanel);
   add(buttonPanel);

 }

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

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

}
</code>

   - Oliver
ripleyfu@gmail.com - 24 Dec 2005 02:32 GMT
thanks for the help,
i hava compiled your code, and the problem seems not yet solved,
it's just a gray background whit in the browser.
ripleyfu@gmail.com - 24 Dec 2005 02:37 GMT
thanks for the help,
i hava compiled the code, and the problem seems not yet solved.
it's also a gray background within the browser.
Roedy Green - 24 Dec 2005 08:20 GMT
>i hava compiled the code, and the problem seems not yet solved.
>it's also a gray background within the browser.

If you want help with run time problems, you need to post your
complete code so others can run it.  Try pruning it down as much as
possible so something that still demonstrates the problem.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

ripleyfu@gmail.com - 24 Dec 2005 09:23 GMT
something i quite understand is just add a JLabel to content pane is
all right,
but other components, this is also my question, from the code above.

i am a newbie to java, and i can't find something hava problem in my
code.
the code Oliver reformatted is still hava same problem in my computer
but it can work he said, i don't know why.

anyway, thanks for the help.
Andrew Thompson - 26 Dec 2005 02:48 GMT
> why my applet can't display components like JButton or JTextField, but
> JLabel is well

Please refrain from multi-posting.
<http://www.physci.org/codes/javafaq.jsp#xpost>

See my response on the other newsgroup.

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



©2008 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.