Results 1 to 7 of 7
  1. #1
    alextemari is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default frames and panels

    Can anybody figure out what is wrong with the following code. I don't see the labels and textfields I expected to see. Thank you.



    import java.awt.*;
    import javax.swing.*;

    public class PracticeApp {

    public static void main(String[] args)
    {
    JFrame SalesF = new JFrame(); //SalesFrame();
    SalesF.setVisible(true);
    }
    }

    class SalesFrame extends JFrame
    {
    public SalesFrame()
    {
    setTitle("Sales");
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new SalesPanel());
    this.pack();
    centerWindow(this);
    }

    private void centerWindow(Window w)
    {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
    }
    }

    class SalesPanel extends JPanel
    {
    JPanel sPanel = new JPanel();
    private JLabel prompt1Label, prompt2Label; //prompt3Label, prompt4Label;
    public JTextField prompt1TextField, prompt2TextField, prompt3TextField;



    public SalesPanel()
    {
    // add panels
    setLayout(new GridBagLayout());

    prompt1Label = new JLabel("Enter ISBN");
    add(prompt1Label, getConstraints(0,0,1,1, GridBagConstraints.EAST));

    prompt1TextField = new JTextField(10);
    prompt1TextField.setEditable(true);
    prompt1TextField.setFocusable(false);
    add(prompt1TextField, getConstraints(1,0,1,1, GridBagConstraints.WEST));

    prompt2Label = new JLabel("Enter Title");
    add(prompt2Label, getConstraints(0,1,1,1, GridBagConstraints.EAST));


    prompt1TextField = new JTextField(10);
    prompt1TextField.setEditable(false);
    prompt1TextField.setFocusable(false);
    add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));


    prompt3TextField = new JTextField(10);
    prompt3TextField.setEditable(false);
    prompt3TextField.setFocusable(false);
    add(prompt3TextField, getConstraints(1,2,2,4, GridBagConstraints.WEST));
    }

    private GridBagConstraints getConstraints(int gridx, int gridy,
    int gridwidth, int gridheight, int anchor)
    {
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 5, 5);
    c.ipadx = 0;
    c.ipady = 0;
    c.gridx = gridx;
    c.gridy = gridy;
    c.gridwidth = gridwidth;
    c.gridheight = gridheight;
    c.anchor = anchor;
    return c;
    }
    }

  2. #2
    MatthewAB is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    I'd appreciate if someone with some knowledge of JFrames could take a look at this, as I am also curious. I think a panel should be added to the frame in main, but I couldn't get it to work right. Also the sizing doesn't seem to be staying as specified. From my experience the frame usually wraps around content and makes itself as small as possible, but it seems like with specified size it should stay that way.

  3. #3
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

    Default

    You never instantiated a SalesFrame object. This line simply creates the standard built-in JFrame:

    Java Code:
    public static void main(String[] args)
    {
    [B]JFrame SalesF = new JFrame(); //SalesFrame();[/B]
    SalesF.setVisible(true);
    }
    }
    You need to do this instead:
    Java Code:
    SalesFrame SalesF = new SalesFrame();
    SalesF.setVisible(true);
    Also comment out this line (it had an error that you need to fix):
    Java Code:
    prompt1TextField = new JTextField(10);
    prompt1TextField.setEditable(false);
    prompt1TextField.setFocusable(false);
    [B]//add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));[/B]
    
    
    prompt3TextField = new JTextField(10);
    prompt3TextField.setEditable(false);
    prompt3TextField.setFocusable(false);
    add(prompt3TextField, getConstraints(1,2,2,4, GridBagConstraints.WEST));
    With those changes, I was able to run your project and see the dialog box with label, etc.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Use the code tags to maintain the readability of your code. And go through this link
    SSCCE (Short, Self Contained, Compilable and Executable)
    It will help you to get rid of extraneous code that has nothing at all to do with the stated problem (like setEditable and setFocusable calls).

    db

  5. #5
    MatthewAB is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    FlipPoker, how were you able to find that the line:
    "add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));"
    was not needed?

    I'm not sure what the line even does, but I have the code in Eclipse and it doesn't make a reference to an error in that class at all.

  6. #6
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

    Default

    Quote Originally Posted by MatthewAB View Post
    FlipPoker, how were you able to find that the line:
    "add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));"
    was not needed?

    I'm not sure what the line even does, but I have the code in Eclipse and it doesn't make a reference to an error in that class at all.
    I'm not saying it's not needed. I just commented it out because it threw an exception. I was just trying to get your dialog to show up.

    But now I see the problem. You never instantiated prompt2TextField. So that add() method threw a null pointer exception. I think you meant to do this (which does work now):

    Java Code:
    [B]prompt2TextField [/B]= new JTextField(10);
    [B]prompt2TextField[/B].setEditable(false);
    [B]prompt2TextField[/B].setFocusable(false);
    add(prompt2TextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));

  7. #7
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Thanks for your help and there were lot of things to be fixed in that cluster. I am replying as a different user because I lost my login info. if you want to see what the code was supposed to do look at my other post "fix my app".

Similar Threads

  1. Need some help with panels inside panels
    By kakefjes in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2011, 11:36 AM
  2. Java Frames
    By Java Unknown in forum New To Java
    Replies: 2
    Last Post: 02-22-2011, 05:45 AM
  3. jsp frames
    By vasug in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-16-2010, 05:00 PM
  4. Switching Frames
    By jonnytabpni in forum New To Java
    Replies: 1
    Last Post: 11-08-2009, 10:12 PM
  5. Help regarding Frames
    By ramesh.8189 in forum AWT / Swing
    Replies: 14
    Last Post: 02-15-2009, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •