Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Frame not displaying:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Frame not displaying:

    Below is the code I am having a issue with. I created a instance of a class AddItemView, inside StartUpMenuController. I then passed it into the class, the main method is below showing that as well. However when I do this:

    else if(e.getSource()==menu.addBtn)
    			{
    				new addItem();
    			}

    I get an error, little red line on the bottom of the text. I am testing the frame at the moment to make sure it is what I want before I move on to the Controller side of it. I just want to display it and go from there. Any idea what I am doing wrong?

    package mainMenu.Home.DataBase;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import addItemBtn.Home.DataBase.AddItemView;
     
    public class StartUpMenuController 
    {
    	StartUpMenu menu;
    	AddItemView addItem;
     
    	StartUpMenuController(StartUpMenu menu,AddItemView addItem)
    	{
    		this.menu = menu;
    		this.addItem = addItem;
     
    		menu.closeBtn.addActionListener(new AddListeners());
    		menu.reportsBtn.addActionListener(new AddListeners());
    		menu.findItemBtn.addActionListener(new AddListeners());
    		menu.addBtn.addActionListener(new AddListeners());
    	}
     
     
    	class AddListeners implements ActionListener
    	{
     
    		public void actionPerformed(ActionEvent e) 
    		{
    			try
    			{
    			if(e.getSource()==menu.closeBtn)
    			{
    				menu.frame.dispose();
    			}
    			else if(e.getSource()==menu.addBtn)
    			{
    				new addItem();
    			}
    			else if(e.getSource()==menu.reportsBtn)
    			{
     
    			}
    			else if(e.getSource()==menu.reportsBtn)
    			{
     
    			}
     
    			}catch(Exception ex)
    			{
    				System.out.println("There was an error "+e);
    			}
     
    		}
     
    	}
    	public static void main(String args[])
    	{
    		StartUpMenu menu = new StartUpMenu();
    		AddItemView addItem = new AddItemView();
     
    		new StartUpMenuController(menu,addItem);
    	}
     
    }


    --- Update ---

    Not sure if this is the issue but the class I am making a new instance of is in a different package. I imported the package though:
    import addItemBtn.Home.DataBase.AddItemView;


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Frame not displaying:

    When you hover over the "little red line", what is the error displayed in the box that appears? You can even mouse into the box, select the text, copy it, and then paste it here.

    You may also get a related error if you try to compile, but sometimes it'll compile fine, other you may get an ambiguous error description.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Frame not displaying:

    Holly Molly it does run, and the frame looks like total crap which is why I am testing it. I just hint proceed anyway when eclipse warned me.

    Change to AddItemModel
    Change to AddItemView
    Create class AddItem
    Fix project setup

    Does this help?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Frame not displaying:

    You didn't include the error part (the very top line), but from the suggested fixes I gather that AddItem or addItem is not recognized as a part of the project. This line:

    new addItem();

    suggests a class named addItem, but Java class names and their constructors should begin with capital letters. It will be more difficult to help you if you do not follow Java's naming conventions.

    You've highlighted another fallback to having your code spread across multiple projects: it's difficult (near impossible) to post pieces of the project in a forum like this to get help from others, because we can't or don't want to replicate your setup.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Frame not displaying:

    addItem is a class but it is a instance of the class AddItemView(). I am simply trying to call the constructor which holds all the info for the new frame in order for the user to enter a item into the database.

    I handled it like this:

    public class StartUpMenuController 
    {
    	StartUpMenu menu;
    	AddItemView addItem;
     
    	StartUpMenuController(StartUpMenu menu, AddItemView addItem)
    	{
    		this.menu = menu;
    		this.addItem = addItem;
     
    		menu.closeBtn.addActionListener(new AddListeners());
    		menu.reportsBtn.addActionListener(new AddListeners());
    		menu.findItemBtn.addActionListener(new AddListeners());
    		menu.addBtn.addActionListener(new AddListeners());
    	}

    I then call the instated class in the else if statement like so:

    public void actionPerformed(ActionEvent e) 
    		{
    			try
    			{
    			if(e.getSource()==menu.closeBtn)
    			{
    				menu.frame.dispose();
    			}
    			else if(e.getSource()==menu.addBtn)
    			{
    				addItem();
    			}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Frame not displaying:

    Your names make it hard to follow. The name: addItem is a variable and a method and also you say its a class.
    That makes the code hard to read and understand. There should be different names for each of those 3 uses:
    AddItem for the class
    addItemView for the instance of AddItemView
    addItem() is OK for a method name
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Frame not displaying:

    Good point and fixed it but still having the same issue.

    public class StartUpMenuController 
    {
    	StartUpMenu menu;
    	AddItemView addItemView;
     
    	StartUpMenuController(StartUpMenu menu, AddItemView addItemView)
    	{
    		this.menu = menu;
    		this.addItemView = addItemView;
     
    		menu.closeBtn.addActionListener(new AddListeners());
    		menu.reportsBtn.addActionListener(new AddListeners());
    		menu.findItemBtn.addActionListener(new AddListeners());
    		menu.addBtn.addActionListener(new AddListeners());
    	}

    class AddListeners implements ActionListener
    	{
     
    		public void actionPerformed(ActionEvent e) 
    		{
    			try
    			{
    			if(e.getSource()==menu.closeBtn)
    			{
    				menu.frame.dispose();
    			}
    			else if(e.getSource()==menu.addBtn)
    			{
    				addItemView();
    			}
    			else if(e.getSource()==menu.reportsBtn)
    			{
     
    			}


    --- Update ---

    Ok so I made a method in AddItemView Class called add and called it in the else if statement in StartUpMenuController Class.
    else if(e.getSource()==menu.addBtn)
    			{
    				addItemView.add();
    			}

    The error went away, but here is the thing, the add method has NOTHING in it.

    Another thing is that even before I click the button the page opens from constructor of AddItemView. Very confused. I hope i didn't confuse you too.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Frame not displaying:

    Can you make a small, complete program that compiles, executes and shows the problem for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Frame not displaying:

    It runs now, but opens the new frame right away without me ever hitting the button. Even weirder I am call a method that has nothing in it.
    This:
    else if(e.getSource()==menu.addBtn)
    			{
    				addItemView.add();
    			}
    Is calling this:
    public void add()
    	{
     
    	}


    Here is what the other class looks like, I am having issues with how it is layout too. But I will get to that later I suppose. For some reason, the name textfield is not displaying nameBox just it label nameLbl.

    public AddItemView()
    	{
    		//set up frame
    		frame = new JFrame("Add Item");
    		frame.setSize(750,500);
    		frame.setLayout(new BorderLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create text boxes and labels
    		nameLbl = new JLabel("Name:");
    		nameBox = new JTextField(15);
     
    		priceLbl = new JLabel("Price:");
    		priceBox = new JTextField(15);
     
    		locationLbl = new JLabel("Location:");
    		locationBox = new JTextField(15);
     
    		//add text fields to textPanel
    		textPanel = new JPanel();
    		textPanel.setLayout(new GridLayout(6,4));
    		textPanel.setSize(100,75);
     
    		textPanel.add(nameLbl);
    		textPanel.add(nameBox);
    		textPanel.add(priceLbl);
    		textPanel.add(nameBox);
    		textPanel.add(locationLbl);
    		textPanel.add(locationBox);
    		textPanel.setVisible(true);
     
    		//create file chooser upload a picture
    		fileopen = new JFileChooser();
    		browseBtn = new JButton("Browse");
     
    		//create and add file chooser to panel
    		picturePanel = new JPanel();
    		picturePanel.add(fileopen);
    		picturePanel.add(browseBtn);
     
    		//create buttons
    		btnPanel = new JPanel();
    		btnPanel.setLayout(new FlowLayout());
    		btnPanel.setSize(getPreferredSize());
    		submitBtn = new JButton("Submit");
    		cancelBtn = new JButton("Cancel");
    		previewBtn = new JButton("Preview");
     
    		//add buttons to panel
    		btnPanel.add(submitBtn);
    		btnPanel.add(cancelBtn);
    		btnPanel.add(previewBtn);
    		btnPanel.setVisible(true);
     
    		//create description text area
    		descriptionLbl = new JLabel("Description:");
    		descriptionBox = new JTextArea();
     
    		//add to description text area to panel
    		descriptionPanel = new JPanel();
    		descriptionPanel.setLayout(new FlowLayout());
    		descriptionPanel.setSize(150,100);
    		descriptionPanel.add(descriptionLbl);
    		descriptionPanel.add(descriptionBox);
     
    		//place textFields to top left of frame
    		frame.add(textPanel,BorderLayout.NORTH);
    		//place picture panel into top right of frame
    		frame.add(picturePanel,BorderLayout.NORTH);
    		//place descriptionPanel to the center of the frame
    		frame.add(descriptionPanel,BorderLayout.CENTER);
    		//place buttons on bottom of frame
    		frame.add(btnPanel,BorderLayout.SOUTH);
     
    		frame.setVisible(true);
    	}
     
    	public void add()
    	{
     
    	}

    This is what it looks like once it compiles.
    Last edited by jocdrew21; September 20th, 2014 at 11:42 AM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Frame not displaying:

    Sorry, without code that compiles and executes, it can be very hard to guess what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Frame not displaying:

    package mainMenu.Home.DataBase;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import addItemBtn.Home.DataBase.AddItemView;
     
    public class StartUpMenuController 
    {
    	StartUpMenu menu;
    	AddItemView addItemView;
     
    	StartUpMenuController(StartUpMenu menu, AddItemView addItemView)
    	{
    		this.menu = menu;
    		this.addItemView = addItemView;
     
    		menu.closeBtn.addActionListener(new AddListeners());
    		menu.reportsBtn.addActionListener(new AddListeners());
    		menu.findItemBtn.addActionListener(new AddListeners());
    		menu.addBtn.addActionListener(new AddListeners());
    	}
     
     
    	class AddListeners implements ActionListener
    	{
     
    		public void actionPerformed(ActionEvent e) 
    		{
    			try
    			{
    			if(e.getSource()==menu.closeBtn)
    			{
    				menu.frame.dispose();
    			}
    			else if(e.getSource()==menu.addBtn)
    			{
    				addItemView.add();
    			}
    			else if(e.getSource()==menu.reportsBtn)
    			{
     
    			}
    			else if(e.getSource()==menu.reportsBtn)
    			{
     
    			}
     
    			}catch(Exception ex)
    			{
    				System.out.println("There was an error "+e);
    			}
     
    		}
     
    	}
    	public static void main(String args[])
    	{
    		StartUpMenu menu = new StartUpMenu();
    		AddItemView addItem = new AddItemView();
     
    		new StartUpMenuController(menu,addItem);
    	}
     
    }

    package addItemBtn.Home.DataBase;
     
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
     
    import javax.swing.*;
     
    public class AddItemView extends JFrame
    {
     
    	private static final long serialVersionUID = 1L;
    	JFrame frame;
    	JPanel btnPanel,descriptionPanel,picturePanel,textPanel;
    	JTextField nameBox,priceBox,locationBox;
    	JLabel nameLbl,descriptionLbl,priceLbl,locationLbl,pictureLbl;
    	JTextArea descriptionBox;
    	JButton submitBtn,cancelBtn,previewBtn,browseBtn;
     
    	JFileChooser fileopen;
     
    	public AddItemView()
    	{
    		//set up frame
    		frame = new JFrame("Add Item");
    		frame.setSize(750,500);
    		frame.setLayout(new BorderLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create text boxes and labels
    		nameLbl = new JLabel("Name:");
    		nameBox = new JTextField(15);
     
    		priceLbl = new JLabel("Price:");
    		priceBox = new JTextField(15);
     
    		locationLbl = new JLabel("Location:");
    		locationBox = new JTextField(15);
     
    		//add text fields to textPanel
    		textPanel = new JPanel();
    		textPanel.setLayout(new GridLayout(6,4));
    		textPanel.setSize(100,75);
     
    		textPanel.add(nameLbl);
    		textPanel.add(nameBox);
    		textPanel.add(priceLbl);
    		textPanel.add(nameBox);
    		textPanel.add(locationLbl);
    		textPanel.add(locationBox);
    		textPanel.setVisible(true);
     
    		//create file chooser upload a picture
    		fileopen = new JFileChooser();
    		browseBtn = new JButton("Browse");
     
    		//create and add file chooser to panel
    		picturePanel = new JPanel();
    		picturePanel.add(fileopen);
    		picturePanel.add(browseBtn);
     
    		//create buttons
    		btnPanel = new JPanel();
    		btnPanel.setLayout(new FlowLayout());
    		btnPanel.setSize(getPreferredSize());
    		submitBtn = new JButton("Submit");
    		cancelBtn = new JButton("Cancel");
    		previewBtn = new JButton("Preview");
     
    		//add buttons to panel
    		btnPanel.add(submitBtn);
    		btnPanel.add(cancelBtn);
    		btnPanel.add(previewBtn);
    		btnPanel.setVisible(true);
     
    		//create description text area
    		descriptionLbl = new JLabel("Description:");
    		descriptionBox = new JTextArea();
     
    		//add to description text area to panel
    		descriptionPanel = new JPanel();
    		descriptionPanel.setLayout(new FlowLayout());
    		descriptionPanel.setSize(150,100);
    		descriptionPanel.add(descriptionLbl);
    		descriptionPanel.add(descriptionBox);
     
    		//place textFields to top left of frame
    		frame.add(textPanel,BorderLayout.NORTH);
    		//place picture panel into top right of frame
    		frame.add(picturePanel,BorderLayout.NORTH);
    		//place descriptionPanel to the center of the frame
    		frame.add(descriptionPanel,BorderLayout.CENTER);
    		//place buttons on bottom of frame
    		frame.add(btnPanel,BorderLayout.SOUTH);
     
    		frame.setVisible(true);
    	}
     
    	public void add()
    	{
     
    	}
    }

    Database.jpg

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Frame not displaying:

    There are compiler errors from missing classes: StartupMenu
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Frame not displaying:

    No here is that class....

    package mainMenu.Home.DataBase;
     
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
     
    import javax.swing.*;
     
    public class StartUpMenu extends JFrame
    {
     
    	private static final long serialVersionUID = 1L;
     
    	Connection conn;
    	JFrame frame;
    	JPanel panel;
    	JPanel panelIcon;
     
    	JButton reportsBtn;
    	JButton findItemBtn;
    	JButton closeBtn;
    	JButton addBtn;
     
    	JLabel pic;
     
    	StartUpMenu()
    	{
    		frame = new JFrame("Jochams Home Database");
    		frame.setSize(400,250);
    		frame.setLayout(new BorderLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//panel will hold the buttons
    		panel = new JPanel();
    		panel.setSize(150,250);
    		panel.setLayout(new GridLayout(2,2));
     
    		reportsBtn = new JButton("Generate Report");
    		findItemBtn = new JButton("Find Item");
    		closeBtn = new JButton("Close");
    		addBtn = new JButton("Add Item");
     
    		//panelIcon will hold label with image
    		panelIcon = new JPanel();
    		panelIcon.setLayout(new FlowLayout());
    		pic = new JLabel();
     
    		panelIcon.add(pic);
    		ImageIcon icon = new ImageIcon("dataImage.jpeg");
    		pic.setIcon(icon);
     
    		//adding buttons to panel
    		panel.add(reportsBtn);
    		panel.add(findItemBtn);
    		panel.add(addBtn);
    		panel.add(closeBtn);
     
    		//adding panel with image label to frame
    		frame.add(panelIcon);
    		//setting buttons to bottom of frame
    		frame.add(panel, BorderLayout.SOUTH);
    		frame.setVisible(true);
     
    		esstablishConnection();//connects to mySQL
    	}
     
    	//connects to mySQL
    	void esstablishConnection()
    	{
    		Object[] options = {"Try Again","Cancel"};
    		try{
    			 conn = null;//8889
    	         String url ="jdbc:mysql://localhost:3306/ch18";
    	         String user="root";
    	         String pass="root";
    	         conn = DriverManager.getConnection(url,user,pass);
     
    	         //if connect show message
    	         JOptionPane.showMessageDialog(frame,
    	        		 "Connectioned,"
    	        		 + JOptionPane.PLAIN_MESSAGE);
    		}catch(SQLException e)
    		{
    			//error cannot to database yes or no to try and reconnect
    			System.out.println("Error Catch:"+e);
    			int n=JOptionPane.showOptionDialog(frame,
    					"Unable to connect to database\n"
    					+"click try again or Cancel\n",
    					"ERROR", JOptionPane.YES_NO_CANCEL_OPTION,
    					JOptionPane.PLAIN_MESSAGE, null,
    					options,
    					options[0]);
     
    			if(n==JOptionPane.YES_OPTION)
    			{
    				esstablishConnection();
    			}
    			else if(n==JOptionPane.NO_OPTION)
    			{
    				System.out.println("Fine");
    			}
    		}finally//close database connection
    		{
    			if(conn != null)
    			{
    				try{
    					conn.close();
    					} catch(Exception e)
    					{
    						JOptionPane.showMessageDialog(frame,
    								"Unable to close database,"
    								+ JOptionPane.ERROR_MESSAGE);
    					}
    			}
    		}
     
    	}
     
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Frame not displaying:

    To see what code is executed and when it is executed, add some println() statements to all the methods and constructors to show what is executed and when.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    jocdrew21 (September 20th, 2014)

  16. #15
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Frame not displaying:

    Ok got it...

    I was calling the addItemView class inside the constructor, so naturally it was going to open right away. I took it out and created a new instance of the class inside the else if statement and it is working great.

    else if(e.getSource()==menu.addBtn)
    			{
    				new AddItemView();
    			}
    Shall I post a new thread about the layout of the swing application. It is not at all what I wanted to do. I am made a JFrame and I am positioning different panels on to it. The picture I post reflects what I am talking about in post #11.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Frame not displaying:

    post a new thread about the layout of the swing application.
    That sounds like a different problem, so create a new thread for it in the AWT / Java Swing section.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: January 19th, 2012, 03:44 PM
  2. frame not displaying correct contents
    By yemista in forum AWT / Java Swing
    Replies: 1
    Last Post: October 20th, 2011, 08:58 AM
  3. problem displaying a new frame
    By yemista in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2011, 02:08 PM
  4. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  5. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 10:31 PM