Results 1 to 4 of 4
- 10-12-2009, 04:29 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
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);
}
}
- 10-21-2009, 09:04 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
One possible problem is the order of the code:
The layout manager MUST be set before add()ing Components.Java Code:this.add(dayNumber); this.add(dayText); this.setLayout(dayLayout); // [COLOR="Red"]TOO LATE[/COLOR]
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.
- 10-22-2009, 05:54 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
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.
- 10-22-2009, 01:43 PM #4
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
You might want to take a look at Using Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
Similar Threads
-
Rotated Shape Object Line weight is not retaining properly in printing
By dorairaj in forum AWT / SwingReplies: 7Last Post: 10-06-2009, 05:58 AM -
why wont it compile
By bje98f in forum Advanced JavaReplies: 1Last Post: 04-23-2009, 10:55 PM -
Serializing an object not working properly - funky chars.
By getstarted in forum XMLReplies: 4Last Post: 01-10-2009, 12:17 AM -
save will work but load wont?!?!
By Sticks_ll in forum New To JavaReplies: 1Last Post: 06-12-2008, 04:19 AM -
Loki Render 0.3
By levent in forum Java SoftwareReplies: 0Last Post: 07-26-2007, 08:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks