Wednesday, April 1, 2009

Friday April 3: Java Swing Tabbed Browsing part Five

Hey chilluns, I just had this sudden thought: what if we wanted to have a list of websites we could goto, instead of laboriously having to type in an address over and over? There are many ways of accomplishing this, and I suppose I COULD have made a menu; but I chose another path, for three reasons:


A) It would require having to make a MenuBar, Menu and several menuitems... too much work


B) We can accomplish the same goal using what's known in Java as a "ComboBox", aka a "pulldown menu". Plus, if we connect this ComboBox to a Vector (remember that component? Told you we'd use 'em again), it might be possible to make the list expand to hold more websites


C) I'm too old, too stubborn and too big to be prevented from using a ComboBox.


So anyway, here's how I did it, perhaps some of you have another idea. I would love to see them :-)


Add this import for using Vectors
import java.util.*; //for use with vectors

add these to your declarations section:
Vector webURL; //to hold addresses
JComboBox myWebPlaces; //to display those addresses

Add these two components to your constructor:
webURL = new Vector( );
webURL.addElement("http://www.google.com");
webURL.addElement("http://www.thinkgeek.com");
webURL.addElement("http://www.yahoo.com");
webURL.addElement("http://www.foxnews.com");

myWebPlaces = new JComboBox(webURL);
myWebPlaces.setEditable(true);
myWebPlaces.setBounds(300,0,100,20);
myWebPlaces.addActionListener(this);
ButtonsAndText.add(myWebPlaces);

Add this “else if” to your actionPerformed
else if (e.getSource( ) == myWebPlaces)
{
System.out.println(myWebPlaces.getSelectedItem());
StartPage= myWebPlaces.getSelectedItem()+ "";
makeTab( ); //call method, make new tab with selected page
}//end using combobox

Now obviously, if you want to use a different set of websites, or a different number of them, go right on ahead. Let's see what's in your fridge!

code is good food,
Uncle Paulie

No comments:

Post a Comment