Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: ElementCoder on August 15, 2012, 11:15:30 am

Title: Multiple objects
Post by: ElementCoder 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);
Title: Re: Multiple objects
Post by: Scipi 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
Title: Re: Multiple objects
Post by: ElementCoder 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>");


}
}
Title: Re: Multiple objects
Post by: Scipi 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.
Title: Re: Multiple objects
Post by: ElementCoder 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
Title: Re: Multiple objects
Post by: Scipi 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.