BillR

Greenhorn
+ Follow
since Apr 11, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by BillR

Should be easy, how do you round decimal places to the least significant digit when calculating using doubles? Or at least limit the decimal places while suppressing trailing zeros?
23 years ago
When you initialzie fields in a class when you declare them, what actually hapens and when?
This compiles, but does not run, no matter it seems, what line or order you initialize 'insets' in the init method. See error message right below the code. I put a second example of *almost* the same code. This difference is that 'theInsets' is initialized with an initializer when declared. What is agrevating, is that this not only compiles but actually runs! Why? What is going on that we don't see? Anybody?
</pre>
<pre>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyInsetsButton extends JApplet
{
JButton theButton;
Container theContainer;
Insets theInsets;
//= new Insets(1,1,1,1);

public void init()
{
theContainer = this.getContentPane();
theInsets = new Insets(1,1,1,1);
//Button
theButton = new JButton("Press Me");
theButton.addActionListener(new Switcher());
theContainer.add(theButton);
}

public Insets getInsets()
{
return theInsets;
}

public class Switcher implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
theInsets = new Insets(theInsets.top + 2, theInsets.left + 2,
theInsets.bottom + 2, theInsets.right + 2);
invalidate();
validate();
}
}
}
//the end

Error Messages:
java.lang.NullPointerException:
at java.awt.BorderLayout.layoutContainer(BorderLayout.java:608)
at java.awt.Container.layout(Container.java:494)
at java.awt.Container.doLayout(Container.java:484)
at java.awt.Container.validateTree(Container.java:553)
at java.awt.Container.validateTree(Container.java:560)
at java.awt.Container.validate(Container.java:535)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:488)
at sun.applet.AppletPanel.run(AppletPanel.java:282)
at java.lang.Thread.run(Thread.java:479)
java.lang.NullPointerException:
at java.awt.BorderLayout.layoutContainer(BorderLayout.java:608)
at java.awt.Container.layout(Container.java:494)
at java.awt.Container.doLayout(Container.java:484)
at java.awt.Container.validateTree(Container.java:553)
at java.awt.Container.validateTree(Container.java:560)
at java.awt.Container.validateTree(Container.java:560)
at java.awt.Container.validate(Container.java:535)
at sun.applet.AppletViewer$1$AppletEventListener.appletStateChanged(AppletViewer.java:226)
at sun.applet.AppletPanel.dispatchAppletEvent(AppletPanel.java:229)
at sun.applet.AppletPanel.appletResize(AppletPanel.java:616)
at java.applet.Applet.resize(Applet.java:149)
at java.applet.Applet.resize(Applet.java:160)
at sun.applet.AppletPanel.run(AppletPanel.java:331)
at java.lang.Thread.run(Thread.java:479)
Process Exit...
</pre>
For example this fairly simple example compiles and runs fine:

<pre>
public class MyInsetsButton extends JApplet
{
JButton theButton;
Container theContainer;
Insets theInsets = new Insets(1,1,1,1);

public void init()
{
theContainer = this.getContentPane();

//Button
theButton = new JButton("Press Me");
theButton.addActionListener(new Switcher());
//Container
}
public Insets getInsets()
{
return theInsets;
}
public class Switcher implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
theInsets = new Insets(theInsets.top + 2, theInsets.left + 2,
theInsets.bottom + 2, theInsets.right + 2);
invalidate();
validate();
}
}
}
//the end
</pre>

23 years ago
I use Kawa as well. I am also unsure of what is happening when using the 'package' keyword. I was thinking it was something I was doing wrong (probably is). I wonder if there is a chance of the cowgirl et al to put something up on the ranch about how to set up packages and how to store the files (directory structure etc.) when we have programmed them. ???
23 years ago
In order to figure it out, I rearranged it a bit and added a little here and there. Here is 'a' solution... I am pretty new at this Java thang too! I am sure there are many other solutions that will work as well. Take a look and see if you can figure out what I did.
<PRE>
import java
.awt.*;
class Gb_lay2
{
public static void main(String arr[])
{
Frame f = new Frame();
Panel p = new Panel();
f.add(p);
GridBagLayout g = new GridBagLayout();
p.setLayout(g);
p.setBackground(Color.lightGray);

List listBox = new List(3 , true);
listBox.add("apple");
listBox.add("orange");
listBox.add("abcdef");
listBox.add("cbsims");
listBox.add("monica");
listBox.add("Becky");

String str = "The quick brown fox";
Label lab = new Label(str , Label.LEFT);
lab.setBackground(Color.white);

GridBagConstraints gbc = new GridBagConstraints();
//GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0; gbc.gridy = 0 ;
gbc.gridwidth = 1; gbc.gridheight = 1 ;
gbc.weightx = 1.0F ; gbc.weighty = 0.05F;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = gbc.NORTH;
p.add(listBox , gbc);

gbc.gridx = 0; gbc.gridy = 1;
gbc.gridwidth = 1 ; gbc.gridheight = 1 ;
gbc.weightx = 1.0F; gbc.weighty = 1.0F;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = gbc.NORTH;
p.add(lab , gbc);

f.setSize(100,200);
f.show();
}
}
</pre>
[This message has been edited by BillR (edited April 14, 2000).]
23 years ago
First the question, then the reason I am asking ... in case you want to know!
In the update method of the observer interface, it will tell you the source of the observerable object.
public void update(Observable source, Object state)
Can someone tell me what exactly the 'source' is telling me? For example, what do I need to do to differentiate between different sources if there is more than one observable object and the observer only wants to handle observable events from particular ones. I know a selection statement (if, switch) can be used, but I don't really understand the details of what to compare to what. I know how to use if and switch very well... I used to do 'C' ;-). For example what is it the 'source' is telling me? (the objects name/reference? it's class?) And what do I need to do to compare it against something else. For example, I only want the observer to react to some types of objects, but not others.
Thanks BillR

Rational:
As a self imposed exercise, I am trying to consolodate what I have taught myself so far by writing a calculator application. I think I have figured out how to grab key press events to trigger specific calculator keys (subclassed JButtons which have their own little messages to send when fired)... even when they are not in focus by 'listening' with a higher level container (like a JPanel) and setting up an observerable interface so that the different keys can listen and tell if they should do their thing. The keys themselves have observer classes to listen and observable interfaces to let the program 'know' that the calculator key is talking to it... or trying to. I am doing this rather than sending an explicit message to the other object specifically in order to keep the calculator keys generalized and thus re-useable without having to figure out which lines of code to change. Maybe even make it in to a generalized keypad component for re-useablity... what a concept this OOP! :-)
If you read this far thanks... hope you can help me out.

[This message has been edited by BillR (edited April 11, 2000).]
[This message has been edited by BillR (edited April 11, 2000).]
[This message has been edited by BillR (edited April 19, 2000).]
23 years ago
Same place you download the JDK from.
23 years ago
You have to call the applet using the object tag. To make it easy Sun has produced an HTML editor. Just code it using the 'applet' tag, and then run the HTML converter on it. It will put it in a format that can understand that it is supposed to run your applet. You can review the code change, and either write it manually next time, or do the same thing. Over and over and over and over...
------------------
Imagination is more important than knowledge... Albert Einstein.
[This message has been edited by BillR (edited April 11, 2000).]
23 years ago