Author Topic: Multiple objects  (Read 5073 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Multiple objects
« on: August 15, 2012, 11:15:30 am »
I'm working on a project with a lot of tabs and was wondering if is there a faster way then this (I'm pretty sure there is):
Code: [Select]
JTabbedPane tabPane = new JTabbedPane();

tabPane.add(panel1);
tabPane.add(panel2);
.....
tabPane.add(panelX);

Some people need a high five in the face... with a chair.
~EC

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Multiple objects
« Reply #1 on: August 15, 2012, 11:27:34 am »
You could make a loader method that takes an array of objects and adds each to a panel sequentially

Code: [Select]
public void loader(JTabbedPane tabPane, Component[] comp){
    for(int i = 0; i < comp.length; i++){
        if(comp[i] != null && tabPane != null)
            tabPane.add(comp[i]);
    }
}

And that should work for you :D
« Last Edit: August 15, 2012, 11:27:47 am by HOMER-16 »

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Multiple objects
« Reply #2 on: August 16, 2012, 11:32:18 am »
Thanks the code will be a lot more readable like this. I've modified it a bit to be like this:

Code: [Select]
public void loader(JTabbedPane tabPane, Component[] comp, String[] names){
    for(int i = 0; i < comp.length; i++){
        if(comp[i] != null && tabPane != null)
            tabPane.add(comp[i], names[i]);
    }
}
But I ran into a problem. It only adds the final tab, all the others won't show up.
Here is the entire java file if it would help:
Code: [Select]
package launch;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.jdesktop.swingx.JXEditorPane;

@SuppressWarnings("serial")
public class InfoPanel extends MainScreen{
//General section
static JPanel shipTab, planetsTab, forumTab, ircTab, wikiTab, mtGeneralTab = new JPanel();
static JPanel[] generalTabs = {shipTab, planetsTab, forumTab, ircTab, wikiTab, mtGeneralTab};
static String[] generalNames = {"Ship", "Planets", "Forum", "IRC channel", "Wiki", "MotionTwin"};
//MotionTwin section
static JPanel mtTab = new JPanel(new BorderLayout());
static JPanel twinoidTab = new JPanel();

static JXEditorPane mtLabel = new JXEditorPane();

/**
* This method will quickly add all components to the JTabbedPane specified.
* @author ElementCoder (with help from HOMER-16)
* @param tabPane - the JTabbedPane to which the tabs will be added
* @param comp - array of panels to add
* @param names - names for the tabs
* @since Alpha
*/
public static void createTabs(JTabbedPane tabPane, Component[] comp, String[] names){
for (int i = 0; i < comp.length; i++){
if (comp[i] != null && tabPane != null){
tabPane.add(comp[i], names[i]);
}
}
}

/**
* Change the tabs to display the info associated with the general button.
* @author ElementCoder
* @param none
* @since Alpha
*/
public static void setInfoPanelGeneral(){
tabPane.removeAll();
tabPane.addTab("Galaxy55 Application", infoTab);
infoTab.add(introLabel, BorderLayout.CENTER);
introLabel.setLineWrap(true);
introLabel.setWrapStyleWord(true);
introLabel.setBackground(null);
introLabel.setEditable(false);
createTabs(tabPane, generalTabs, generalNames);
}
/**
* Change the tabs to display the info associated with the twinoid button.
* @author ElementCoder
* @param none
* @since Alpha
*/
public static void setInfoPanelMT(){
tabPane.removeAll();
tabPane.add("MotionTwin", mtTab);
mtTab.add(mtLabel);
mtLabel.setBackground(null);
mtLabel.setEditable(false);
mtLabel.setContentType("text/html");

mtLabel.setText("Besides Galaxy55, MotionTwin has other services too. The main service connecting all of their " +
"games is Twinoid. On the next tab you will find a link to visit twinoid." +
" Below you can find some other services/games provided by MotionTwin." +
"<ul type=\"square\">" +
"<li>KadoKado</li>" +
"</ul>");


}
}

Some people need a high five in the face... with a chair.
~EC

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Multiple objects
« Reply #3 on: August 17, 2012, 06:05:11 am »
You are only creating a single object is why.

In java when you create an object, say,

Code: [Select]
Object o;
All it does is create a reference for that object, but the actual reference is null.

When you do,

Code: [Select]
o = new Object();
This creates the actual object

Code: [Select]
Object o;//null value
o = new Object;//Object is created

So what you need to do is instead of,

Code: [Select]
static JPanel shipTab, planetsTab, forumTab, ircTab, wikiTab, mtGeneralTab = new JPanel();
Try,

Code: [Select]
static JPanel shipTab = new JPanel();
static JPanel planetsTab = new JPanel();
static JPanel forumTab = new JPanel();
static JPanel ircTab = new JPanel();
static JPanel wikiTab = new JPanel();
static JPanel mtGeneralTab = new JPanel();

You also don't need to have each static since your array is static ;)

In fact, do you need static at all if each is being added to the JTabbedPane?

Public may be a better option.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Multiple objects
« Reply #4 on: August 17, 2012, 08:31:23 am »
Note to self:
doing
Code: [Select]
static JPanel shipTab, planetsTab, forumTab, ircTab, wikiTab, mtGeneralTab = new JPanel();does NOT create an object for everyone of them.

Well since I need to repeat this process lots of times, I'm currently using the following which saves me a lot of typing when using large amounts of tabs ;)
Code: [Select]
static JPanel[] generalTabs = {shipTab, planetsTab, forumTab, ircTab, wikiTab, mtGeneralTab};
for (int j =0; j < comp.length; j++){
generalTabs[j] = new JPanel();
}

As for the static. I really don't know.
It's done like this:
Code: [Select]
MainScreen: --Creates frame to hold everything as well as the initial layout
      private class ActionHandler for button presses.
      if the event is from the general button, InfoPanel.setInfoPanelGeneral() is called.
This keeps giving me an error "cannot make a static reference to the non-static method setInfoPanelGeneral()". Subsequently, when I make setInfoPanelGeneral static, it makes me make everything else static too. It can very well be that I'm just not writing my code correctly. It works now, but as I haven't made that much progress yet, I am considering rewriting it.

Code is available should you wish
« Last Edit: August 17, 2012, 08:32:17 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Multiple objects
« Reply #5 on: August 17, 2012, 12:32:38 pm »
Quote
his keeps giving me an error "cannot make a static reference to the non-static method setInfoPanelGeneral()". Subsequently, when I make setInfoPanelGeneral static, it makes me make everything else static too. It can very well be that I'm just not writing my code correctly. It works now, but as I haven't made that much progress yet, I am considering rewriting it.

Ah, ok I see

Code: [Select]
static JPanel[] generalTabs = {shipTab, planetsTab, forumTab, ircTab, wikiTab, mtGeneralTab};
 for (int j =0; j < comp.length; j++){
  generalTabs[j] = new JPanel();
 }

Pretty smart, though I would use generalTabs.length instead to ensure each object is initialized.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...