I have an assignment for class, and I made a simple accounting program in Java with a GUI. This is my first time working with a GUI in Java, and i'm not that good a programmer.
When the program runs, the user can select five JButtons which when clicked takes them to a new JFrame with some input dialogs, which are saved as Strings and Doubles. My variables are set to something like
variable = JOptionPane.showInputDialog("TEXTGOESHERE");
My Problem is that after entering values into the dialogs, they will pop up a second time, like in a loop. The user has to enter an input into every dialog twice.
Code for one of the buttons:
pcButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFrame pcframe = new JFrame("Choosing a File Menu");
pcframe.setSize(760,500);
pcframe.add( new JPanel()
{
public void paintComponent( Graphics g )
{
super.paintComponent(g);
//ACTION BEGIN-----------------------------------------
The input dialogs which are showing up twice:
compName = JOptionPane.showInputDialog("What is the name of your business?");
firstName = JOptionPane.showInputDialog("What is your first name?");
lastName = JOptionPane.showInputDialog("What is your last name?");
day = JOptionPane.showInputDialog("What is the day?");
month = JOptionPane.showInputDialog("What is the month?");
year = JOptionPane.showInputDialog("What is the year?");
String filename = JOptionPane.showInputDialog("Would you like file 1, 2, or 3? (Type in 1, 2, or 3");
filename = (filename + ".txt");
//Storing File into array
//Calculations
g.drawString("" + compName, 330, 15);
//More drawStrings
//ACTION END-------------------------------------------
}
});
pcframe.setVisible( true );
}
});
modePanel.add(pcButton);
When the pcButton is pressed, the user is supposed to enter their name, file, etc. However, every dialog input shows up twice. I want the inputs to show up just once.
Any help would be greatly appreciated, thanks.
Sorry, but that's all wrong. You should never call JOptionPanes or do anything other than painting from within a paintComponent method. You never have full control over when or even if that method gets called, and your program's perceived responsiveness is partly dependent on how fast that method completes its jobs. So my main recommendation -- get all non-painting code out of that method and into a method that you can fully control.
Myself, rather than throw a bunch of JOptionPanes at the user, I'd create a JPanel that has all the fields that I want the user to fill out, and then show one single JOptionPane that holds this JPanel, and get all the input all at once.
Next, your secondary dialog window should be a true dialog, and for Swing that means a JDialog (or JOptionPane which is a type of modal JDialog) and not a JFrame.