问题
I am trying to use a popup sort of panel to enter information in order to add to my arraylist. I found this code
import javax.swing.*;
public class JOptionPaneMultiInput {
public static void main(String[] args) {
JTextField xField = new JTextField(15);
JTextField yField = new JTextField(15);
JTextField zField = new JTextField(15);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Item Name:"));
myPanel.add(xField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Number in inventory:"));
myPanel.add(yField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Unit Price:"));
myPanel.add(zField);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter data into all boxes", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
}
}
}
which works on it's own and displays what I need displayed, however how do I get it to work with my actionListner? I want it to pop up when the user clicks on the add button.
I also am not sure how to input this information into my stored arraylist that was previously built, but I can make that a separate question if needed.
回答1:
Try,
JButton but = new JButton("Popup");
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JPanel myPanel = new PopupPanel();// Create a separate class extends JPanel
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter data into both boxes", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
}
}
});
回答2:
I was able to get the window to popup with an action listener by creating a separate class and calling that method within the action listener. This class looks like this:
public class JOptionPaneMultiInput {
public static void main(String[] args) {
buildWindow();
}
public static void buildWindow() {
JTextField xField = new JTextField(15);
JTextField yField = new JTextField(15);
JTextField zField = new JTextField(15);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Item Name:"));
myPanel.add(xField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Number in inventory:"));
myPanel.add(yField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Unit Price:"));
myPanel.add(zField);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter data into all boxes", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String newItemName = String.valueOf(xField);
String newInventoryNumber = String.valueOf(yField);
int newNumber = Integer.parseInt(newInventoryNumber);
String newUnitPrice = String.valueOf(zField);
double newPrice = Double.parseDouble(newUnitPrice);
}
}
}
My action listener simply looks like this:
addButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e
) {
JOptionPaneMultiInput.buildWindow();
}
}
);
来源:https://stackoverflow.com/questions/20437699/using-a-popup-window-to-add-items-to-arraylist