I would like to post an SSCCE here for review. However, Eclipse tends
to make the code rather ugly and impossible to cut and paste into an ide
or text editor for compiling. I would like to know how to fix my
Eclipse installation so that it does proper indentation. I seem to
recall someone mentioning a configuration option that will make this
work for me, but I cannot find the exact thread or post.
BTW - I just discovered something very cool about Eclipse, although it
might not be new to others. It is possible to install Eclipse on an
SD/MMC card (I use a USB card reader) and run it from there. So my
entire workspace and IDE is contained on the SD card.
--
PSK - RLU 452647 http://improve-usenet.org
Killing GG and Supernews now on Ubuntu, Vista and XP
http://www.grc.com/stevegibson.htm
|
|
0
|
|
|
|
Reply
|
Pseudo
|
9/16/2007 2:51:01 PM |
|
Pseudo Silk Kimono wrote:
> I would like to know how to fix my
> Eclipse installation so that it does proper indentation. I seem to
> recall someone mentioning a configuration option that will make this
> work for me, but I cannot find the exact thread or post.
Project,
Properties,
Java Code Style,
Formatter,
Configure Workspace Settings ...
Select a profile
Java Conventions
|
|
0
|
|
|
|
Reply
|
RedGrittyBrick
|
9/16/2007 4:59:04 PM
|
|
On 2007-09-16, RedGrittyBrick <RedGrittyBrick@SpamWeary.foo> wrote:
> Pseudo Silk Kimono wrote:
>
>> I would like to know how to fix my
>> Eclipse installation so that it does proper indentation. I seem to
>> recall someone mentioning a configuration option that will make this
>> work for me, but I cannot find the exact thread or post.
>
> Project,
> Properties,
> Java Code Style,
> Formatter,
> Configure Workspace Settings ...
> Select a profile
> Java Conventions
>
If this doesn't look good just ignore it.
import java.util.ArrayList;
import java.util.Random;
/**
*
* @author PSK
*/
public class fortuneScreen extends javax.swing.JFrame {
private static ArrayList<String> listOfFortunes = new ArrayList();
{
listOfFortunes.add("I Like food");
listOfFortunes
.add("dude. i think the bootstrap loader has your leg.
try not to move. ill get this POST over here and bit it to death. damn
boot strap loaders");
listOfFortunes
.add("i didn't sell my soul i just lease it out on a
monthly basis");
listOfFortunes
.add("If a cow and a half and a calf and a half can eat
a bale and a half of hay in a day and a half, how many waffles does it
take to shingle a dog house?");
listOfFortunes
.add("The difference between a Miracle and a Fact is
exactly the difference between a mermaid and a seal.");
listOfFortunes
.add("Many a writer seems to think he is never profound
except when he can't understand his own meaning.");
listOfFortunes.add("Beware of a tall blond man with one black
shoe.");
listOfFortunes
.add("You will be attacked next Wednesday at 3:15 p.m.
by six samurai sword wielding purple fish glued to Harley-Davidson
motorcycles.");
listOfFortunes
.add("You have many friends and very few living
enemies.");
listOfFortunes.add("You are magnetic in your bearing.");
listOfFortunes.add("Harp not on that string.");
listOfFortunes
.add("You could live a better life, if you had a better
mind and a better body.");
listOfFortunes
.add("You will be advanced socially, without any special
effort on your part.");
listOfFortunes
.add("You may be gone tomorrow, but that doesn't mean
that you weren't here today.");
listOfFortunes
.add("Wrinkles should merely indicate where smiles have
been.");
listOfFortunes.add("A piano will fall near you at 2pm");
listOfFortunes.add("Live long, and prosper");
listOfFortunes.add("You will be assimilated");
listOfFortunes.add("I am nothing");
}
public fortuneScreen() {
initComponents();
}
private void initComponents() {
randomJButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
fortuneTextArea = new javax.swing.JTextArea();
exitJButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
randomJButton.setText("Press for a Random Fortune");
randomJButton.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
randomJButtonActionPerformed(evt);
}
});
fortuneTextArea.setColumns(50);
fortuneTextArea.setRows(10);
jScrollPane1.setViewportView(fortuneTextArea);
exitJButton.setText("Exit");
exitJButton.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
exitJButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(
76,
76,
76)
.addComponent(
randomJButton)
.addGap(
88,
88,
88)
.addComponent(
exitJButton))
.addGroup(
layout
.createSequentialGroup()
.addContainerGap()
.addComponent(
jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE,
480,
Short.MAX_VALUE)))
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGap(39, 39,
39).addComponent(
jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
31,
javax.swing.GroupLayout.PREFERRED_SIZE).addGap(41,
41, 41).addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(randomJButton).addComponent(
exitJButton)).addContainerGap(21,
Short.MAX_VALUE)));
pack();
}// </editor-fold>//GEN-END:initComponents
private void exitJButtonActionPerformed(java.awt.event.ActionEvent
evt) {//GEN-FIRST:event_exitJButtonActionPerformed
System.exit(0);
}//GEN-LAST:event_exitJButtonActionPerformed
private void randomJButtonActionPerformed(java.awt.event.ActionEvent
evt) {//GEN-FIRST:event_randomJButtonActionPerformed
Random generator = new Random();
int randomIndex = generator.nextInt(18);
String fortuneDisplay = "Fortune number: "
+ String.valueOf(randomIndex) + " "
+ listOfFortunes.get(randomIndex);
fortuneTextArea.setText(fortuneDisplay);
}//GEN-LAST:event_randomJButtonActionPerformed
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new fortuneScreen().setVisible(true);
}
});
}
private javax.swing.JButton exitJButton;
private javax.swing.JTextArea fortuneTextArea;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton randomJButton;
--
PSK - RLU 452647 http://improve-usenet.org
Killing GG and Supernews now on Ubuntu, Vista and XP
http://www.grc.com/stevegibson.htm
|
|
0
|
|
|
|
Reply
|
Pseudo
|
9/16/2007 6:49:30 PM
|
|
Pseudo Silk Kimono wrote:
>>> I would like to know how to fix my
>>> Eclipse installation so that it does proper indentation. I seem to
Set it to use spaces instead of TABs. You should not embed TABs in Usenet
source listings.
> If this doesn't look good just ignore it.
That's not really good advice. The layout affects the ability, and arguably
the willingness of people to read your listing with the attention needed to
help you.
> private static ArrayList<String> listOfFortunes = new ArrayList();
> {
> listOfFortunes.add("I Like food");
.... // more add() calls
> listOfFortunes.add("You will be assimilated");
> listOfFortunes.add("I am nothing");
> }
This initialization will have the unfortunate effect of causing your fortunes
list to grow each time you instantiate the object.
Why do you declare the list static but initialize it in the constructor?
Wouldn't it be better to initialize the list statically, or else declare it as
an instance variable?
Apropos of that collection, don't mix generics and raw types.
The variable should be of the interface type, not the concrete implementation.
It is inadvisable to build the implementation type into the variable name.
"listOf" is redundant and will cause trouble if you decide later to change the
implementation.
private static List<String> fortunes = new ArrayList<String>();
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
9/16/2007 7:06:19 PM
|
|
Pseudo Silk Kimono wrote:
> On 2007-09-16, RedGrittyBrick <RedGrittyBrick@SpamWeary.foo> wrote:
>> Pseudo Silk Kimono wrote:
>>
>>> I would like to know how to fix my
>>> Eclipse installation so that it does proper indentation. I seem to
>>> recall someone mentioning a configuration option that will make this
>>> work for me, but I cannot find the exact thread or post.
>> Project,
>> Properties,
>> Java Code Style,
>> Formatter,
>> Configure Workspace Settings ...
>> Select a profile
>> Java Conventions
>>
>
> If this doesn't look good just ignore it.
>
It doesn't.
You may need to select "Show" and change the indent policy to "spaces
only". Make sure the indentation size is set to 4
Also go to "line wrapping" and set the maximum line width to something
appropriate (e.g. 80 or even 72).
Then save the profile with a new name and make sure it is set as your
default.
The actual code may then need to be reformatted using Ctrl+Shift+F
(or use Menu: Source, Format)
P.S. There are support neswgroups for Eclipse which you can access via
the "community" section of elipse.org
|
|
0
|
|
|
|
Reply
|
RedGrittyBrick
|
9/16/2007 7:13:23 PM
|
|
On Sun, 16 Sep 2007 19:06:19 GMT, Lew expounded upon us in
comp.lang.java.help :
>
> Why do you declare the list static but initialize it in the
> constructor? Wouldn't it be better to initialize the list
> statically, or else declare it as an instance variable
Lew, perhaps I am wrong but is it not the case that my static
ArrayList is only created once. I was going to put the creation in
the method that picks and displays the random fortune, but I knew
that since I only needed it to be created once, why have the JVM
create it every time.
> Apropos of that collection, don't mix generics and raw types.
>
> The variable should be of the interface type, not the concrete
> implementation.
I don't understand this. I used Netbeans to do all the initial
work and added the fortune picking piece in Eclipse once I had the
layout working and the button doing something.
>
> It is inadvisable to build the implementation type into the
> variable name. "listOf" is redundant and will cause trouble if
> you decide later to change the implementation.
>
This makes sense.
I'll make some changes, fix the indentation and repost with the
intention of this being the solution to one of Roedy's beginnger
projects.
|
|
0
|
|
|
|
Reply
|
Pseudo
|
9/17/2007 4:09:18 PM
|
|
On Sun, 16 Sep 2007 19:06:19 GMT, Lew expounded upon us in
comp.lang.java.help :
>That's not really good advice. The layout affects the ability, and arguably
>the willingness of people to read your listing with the attention needed to
>help you
This program is meant to be a solution to a question on Roedy's beginner
projects page. http://www.mindprod.com/project/beginner.html
A simple GUI with a button. When you press it, the program displays one of a
number of preselected �fortunes�.
I still have to figure out how to make the longer fortunes display without
opening a scroll panel. It would be nice if the jTextArea had a dynamic
sizing method. It might, but I haven't looked yet.
package fortuneTeller;
import java.util.ArrayList;
import java.util.Random;
/**
*
* SSCCE
*/
public class fortuneScreen extends javax.swing.JFrame {
public fortuneScreen() {
initComponents();
}
private void initComponents() {
randomJButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
fortuneTextArea = new javax.swing.JTextArea();
exitJButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
randomJButton.setText("Press for a Random Fortune");
randomJButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
randomJButtonActionPerformed(evt);
}
});
fortuneTextArea.setColumns(50);
fortuneTextArea.setRows(10);
jScrollPane1.setViewportView(fortuneTextArea);
exitJButton.setText("Exit");
exitJButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitJButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGap(
76,76,76).addComponent(randomJButton).addGap(
88,88,88).addComponent(exitJButton)).addGroup(
layout.createSequentialGroup().addContainerGap().
addComponent(
jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE,
480,
Short.MAX_VALUE))).addContainerGap()));
layout.setVerticalGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGap(
39, 39, 39).addComponent(
jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
31, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
41,41, 41).addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE).
addComponent(randomJButton).addComponent(exitJButton)).
addContainerGap(21,Short.MAX_VALUE)));
pack();
}
private void exitJButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void randomJButtonActionPerformed(java.awt.event.ActionEvent evt) {
Random generator = new Random();
int randomIndex = generator.nextInt(18);
String fortuneDisplay = "Fortune number: + String.valueOf(randomIndex)" +
" " + fortunes.get(randomIndex);
fortuneTextArea.setText(fortuneDisplay);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new fortuneScreen().setVisible(true);
}
});
}
private javax.swing.JButton exitJButton;
private javax.swing.JTextArea fortuneTextArea;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton randomJButton;
//private static ArrayList<String> fortunes = new ArrayList();
private static ArrayList<String> fortunes = new ArrayList<String>();
{
fortunes.add("I Like food");
fortunes.add("I think the bootstrap loader has " +
"your leg.");
fortunes.add("i didn't sell my soul i just lease it" +
" out on a monthly " +
"basis");
fortunes.add("If a cow and a half and a calf and a half " +
"can eat a bale " +
"and a half of hay in a day and a half, " +
"how many " +
"waffles does " +
"it take to shingle a dog house?");
fortunes.add("The difference between a Miracle and a Fact " +
"is exactly the " +
"difference between a mermaid and a seal.");
fortunes.add("Many a writer seems to think he is never " +
"profound except when" +
" he can't understand his own meaning.");
fortunes.add("Beware of a tall blond man with one " +
"black shoe.");
fortunes.add("You will be attacked next Wednesday at " +
"3:15 p.m. by six " +
"samurai sword wielding purple fish glued to " +
"Harley-Davidson" +
" motorcycles.");
fortunes.add("You have many friends and very few living " +
"enemies.");
fortunes.add("You are magnetic in your bearing.");
fortunes.add("Harp not on that string.");
fortunes.add("You could live a better life, if you had a " +
"better mind " +
"and a better body.");
fortunes.add("You will be advanced socially, without any " +
"special effort" +
" on your part.");
fortunes.add("You may be gone tomorrow, but that doesn't " +
"mean that you" +
" weren't here today.");
fortunes.add("Wrinkles should merely indicate where " +
"smiles have been.");
fortunes.add("A piano will fall near you at 2pm");
fortunes.add("Live long, and prosper");
fortunes.add("You will be assimilated");
fortunes.add("I am nothing");
}
}
Ok, Lew... I moved the creation of my list of fortunes out to where the
screen elements are created and I changed the listOfFortunes to "fortunes" so
that if I were to make it a different structure later on, the name won't be
misleading. And I cleaned up the indentation as best as I could. It seems
to be alot of work to make a program look nice for posting on usenet, but
that's ok... I don't mind the effort if I can gain some knowledge from it.
Once I moved the declaration out to where the rest of the private variables
are declared, Eclipse gave me a warning which I resolved by making the same
change as you pointed out
private static ArrayList<String> fortunes = new ArrayList<String>();
I know about programming to an interface, not an implementation but I don't
see where that comes into play in this program.
Further, I don't claim to know what Netbeans did when it created the
initComponents method. I guess the "beauty" of an IDE is that it takes care
of all that stuff for you. However, I am going to do some studying on it and
see if I can figure it out.
|
|
0
|
|
|
|
Reply
|
Pseudo
|
9/17/2007 5:31:59 PM
|
|
RedGrittyBrick wrote:
> Pseudo Silk Kimono wrote:
....
> You may need to select "Show" and change the indent policy to "spaces
> only". Make sure the indentation size is set to 4
For Usenet posting, I set the indentation to 2. It is just enough to
make the indentation visible.
Patricia
|
|
0
|
|
|
|
Reply
|
Patricia
|
9/17/2007 6:33:33 PM
|
|
Lew expounded:
>> Why do you declare the list static but initialize it in the
>> constructor? Wouldn't it be better to initialize the list
>> statically, or else declare it as an instance variable
Pseudo Silk Kimono wrote:
> Lew, perhaps I am wrong but is it not the case that my static
> ArrayList is only created once. I was going to put the creation in
> the method that picks and displays the random fortune, but I knew
> that since I only needed it to be created once, why have the JVM
> create it every time.
it is /created/ once, because it's static, but it is /filled/ with each
instantiation of the class, because you put all the add() calls in the
constructor instead of a static initializer.
This will only show up if you create more than one instance of the class in
the same JVM with the same ClassLoader.
>> Apropos of that collection, don't mix generics and raw types.
>>
>> The variable should be of the interface type, not the concrete
>> implementation.
> I don't understand this. I used Netbeans to do all the initial
> work and added the fortune picking piece in Eclipse once I had the
> layout working and the button doing something.
The IDE has nothing whatsoever at all to do with what I was saying.
You declared the variable as type ArrayList<String>. It should have been
declared as List<String>. You instantiated it as a raw ArrayList. You should
have instantiated it as an ArrayList<String>.
See Joshua Bloch's excellent book /Effective Java/ on preferring interfaces to
concrete implementations for variable types.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
9/18/2007 2:07:19 AM
|
|
On Tue, 18 Sep 2007 02:07:19 GMT, Lew expounded upon us in
Hi Lew,
New SSCCE posted:
Message-ID: <Xns99AE93E8CF202misplacedChildhood88@85.214.62.108>
|
|
0
|
|
|
|
Reply
|
Pseudo
|
9/18/2007 12:58:56 PM
|
|
|
9 Replies
304 Views
(page loaded in 0.096 seconds)
|