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

F

feofao

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

zero

(e-mail address removed) wrote in @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.
 
R

ripleyfu

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



}
 
P

Paulus de Boska

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,
 
T

Thomas Hawtin

Paulus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top