Reputation: 31
I have a few JFrame GUIs I created for my program; one with a menu and display of an output.txt file, and the other to enter information.
I can not interact or "click" on them until the program is totally done running.
I thought it was the JOption panes (Yes/No) taking precedence over the JFrames, however, I commented out all the JOption panes in my program to test this, and you still have to wait till the program is totally finished to access the JFrames.
public class TheList extends JPanel {
public TheList(){
}
public static void List(){
ArrayList<Food> didntBuy = new ArrayList<Food>();
ArrayList<Food> arrayList = new ArrayList<Food>();// creating an ArrayList for Food objects.
ArrayList<Food> sortLow = new ArrayList<Food>();// creating ArrayLists for high, medium, and low priorities.
ArrayList<Food> sortMed = new ArrayList<Food>();
ArrayList<Food> sortHigh = new ArrayList<Food>();
final Scanner keyboard = new Scanner(System.in);
//System.out.println("\nEnter your name");
String yourName=JOptionPane.showInputDialog("\nEnter your name for the list");
JOptionPane.showMessageDialog(null, "\nYou entered: "+ yourName);
boolean checkLetterFirstName;
checkLetterFirstName = CheckInput.CheckForNumber(yourName);// Used this class from another student.
if(checkLetterFirstName == true)
{ JOptionPane.showMessageDialog(null, "I'm sorry but no numbers or spaces can be in your first name.", yourName, JOptionPane.ERROR_MESSAGE);
//System.out.println("Enter you name");
// yourName=keyboard.nextLine();
yourName=JOptionPane.showInputDialog("\nEnter your name");
checkLetterFirstName = CheckInput.CheckForNumber(yourName);}
try{
//System.out.println("Enter funds");
//Food.currentCash=keyboard.nextDouble();
String fn=JOptionPane.showInputDialog("Enter funds for the list");
Food.currentCash= Double.parseDouble(fn);// converting String to a Double.
if(Food.getCurrentCash()<0)
throw new Exception("Exception: no negative numbers");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, (e.getMessage()), null, JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Enter postive numbers only");
String fn=JOptionPane.showInputDialog("Enter funds for the list");
Food.currentCash= Double.parseDouble(fn);
}
Food.currentCash1=Food.currentCash;
NumberFormat formatter=NumberFormat.getCurrencyInstance();
System.out.println("Current Cash");
System.out.println(formatter.format(Food.currentCash));// used a currency formatter to covert to dollars.
final Scanner keyboard6 = new Scanner(System.in);
for (int i = 0; i < 50; i++) {
//An abstract class cannot be instantiated
int answer=
JOptionPane.showConfirmDialog(null, "Do you want to add a new apple(s) object to list?", "Click Yes or No:", JOptionPane.YES_NO_OPTION);
if (answer== JOptionPane.YES_OPTION)
{
JFrame frame = new JFrame(); // creating a new JFrame object called "frame".
Container pane = null;
// SpringLayout layout = new SpringLayout();
BoxLayout layout= new BoxLayout(pane, BoxLayout.Y_AXIS);
JTextArea text0 =new JTextArea("Do you want to add an apple(s) to list?"); //creating a new TextArea object called "text".
frame.setSize(600, 600); //setting width and hight of window frame.
JButton yes = new JButton("Yes");
JButton no = new JButton("No");
JButton enter= new JButton("Enter");
JButton enter2= new JButton("Enter");
JButton enter3= new JButton("Enter");
JButton enter4= new JButton("Enter");
yes.setAlignmentX(Component.CENTER_ALIGNMENT);
no.setAlignmentX(Component.CENTER_ALIGNMENT);
final JPanel panel = new JPanel(); // creating 4 new panels
final JPanel panel2 = new JPanel();
final JPanel panel3 = new JPanel();
final JPanel panel4 = new JPanel();
JLabel label = new JLabel("What kind of apple?"); // creating 4 labels per panel.
JLabel label2 = new JLabel("Enter priority or -1 to exit program.");
JLabel label3 = new JLabel ("Enter price you want to pay.");
JLabel label4 = new JLabel ("Enter quanity.");
JTextField text = new JTextField(10);
JTextField text2 = new JTextField(10);
JTextField text3 = new JTextField(10);
JTextField text4 = new JTextField(10);
panel.add(label);// adding labels to the panel
panel2.add(label2);
panel3.add(label3);
panel4.add(label4);
panel.add(text); // adding text fields to each panel to enter information.
panel2.add(text2);
panel3.add(text3);
panel4.add(text4);
panel.add(enter);
panel2.add(enter2);
panel3.add(enter3);
panel4.add(enter4);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel2.setAlignmentX(Component.CENTER_ALIGNMENT);
panel3.setAlignmentX(Component.CENTER_ALIGNMENT);
panel4.setAlignmentX(Component.CENTER_ALIGNMENT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// ends program if closed.
frame.setAlwaysOnTop(true); // on top of other windows.
frame.setTitle("Apple");
//Image img = ImageIO.read(new File("C:\\Users\\joey zasa\\workspace\\homework4\\src\\homework4\\apple-full2.jpg"));
frame.add(text0);
frame.add(yes);
frame.add(no);
frame.add(panel);
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel3);
frame.getContentPane().add(panel4);
addComponentsToPane(frame.getContentPane());
final Fruit appleObject =new apple();
yes.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
panel.setVisible(true);
panel2.setVisible(true);
panel3.setVisible(true);
panel4.setVisible(true);
}
});
no.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
panel.setVisible(false);
panel2.setVisible(false);
panel3.setVisible(false);
panel4.setVisible(false);
}
});
enter.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
appleObject.setName(keyboard6.nextLine());
}
});
enter2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
appleObject.setPriority(keyboard.nextInt());
}
});
enter3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
appleObject.setPrice(keyboard.nextDouble());
}
});
enter3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try{
System.out.println("Enter quanity");
appleObject.setQuanity(keyboard.nextInt());
if(appleObject.getQuanity()<0)
throw new negativeException();
}
catch (negativeException x) {
System.out.println(x.getMessage());
System.out.println("Enter quanity again");
appleObject.setQuanity(keyboard.nextInt());
}
}
});
frame.pack();
frame.setVisible(true);// setting the visibility of the frame to true.
for (int z=0; z<appleObject.getQuanity(); z++){
arrayList.add (appleObject);
i++;
}}
if( appleObject.getPriority()==1||appleObject.getPriority()==2){
sortHigh.add(appleObject);}// adding only one instance of an Object to one of 3 Arraylists
if( appleObject.getPriority()==3||appleObject.getPriority()==4){
sortMed.add(appleObject);}
if( appleObject.getPriority()==5){
sortLow.add(appleObject);}
}
System.out.println("\n"+yourName+"'s list:");
int listSize=arrayList.size();
for(int position=0; position <listSize; position++){
System.out.println(arrayList.get(position));
}
int y = arrayList.size();
System.out.println("\nSize of list: "+y+"\n" );
// Sorting with low, medium,100 high, needs. Priorities 1-2=high, 3-4=medium, 5= low.
System.out.println("\nHigh priority items :\n");
int listHigh=sortHigh.size();
for(int position=0; position <listHigh; position++){
System.out.println(sortHigh.get(position));
}
System.out.println("\nMedium priority items :\n");
int listMed=sortMed.size();
for(int position=0; position <listMed; position++){
System.out.println(sortMed.get(position));
}
System.out.println("\nLow priority items :\n");
int listLow=sortLow.size();
for(int position=0; position <listLow; position++){
System.out.println(sortLow.get(position));
}
System.out.println("\n\nCash left for virtual list: ");
System.out.println(formatter.format(Food.currentCash));
System.out.println("\n\nCash left after real world prices: ");
System.out.println(formatter.format(Food.currentCash1));
String nameofFile="out.txt";
PrintWriter outputStream=null;
try{
outputStream=new PrintWriter(nameofFile);
}
catch(FileNotFoundException e)
{
System.out.println("Error opening file");
System.exit(0);
}
{
outputStream.println("\n"+yourName+"'s list of purchased Items:");
int listSize4=arrayList.size();
for(int position=0; position <listSize4; position++){
outputStream.println(arrayList.get(position));
}
outputStream.println("\nHigh priority items :\n");
int listHigh1=sortHigh.size();
for(int position=0; position <listHigh; position++){
outputStream.println(sortHigh.get(position));
}
outputStream.println("\nMedium priority items :\n");
int listMed1=sortMed.size();
for(int position=0; position <listMed1; position++){
outputStream.println(sortMed.get(position));
}
outputStream.println("\nLow priority items :\n");
int listLow1=sortLow.size();
for(int position=0; position <listLow; position++){
outputStream.println(sortLow.get(position));
}
outputStream.println("\nItems you did not buy, but you may need: \n");
int listDidntBuy=didntBuy.size();
for(int position=0; position <listDidntBuy; position++){
outputStream.println(didntBuy.get(position));
}
outputStream.close();
}
}
}
private static void addComponentsToPane(Container pane) {
{
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
}
}
}
My program has too many classes provide you with a small program, sorry. This is the class I am creating one of the GUIs in. Its the small GUI called "apple". The GUI behind apple in the picture is from another class and opened in Main. I also edited out the rest of the "grocery list objects" only including "apple".
Upvotes: 0
Views: 321
Reputation: 347314
You seem to be blocking the Event Dispatching Thread with some kind of long running process or infinite loop
Take a look at Concurrency in Swing for more details.
Upvotes: 1