Reputation: 31
I have a JPanel in another class and I want it to run when I click the "enter" button. For some reason it wont do that, I tried to write a simple system.out.println in my code and that works but when i want it to run the JPanel, it wont run it, does anyone know what my problem is?
import inf45.spring2010.examples.gui3.fileChooser;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class clientGUI extends JFrame {
/**
* Variables
*/
private JLabel nameLabel;
private JLabel IPLabel;
private JLabel portLabel;
private JTextField nameText;
private JTextField IPText;
private JTextField portText;
private JButton enterButton;
private JButton cancelButton;
/**
* Constructor for the Frame
*/
public clientGUI () {
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
buildUI();
}
private void buildUI(){
// Setup the GridBagLayout
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
/**
* Set Up the JLabels
*/
nameLabel = new JLabel ("Name:");
getContentPane().add(nameLabel);
layout.setConstraints(
nameLabel,
new GridBagConstraints(
0, 1, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
IPLabel = new JLabel ("IP Address:");
getContentPane().add(IPLabel);
layout.setConstraints(
IPLabel,
new GridBagConstraints(
0, 2, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
portLabel = new JLabel("Port:");
getContentPane().add(portLabel);
layout.setConstraints(
portLabel,
new GridBagConstraints(
0, 3, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
/**
* Setup JTextFields
*/
nameText = new JTextField (15);
getContentPane().add(nameText);
layout.setConstraints(
nameText,
new GridBagConstraints(
2, 1, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
IPText = new JTextField (15);
getContentPane().add(IPText);
layout.setConstraints(
IPText,
new GridBagConstraints(
2, 2, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
portText = new JTextField(15);
getContentPane().add(portText);
layout.setConstraints(
portText,
new GridBagConstraints(
2, 3, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
/**
* Setup JButtons
*/
enterButton = new JButton ("Enter");
getContentPane().add(enterButton);
layout.setConstraints(
enterButton,
new GridBagConstraints(
0, 4, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
//Action Listener for enter button that prompts the user to the send GUI
enterButton.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
fileChooser enterFile = new fileChooser();
enterFile.setVisible(true);
enterFile.setSize(150,150);
System.out.println("osama");
}
});
cancelButton = new JButton ("Cancel");
getContentPane().add(cancelButton);
layout.setConstraints(
cancelButton,
new GridBagConstraints(
2, 4, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 10, 5, 10), 0, 0));
//ActionListener for the cancel button that should clear all fields.
cancelButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//cancelPressed();
}
});
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class fileChooser extends JPanel implements ActionListener {
static private final String newline = "\n";
JButton openButton, saveButton;
JTextArea log;
JFileChooser fc;
public fileChooser() {
super(new BorderLayout());
//Create the log first, because the action listeners
//need to refer to it.
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
//Create a file chooser
fc = new JFileChooser();
openButton = new JButton("Select a File...");
openButton.addActionListener(this);
//Create the save button. We use the image from the JLF
//Graphics Repository (but we extracted it from the jar).
saveButton = new JButton("Send Selected File");
saveButton.addActionListener(this);
//For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(openButton);
// buttonPanel.add(saveButton);
//Add the buttons and the log to this panel.
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
//Handle SEND button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(fileChooser.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Selected: " + file.getName() + "." + newline);
System.out.println("Sent " + file.getName());
} else {
log.append("Open command cancelled by user." + newline);
System.out.println("File selection canceled.");
}
log.setCaretPosition(log.getDocument().getLength());
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = fileChooser.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
public static void createAndShowFileChooser() {
//Create and set up the window.
JFrame frame = new JFrame("FileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new fileChooser());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Displays and creates the file chooser
createAndShowFileChooser();
}
});
}
}
public class mainClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
clientGUI client = new clientGUI();
client.setTitle("Client GUI");
client.setVisible(true);
client.setSize(200,200);
}
}
Upvotes: 3
Views: 834
Reputation: 103145
You are creating a new JPanel but you are not adding it to a top level container. A JPanel has to be added to the JFrame similar to the way you are adding the labels and textfields.
Another option is to have your filechooser class extend JDialog which was designed to create these types of Dialogs. Are you sure that you do not want to use the built in javax.swing.JFileChooser class?
Upvotes: 3
Reputation: 137442
A JPanel
cannot be displayed on its own, it has to be inside a JFrame
. So make fileChooser
extend JFrame
(and add what's needed), or add the new panel (enterFile) to your current frame (whatever fit your needs).
Upvotes: 3