Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

help with GUI

Hi,

I'm new to java and i have to create a GUI.
So far i have created a GUI with 2 buttons on it. one which opens a file (which works fine) and the other called parameters (which doesn't)
When the user presses the parameter button i want a screen to pop up which will allow the user to enter some parameters and when pressed ok these parameters are saved for use later on.
i really don't know where to start with this.

my code so far is:

public class RunPanel extends JPanel implements ActionListener
{

JButton openButton, setParametersButton;
static private final String newline = "\n";
JTextArea log;
JFileChooser fc;

public RunPanel()
{
super(new BorderLayout());
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);

fc = new JFileChooser();

openButton = new JButton("Open a File...",
createImageIcon("images/Open16.gif"));
openButton.addActionListener(this);

setParametersButton = new JButton("Set User Parameters");

setParametersButton.addActionListener(this);

JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(openButton);
buttonPanel.add(setParametersButton);

add(buttonPanel, BorderLayout.PAGE_START);

add(logScrollPane, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(RunPanel.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
else
{
if (e.getSource() == setParametersButton)
{
//this is where i want a new panel to pop up with options for user to input parameters
}
}

}

private static void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("AIRS");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane = new RunPanel();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

Can anybody suggest a way for me to do this?

Thankyou
[3259 byte] By [Rebekah] at [2007-11-11 8:04:22]
# 1 Re: help with GUI
The simplest way to get a single parameter from a user is to use a JOptionPane:

// variable declaration
String parameter;
//....
//....
if (e.getSource() == setParametersButton)
{
parameter = JOptionPane.showInputDialog(this, "A message here", "Title here", JOptionPane.QUESTION_MESSAGE);
}

Once the user enters the parameter and click ok, it will be stored into the String called parameter. You are then free to do what you want with it.

Kind regards,
Noel
noelob at 2007-11-11 22:36:38 >
# 2 Re: help with GUI
cheers for that,

but its a few parameters that's required form the user about 10.

Is there an easy way of doing this?

Cheers
Rebekah at 2007-11-11 22:37:37 >
# 3 Re: help with GUI
The best way is to use a JDialog (its similar to a JFrame) and add textfields for all the parameters you need.

Kind regards,
Noel
noelob at 2007-11-11 22:38:30 >