Not really Understanding JPanel..
I don't get it... Why do we have to use JPanel??? Why cannot we just set the location of each button or whatever and be done with it?? It's making me so frustrated right now. Like how can I skip a panel from re-arranging my stuff urghh, why so complicated?
And how did you learn how to master arranging buttons. Links, anything. Thanks.
Re: Not really Understanding JPanel..
Using JPanel to make it easy to arrage every component which we want to put into the frame. Once you used the JPanel, you need to set a layout for it to make it easy for you to arrange all your components. Different layout have different ways of arrangement. For easy understanding, you just need to think that the JPanel is just an empty box and you need to decide how you going to place your stuff into the box. If you really feel that using JPanel bring you a lots of problem, you may use Netbean which provide drag and drop feature for you. Wish that the words I said might help you...
Re: Not really Understanding JPanel..
Yeah I guess. I i'll get by and learn it. Just found out how to use GridLayout, so I think I know how to use all the other ones now.. Just a pain in the butt =p
How do I set a text area a int? It only accepts string, do I have to parse int?? Does that make a int usuable as a string representing the number?
Re: Not really Understanding JPanel..
Yeah it's bit of a pain huh. Personally, I hate working on Java GUI's. Much rather work with VB.NET for graphical interfaces. Think of JPanel as a top level container, something that holds other things. JFrame is just the actual Window, title bar, etc.
Read up on it these at the Using Swing Components tutorial. In order to get the buttons placing in the right place you need to get familiar with Layout Managers. The place to do this is at: A visual guide to layout managers.
Personally, my favorite is the GroupLayout. It's one of the more complicated layout managers but it is the most flexible. I believe the netbeans GUI builder creates a GroupLayout. Speaking of which, it may be worth while checking out netbeans GUI builder. Many programmers look down there noses at it (for good reason) but it works and tweaking with the code it outputs taught me a lot about GroupLayouts.
As for why you can't just add buttons to a JFrame and tell it the X/Y position? Don't know. Could be that doing it that way is a restrictive design pattern, preventing more advanced graphical interfaces like layered panes and child windows.
Re: Not really Understanding JPanel..
Quote:
Originally Posted by
Emperor_Xyn
How do I set a text area a int? It only accepts string, do I have to parse int?? Does that make a int usuable as a string representing the number?
There are of course many, MANY ways you could this. I like stringbuffers. You will notice that the integer is not modified/destroyed in the process. You can continue to use it later on but it will not be updated in the text area until you tell it to.
Re: Not really Understanding JPanel..
@Christopher
StringBuffer is not as readable (to me) as
Code Java:
String str = myInteger+""; //The compiler will convert this to new StringBuilder().append(myInteger).append("").toString(), so unless your doing heavy string manipulation your fine.
//I haven't tried this in a while, but I can't see why they would change it
JTextArea area = new JTextArea(str);
and also isn't actually as efficient as you might think, because, as in the example, it converts the string addition into StringBuilder.append. So this:
Code Java:
String str = "Example "+"of what "+"I believe the compiler does";
is the same as
Code Java:
String str = new StringBuilder().append("Example").append("of what ").append("I believe the compiler does").toString();
I found this out when I used a decompiler on one of my .class's because I lost my source file somehow.
I find "String" + "String" much more readable, but I suppose it comes down to personal preferrence. It's just that I have noticed a few people didn't realize the
compiler did this. (I didn't for a while), and a myth (Or maybe it didn't do this in older versions?) came about that everything had to be StringBuilder to be efficient.
Links (Saying something like this requires these :P):
http://java.sun.com/docs/books/jls/t...html#15.18.1.2, which has an interesting explanation on how it evaluates string concat, and is also linked here
Really small type, but otherwise good explanation:
http://willcode4beer.blogspot.com/20...revisited.html
Re: Not really Understanding JPanel..
Quote:
Originally Posted by
ChristopherLowe
As for why you can't just add buttons to a JFrame and tell it the X/Y position? Don't know. Could be that doing it that way is a restrictive design pattern, preventing more advanced graphical interfaces like layered panes and child windows.
You can: Doing wihtout a layout manager
However, I wouldn't recommend it because it's a lower-level control and usually means you're going to end up doing more work for something that's not quite as nice as using a layout manager.
Re: Not really Understanding JPanel..
@HelloWorld922 - Oh cool! So there you have it, it can be done. I did actually try this out before posting but didn't realize you needed to setBounds() for it to work. Good spot.
@Tjstretch - Java strings are immutable, so like you said, the compiler needs to do some extra work behind the scenes to get String concatenation with '+' working. Normally, this extra workload is not really an issue but failing to recognize what this shortcut does to your complied code will bite you on the ass eventually.
For example, what if this integer was your health for a the HUD of a game. Using '+' you will be creating a tonne of extra objects at run-time, in a loop that instantly gets garbage collected. Why not just use the one object (StringBuffer or StringBuilder) that won't get GC'ed until it falls out of scope, will only get instantiated once and can be reused however you like. The biggest reason I like StringBuffer as opposed to StringBuilder is because you can tell it when to increases the buffer, (so if I know what kind of data I am dealing with I can fine tune it even more efficiently).
That is not to say I never use '+'. I do, just not often, the only time I do is for things like error messages or System.out.println() debugs; places where it is convenient to pack the code into one line and is not used for any computations or loops.
I completely agree that the '+' is more readable than StringBuffer but for the reasons we have both stated, it is not as good a solution. This being said, I did say there are plenty of ways to achieve this. Here is another one which may be easier to read.