Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!

Mike Croteau

Greenhorn
+ Follow
since Apr 09, 2007
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 Mike Croteau

I understand how to use File as well as File[] to retrieve file information on my PC given a specified directory. However, I would like to know if there was a way to find a file which I have located within a web-application which could be installed on many different directories. I am using tomcat5 and could use the same logic as I did previously, but tomcat5 could lie on a much different directory if I installed my web application else where. Is there a work around? I apologize if this post is convoluted...

Any information on this would be most helpful. Thank you in advance!!!

Sincerely,
Mike
14 years ago
Thanks for the post! This is helpful, however, have another element to throw into the pot. What if I needed to do a partial search... example:

Let say you have a list of animals:

Cat
Dog
Bat

And you search for animals containing "at" within the name. You need to return: Cat, and Bat. How can i do this? Thanks again!

Mike
15 years ago
I have created an address book which holds information including first name, last name, address, city, etc. I have text fields for each of those values in which I use to set. How can I use those text fields to type in part of an address to find an address that matches within my list collection? Thanks in advance.
15 years ago
Not sure whats going on here... my code is compiling fine, however, when run, I keep getting this AWT Exception.

at finalProject.address.AddressMain.takeAction(AddressMain.java:40)
at finalProject.address.Gui.addToAddressBook(Gui.java:70)
at finalProject.address.Gui.actionPerformed(Gui.java:47)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


Here is the code its breaking on... (heads up, I know some of this is redundant. Part of this assignment is to utilize class files that were given to me.)

My Gui's add button gets pressed:

private void addToAddressBook() {
new Thread();
System.out.println("add");
int action = ACTION_ADD;
AddressRecord addrss = new AddressRecord(firstName.getText(),
lastName.getText(),address.getText(),city.getText(),
state.getText(), zip.getText(),phoneNumber.getText(),
emailAddress.getText());
try {
AddressMain.takeAction(ACTION_ADD, addrss);
} catch (DuplicateException e1) {
e1.printStackTrace();
}
}

which gets passed to AddressMain.takeAction...

public static void takeAction(int action, AddressRecord a) throws DuplicateException {

System.out.println("action = " + action);

switch(action) {
case ACTION_ADD:
System.out.println("in ACTION_ADD case:");
System.out.println("action = " + action);
System.out.println("address = " + a);
addressBook.takeAction(action, a);
break;
case ACTION_REMOVE:
addressBook.takeAction(action, a);
gui.updateDisplay(addressBook.toString());
break;
case ACTION_FIND:
addressBook.takeAction(action, a);
gui.updateDisplay(addressBook.foundAddressesToString());
break;
case ACTION_DISPLAY:
gui.updateDisplay(addressBook.toString() );
break;
} //switch
} //takeAction


which then calls takeAction in my AddressBook... yes I know.

public void takeAction(int action, AddressRecord a) {
System.out.println("in AddressBook");
//System.out.println("address = " + a);
System.out.println("action = " + action);
switch(action){
case ACTION_ADD:
addToList(a);
break;
case ACTION_FIND:
findAddress(a);
break;
case ACTION_DISPLAY:
displayAddresses();
break;
case ACTION_REMOVE:
removeAddress(a);
break;
}

When running it, I keep getting the Exception when I press the add button. Im not even getting to the System.out within the takeAction in AddressBook. I am lost here and out of clues. Thanks in advance.

Mike
15 years ago
I am using the Reflection API, trying to obtain class information. Displaying in an applet, when using the .getDeclaredMethods or .getDeclaredFields, I come across a Security Exception error. How do I get around this? Thanks in advance!


Here is sample code which I think is throwing the Exception:

public String getPublicMethods(String a) throws ClassNotFoundException{

this.initializeVariable(a);
sb.append(PB + MTD);
sb.append(HEADER);
Method[] theMethods = clas.getDeclaredMethods();
for (int i = 0; i < theMethods.length; i++) {
mod = theMethods[i].getModifiers();
Modifier modifier = new Modifier();
if(modifier.isPublic(mod)) {
methodName = theMethods[i].getName();
returnType = theMethods[i].getReturnType().getName();
sb.append(" " + returnType + " " + methodName + " (");

Class[] parameterTypes = theMethods[i].getParameterTypes();
boolean first = true;
for (int k = 0; k < parameterTypes.length; k ++) {
if(first) {
sb.append(parameterTypes[k].getName());
first = false;
} else {
sb.append(", " +parameterTypes[k].getName());
}
}
sb.append(" )\n");
}
}
15 years ago
I keep having the same problems with my gridbag layout when trying to use JTextArea. Im either creating a faulty layout or Im not properly adding a JTextArea to my GridBagLayout. I pasted my code along with this post... can anyone give some incite to what I may be doing wrong or missing when trying to implement this? Thanks in advance!!

*******
CODE:

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ClassBrowserGui extends JFrame implements ActionListener{

JRadioButton privateRadio = new JRadioButton("Private");
JRadioButton protectedRadio = new JRadioButton("Protect");
JRadioButton publicRadio = new JRadioButton("Public");
JButton methodButton = new JButton("Methods");
JButton fieldButton = new JButton("Fields");
JButton interfaceButton = new JButton("Interfaces");
JButton classModButton = new JButton("Class Modifiers");
JButton constructorButton = new JButton("Constructors");
JLabel entClassLabel = new JLabel("Enter Class");
JTextField entClass = new JTextField(10);
JTextArea printout = new JTextArea(40, 40);
JPanel output = new JPanel();

private GridBagLayout layout;
private GridBagConstraints constraints;


public ClassBrowserGui(){
super("Class Browser");
layout = new GridBagLayout();
setLayout(layout);
//output.setLayout(layout);//setting frame layout
//output.setVisible(true);
//output.add(GridBagConstraints);
constraints = new GridBagConstraints();
//constraints.fill = GridBagConstraints.HORIZONTAL;
//JButton[] buttons = {methodButton, fieldButton, classModButton, interfaceButton, constructorButton};
addComponentsToGui();
addActionListeners();
setComponentsVisible();
}

private void setComponentsVisible() {
//buttons
methodButton.setVisible(true);
interfaceButton.setVisible(true);
fieldButton.setVisible(true);
constructorButton.setVisible(true);
classModButton.setVisible(true);
//text
printout.setVisible(true);
entClass.setVisible(true);

}

// private void addLabelTextRows(JLabel[] labels,
// JTextField[] textFields,
// GridBagLayout gridbag,
// Container container) {
//GridBagConstraints c = new GridBagConstraints();
//c.anchor = GridBagConstraints.EAST;
//int numLabels = labels.length;
//
//for (int i = 0; i < numLabels; i++) {
//c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last
//c.fill = GridBagConstraints.NONE; //reset to default
//c.weightx = 0.0; //reset to default
//container.add(labels[i], c);
//
//c.gridwidth = GridBagConstraints.REMAINDER; //end row
//c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 1.0;
//container.add(textFields[i], c);
//}
// }

private void addComponentsToGui() {
addComponent(entClassLabel, 0, 1, 1, 1);
addComponent(entClass, 1, 1, 1, 1 );
addComponent(methodButton, 1, 0, 1, 1);
addComponent(fieldButton, 2, 0, 1, 1);
addComponent(interfaceButton, 3, 0, 1, 1);
addComponent(constructorButton, 4, 0, 1, 1);
addComponent(classModButton, 5, 0, 1, 1);
addComponent(printout, 2, 1, 1, 1);

printout.getScrollableTracksViewportHeight();
printout.getScrollableTracksViewportWidth();
}

private void addActionListeners(){
methodButton.addActionListener(this);
}

private void addComponent(Component component, int row, int column, int width, int height) {

constraints.gridy = row;//set gridy
constraints.gridx = column;//set gridx
constraints.gridwidth = width;//set width
constraints.gridheight = height; //set height
//constraints.weightx = weightx;
//constraints.weighty = weighty;
//constraints.ipadx = ipadx;
//constraints.ipady = ipady;
layout.setConstraints(component, constraints);//set constraints
add(component);//add component
}

public void actionPerformed(ActionEvent e) {

if(e.getSource()== methodButton){
String a = "java.awt.GridBagConstraints";
ClassBrowser classBrowser = new ClassBrowser(a);
printout.setText(classBrowser.getPublicMethods(a));
}
}

}
15 years ago