Help - Search - Members - Calendar
Full Version: casting in java
4peeps.com Forums > General Hardware/Software > Programmers Corner
Mephist
i have a class in java that extends a ToggleButton, so that I can add the class as a ToggleButton to a form's toolbar while being able to associate the button to it's window by just calling class.associatedWindow.

Now when I want to refer back to a togglebutton that has already been added to the toolbar, i can get the button back as a Component... now, how do i cast the Component back into my custom made class so that I can access all it's methods etc.?

for example:

public class inputWindowButton extends javax.swing.JToggleButton {

private inputWindow myInputWindow;

public inputWindowButton(inputWindow associatedInputWindow) {
myInputWindow = associatedInputWindow;
this.setText(myInputWindow.getWindowName());
}

public void focusWindow() { myInputWindow.focusWindow(); }

public void minimiseWindow() { myInputWindow.minimiseWindow(); }
}

then:

so once i add one of these to my JToolBar, then try to refer back to it, i have to do something along the lines of:

Component toolBarComps[] = aJToolBar.getComponents();

which returns an array of the components on the toolbar. so how do i cast toolBarComps[x] back into an inputWindowButton? icon_eek.gif
Mephist
ummm... stupid question... nevermind, found out how.. simple simple simple... sigh.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.