Board index » jbuilder » [structure] accessing other non static class

[structure] accessing other non static class


2004-04-01 03:08:46 PM
jbuilder2
I have some classes, UI_Main and UI_Panel1 UI_Panel2,
UI_Panel1 and 2 are TabbdPanel, when a button fires action in Panel1, i want
Main to switch
to Panel2 as current view. non of those classes are static, how could i get
access to Main, which
has TabbedPanel variable, and do tabbedPanel.setEnabled(1) in order to
switch to Panel2 ?
Thank you so much
raydog
 
 

Re:[structure] accessing other non static class

Raydog wrote:
Quote
I have some classes, UI_Main and UI_Panel1 UI_Panel2,
UI_Panel1 and 2 are TabbdPanel, when a button fires action in Panel1, i
want Main to switch
to Panel2 as current view. non of those classes are static, how could i
get access to Main, which
has TabbedPanel variable, and do tabbedPanel.setEnabled(1) in order to
switch to Panel2 ?

Thank you so much
raydog
Several ways to do this, including creating some kind of Action and
ActionMapping classes (preferred), but since you are a student, might be
overkill for you right now. :)
I asusme you have a JTabbedPane on your MainFrame. These Panels, which make
up the JTabbedPane are added in the MainFrame, probably in jbInit(). Are my
assumptions right so far?
If yes, then the best way to do this might be something like the following
I am supposing that you built this with JBuilder as an application. If so,
you will have a separate MainClass from the MainFrame. If this is not the
case, simply move the following items I comment on.
public class MainClass {
static MainFrame frame; //add this to your MainClass or MainFrame.
public MainClass() {
frame = new MainFrame();
/* All of the other methods would be here as normal */
//Last make the framer visible. Should have been added by JBuilder
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new MainClass();
}
//Add this method to the MainClass or the MainFrame. This will allow you to
//get back the MainFrame instance
public static MainFrame getMainFrame() {
return frame;
}
}
Now in your MainFrame class do the following:
public class MainFrame extends JFrame() {
TabPanel1 pnl1= new TabPanel1();
TabPanel2 pnl2= new TabPanel2();
JTabbedPane jtpMain = new JTabbedPane();
public MainFrame() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
//this would need to be added of course
jtpMain.add("Tabbed Pane 1",pnl1);
jtpMain.add("Tabbed Panel 2",pnl2);
/* All other code as normally done by JB */
}
//Add the following method in your MainFrame class
public JTabbedPane getMainTabbedPane() {
return jtpMain;
}
Now for the TabPanel1 class
public class TabPanel1 extends JPanel {
public TabPanel1() {
}
The ActionEvent for JButton1
void jButton1_actionPerformed(ActionEvent e) {
//switch to TabPanel2
MainClass.getMainFrame().getMainTabbedPane().setSelectedIndex(1);
}
That should do the trick for you!!
 

Re:[structure] accessing other non static class

Thanks !
i always like to ask more, the 'ActionMapping classes' you mentioned looks
very interesting to
me, would you give me a little hint on that, or a website ?
Thanks so much,
raydog
"pnichols" < XXXX@XXXXX.COM >wrote in message
Raydog wrote:
Quote
I have some classes, UI_Main and UI_Panel1 UI_Panel2,
UI_Panel1 and 2 are TabbdPanel, when a button fires action in Panel1, i
want Main to switch
to Panel2 as current view. non of those classes are static, how could i
get access to Main, which
has TabbedPanel variable, and do tabbedPanel.setEnabled(1) in order to
switch to Panel2 ?

Thank you so much
raydog
Several ways to do this, including creating some kind of Action and
ActionMapping classes (preferred), but since you are a student, might be
overkill for you right now. :)
I asusme you have a JTabbedPane on your MainFrame. These Panels, which make
up the JTabbedPane are added in the MainFrame, probably in jbInit(). Are my
assumptions right so far?
If yes, then the best way to do this might be something like the following
I am supposing that you built this with JBuilder as an application. If so,
you will have a separate MainClass from the MainFrame. If this is not the
case, simply move the following items I comment on.
public class MainClass {
static MainFrame frame; //add this to your MainClass or MainFrame.
public MainClass() {
frame = new MainFrame();
/* All of the other methods would be here as normal */
//Last make the framer visible. Should have been added by JBuilder
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new MainClass();
}
//Add this method to the MainClass or the MainFrame. This will allow you to
//get back the MainFrame instance
public static MainFrame getMainFrame() {
return frame;
}
}
Now in your MainFrame class do the following:
public class MainFrame extends JFrame() {
TabPanel1 pnl1= new TabPanel1();
TabPanel2 pnl2= new TabPanel2();
JTabbedPane jtpMain = new JTabbedPane();
public MainFrame() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
//this would need to be added of course
jtpMain.add("Tabbed Pane 1",pnl1);
jtpMain.add("Tabbed Panel 2",pnl2);
/* All other code as normally done by JB */
}
//Add the following method in your MainFrame class
public JTabbedPane getMainTabbedPane() {
return jtpMain;
}
Now for the TabPanel1 class
public class TabPanel1 extends JPanel {
public TabPanel1() {
}
The ActionEvent for JButton1
void jButton1_actionPerformed(ActionEvent e) {
//switch to TabPanel2
MainClass.getMainFrame().getMainTabbedPane().setSelectedIndex(1);
}
That should do the trick for you!!
 

{smallsort}

Re:[structure] accessing other non static class

Raydog wrote:
Quote
Thanks !
i always like to ask more, the 'ActionMapping classes' you mentioned looks
very interesting to
me, would you give me a little hint on that, or a website ?

Well you can roll your own, similar to the Struts ActionClasses and
ActionEvents, or yuo can use the internal Sun interfaces as a starting
point
java.sun.com/j2se/1.4.2/docs/api/javax/swing/Action.html