Results 1 to 4 of 4
  1. #1
    toecutter is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default Why wont my object render properly

    Problem: none of the components render as is, however comment either of the .addLayout methods and all of the sudden they render. Keep in mind each layout is in a different class, my confusion is "isn't encapsulation supposed to mean that these elements shouldn't affect each other?"

    so any tips, hints, solutions would be very appreciated

    Alright so here is my code for Main class

    package eventtracker;

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



    public class Main {


    public static void main(String[] args) {
    JFrame testScreen = new JFrame();
    testScreen.setDefaultCloseOperation(WindowConstant s.EXIT_ON_CLOSE);
    SpringLayout layout = new SpringLayout();

    Day testDay = new Day(5);
    layout.putConstraint(SpringLayout.WEST , testDay, 5, SpringLayout.WEST , testScreen);
    layout.putConstraint(SpringLayout.NORTH, testDay, 5, SpringLayout.NORTH, testScreen);


    testScreen.add(testDay);
    testScreen.setSize(300,300);
    testScreen.setLayout(layout);
    testScreen.setVisible(true);
    }

    }

    and here is the code for my Day class (yeah names not finalized :))


    package eventtracker;


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

    public class Day extends JPanel{
    JLabel dayNumber = new JLabel();
    JTextArea dayText = new JTextArea();
    SpringLayout dayLayout = new SpringLayout();

    public Day(int number)
    {

    dayNumber.setText(Integer.toString(number));
    Font dayFont = new Font("nothing", Font.BOLD, 25);
    dayNumber.setFont(dayFont);

    //arranging the springs
    dayLayout.putConstraint(SpringLayout.WEST, dayNumber, 5, SpringLayout.WEST, this);
    dayLayout.putConstraint(SpringLayout.NORTH, dayNumber , 5, SpringLayout.NORTH, this);

    dayLayout.putConstraint(SpringLayout.WEST, dayText, 5, SpringLayout.WEST, this);
    dayLayout.putConstraint(SpringLayout.NORTH, dayText , 5, SpringLayout.SOUTH, dayNumber);

    this.setSize(60,60);
    dayNumber.setSize(10, 10);
    dayText.setSize(40,40);

    dayText.setText("ANYTHING");

    this.add(dayNumber);
    this.add(dayText);
    this.setLayout(dayLayout);


    }
    public void setDayText(String text)
    {
    dayText.setText(text);
    }
    public void appendDayText(String text)
    {
    dayText.setText(dayText.getText() + " " + text);
    }

    }

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    One possible problem is the order of the code:
    Java Code:
    this.add(dayNumber);
    this.add(dayText);
    this.setLayout(dayLayout);   // [COLOR="Red"]TOO LATE[/COLOR]
    The layout manager MUST be set before add()ing Components.
    Otherwise the layout manager will not learn about the Components
    and will not lay them out.

    Another possible problem is the use of setSize().
    That method works best when called from the layout manager.
    If you want to say what size a java.swing element would like to be,
    you can call setMinimumSize(), setPreferredSize(), or both.

  3. #3
    toecutter is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    I've been swapping the code and commenting out different parts in debugging. While if this were a complicated layout setting the layout last might throw my display off, but not displaying anything is a bit odd. (normally layout errors tend to just lead to odd object placement)

    However when I decide to comment one of my setlayout commands the other layoutmanager works 100% as intended. This means that in spite of making the error in adding objects before layouts the frame still understands the object layout relation. The order of operations is most likely not the source of my frustration in this case.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

Similar Threads

  1. Replies: 7
    Last Post: 10-06-2009, 05:58 AM
  2. why wont it compile
    By bje98f in forum Advanced Java
    Replies: 1
    Last Post: 04-23-2009, 10:55 PM
  3. Replies: 4
    Last Post: 01-10-2009, 12:17 AM
  4. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:19 AM
  5. Loki Render 0.3
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-26-2007, 08:31 PM

Posting Permissions

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