Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2004

Tip: Looking for answers? Try searching our database.

newbie GUI question (tabs)

Thread view: 
zcraven - 01 Nov 2004 20:25 GMT
I want to make a window GUI that contains the following:

(top) - 3 tabs leading to League, Player, and Club menus (default is to
display League).
(underneath tabs) - a main frame with various nested panels.

I cannot get this code to do this.  It just displays the tabs, and the
league frame, but when I click the tabs it doesnt change at all.  Can anyone
point me in the right direction?

Thanks,

Zac

---------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUI implements ActionListener {

   JFrame frame;
   JPanel leaguemenu, clubmenu, playermenu;
   final static String LOOKANDFEEL = null;

    ///////////// CONSTRUCTOR /////////////
   public GUI() {  //static?
       //Set the look and feel.
       initLookAndFeel();
       //Make sure we have nice window decorations.
       JFrame.setDefaultLookAndFeelDecorated(true);
       // set up the main window
       frame = new JFrame("League Administrator (Zac Craven - 2004)");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(new Dimension(120, 40));

       Component tabs = createTabs();
       Component leaguemenu = leagueMenu();
       Component clubmenu = clubMenu();
       Component playermenu = playerMenu();

       frame.getContentPane().add(tabs, BorderLayout.NORTH);
       frame.getContentPane().add(leaguemenu, BorderLayout.CENTER);
       //  frame.getContentPane().add(clubmenu, BorderLayout.CENTER);
       frame.pack();
       frame.setVisible(true);
   }

   public Component leagueMenu(){
       JPanel league = new JPanel();
       Component buttons = createLeagueButtons();
       Component display = createDisplay();
       league.add(buttons, BorderLayout.EAST);
       league.add(display, BorderLayout.WEST);
       return league;
   }

   public Component clubMenu(){
       JPanel club = new JPanel();
       Component buttons = createClubButtons();
       Component display = createDisplay();
       club.add(buttons, BorderLayout.EAST);
       club.add(display, BorderLayout.WEST);
       return club;
   }

   public Component playerMenu(){
       JPanel player = new JPanel();
       player.setLayout(new GridLayout(0,1));
       Component buttons = createPlayerButtons();
       Component display = createDisplay();
       player.add(buttons, BorderLayout.EAST);
       player.add(display, BorderLayout.WEST);
       return player;
   }

   public Component createTabs(){
       JTabbedPane tabbedPane = new JTabbedPane();

       JComponent leaguemenu = makeTextPanel("the League Menu");
       tabbedPane.addTab("League Menu", leaguemenu);
       tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

       JComponent panel2 = makeTextPanel("da club");
       tabbedPane.addTab("Club Menu", clubmenu);
       tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

       JComponent panel3 = makeTextPanel("da player");
       tabbedPane.addTab("Player Menu", playermenu);
       tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

       return tabbedPane;
   }

   protected JComponent makeTextPanel(String text) {
       JPanel panel = new JPanel(false);
       JLabel filler = new JLabel(text);
       filler.setHorizontalAlignment(JLabel.CENTER);
       panel.setLayout(new GridLayout(1, 1));
       panel.add(filler);
       return panel;
   }

   public Component createLeagueButtons() {

       JPanel toolbar = new JPanel();
       toolbar.setLayout(new GridLayout(0,1));
       JButton createClubButton = new JButton("Create Club");
       toolbar.add(createClubButton);
       JButton addClubButton = new JButton("Add Club");
       toolbar.add(addClubButton);

       JPanel flow = new JPanel();
       flow.add(toolbar);
       return flow;
   }

   public Component createClubButtons() {
       JPanel toolbar = new JPanel();
       toolbar.setLayout(new GridLayout(0,1));
       JButton addPlayerButton = new JButton("Add Player");
       toolbar.add(addPlayerButton);
       JPanel flow = new JPanel();
       flow.add(toolbar);
       return flow;
   }

   public Component createPlayerButtons() {
       JPanel toolbar = new JPanel();
       toolbar.setLayout(new GridLayout(0,1));
       JButton addPlayerButton = new JButton("Add Player?");
       toolbar.add(addPlayerButton);
       JPanel flow = new JPanel();
       flow.add(toolbar);
       return flow;
   }

   public Component createDisplay() {

       //Create the display panel for the clubs in the league.
       JPanel displayPanel = new JPanel();

       //Add various widgets to the sub panels.
  //     addWidgets();

       //Create the main panel to contain the two sub panels.
       JPanel mainPanel = new JPanel();
       mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
       // Add border around the select panel

mainPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createT
itledBorder("Premiership"),
       BorderFactory.createEmptyBorder(5,5,5,5)));

       //Add display panels to the main panel.
       mainPanel.add(displayPanel);
       return mainPanel;
   }

   public void actionPerformed(ActionEvent e) {
    //   numClicks++;
      // label.setText(labelPrefix + numClicks);
   }

//dont worry about stuff below here //
   private static void initLookAndFeel() {
       String lookAndFeel = null;

       if (LOOKANDFEEL != null) {
           if (LOOKANDFEEL.equals("Metal")) {
               lookAndFeel =
UIManager.getCrossPlatformLookAndFeelClassName();
           } else if (LOOKANDFEEL.equals("System")) {
               lookAndFeel = UIManager.getSystemLookAndFeelClassName();
           } else if (LOOKANDFEEL.equals("Motif")) {
               lookAndFeel =
"com.sun.java.swing.plaf.motif.MotifLookAndFeel";
           } else if (LOOKANDFEEL.equals("GTK+")) {
               lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
           } else {
               System.err.println("Unexpected value of LOOKANDFEEL
specified: "
                                  + LOOKANDFEEL);
               lookAndFeel =
UIManager.getCrossPlatformLookAndFeelClassName();
           }

           try {
               UIManager.setLookAndFeel(lookAndFeel);
           } catch (ClassNotFoundException e) {
               System.err.println("Couldn't find class for specified look
and feel:"
                                  + lookAndFeel);
               System.err.println("Did you include the L&F library in the
class path?");
               System.err.println("Using the default look and feel.");
           } catch (UnsupportedLookAndFeelException e) {
               System.err.println("Can't use the specified look and feel ("
                                  + lookAndFeel
                                  + ") on this platform.");
               System.err.println("Using the default look and feel.");
           } catch (Exception e) {
               System.err.println("Couldn't get specified look and feel ("
                                  + lookAndFeel
                                  + "), for some reason.");
               System.err.println("Using the default look and feel.");
               e.printStackTrace();
           }
       }
   }

   private static void createAndShowGUI() {
       //Make sure we have nice window decorations.
       JFrame.setDefaultLookAndFeelDecorated(true);
       GUI app = new GUI();
   }

   public static void main(String[] args) {
       //Schedule a job for the event-dispatching thread:
       //creating and showing this application's GUI.
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }
}
Florian Buchholz - 02 Nov 2004 00:05 GMT
Zac,

in your createTabs() method, instead of 'panel2' and 'panel3' you should
name the components 'clubmenu' and 'playermenu', or pass 'panel2' and
'panel3' as the parameters for the tab creation. Also you have global
JPanels declared with the same names. In the method here you are
overriding them with local JComponents. This doesn't cause the error but
I am not sure what you want to achieve with this. This is the reason why
the compiler didn't complain though: 'clubmenu' amd 'playermenu' exist
but are not initialized.

Hope this helps,

Florian

> I want to make a window GUI that contains the following:
>
[quoted text clipped - 9 lines]
>
> Zac
zcraven - 02 Nov 2004 00:46 GMT
excellent, that works better now  - thanks a lot!

however, it is not adding the other panels that i want to add (in the
createLeagueButtons method for example).  Any ideas?

Zac

> Zac,
>
[quoted text clipped - 24 lines]
> >
> > Zac
zcraven - 02 Nov 2004 01:14 GMT
by the way this is the url of the file:

www.dur.ac.uk/z.a.craven/GUI.txt

> excellent, that works better now  - thanks a lot!
>
[quoted text clipped - 32 lines]
> > >
> > > Zac
Florian Buchholz - 02 Nov 2004 08:11 GMT
Zac,

I'm not sure what exactly you want to do with the tabs. If you want the
"main frame" to display the various *Menu() contents based on the tab
selection, you should create three panels with the textPanel on top and
the Menu Panel below and then add those to the respective tab.

Your constructor would then look like this:

    public GUI() {
        initLookAndFeel();
        JFrame.setDefaultLookAndFeelDecorated(true);

        frame = new JFrame("League Administrator (Zac Craven - 2004)");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(120, 40));

        frame.getContentPane().add(createTabs());
        frame.pack();
        frame.setVisible(true);
    }

and the createTabs() methode as follows:

    public Component createTabs(){
        JTabbedPane tabbedPane = new JTabbedPane();

    JPanel league = new JPanel();
    league.setLayout(new BorderLayout());
    league.add(makeTextPanel("the League Menu"), BorderLayout.NORTH);
    league.add(leagueMenu(), BorderLayout.CENTER);
        tabbedPane.addTab("League Menu", league);
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    JPanel club = new JPanel();
    club.setLayout(new BorderLayout());
    club.add(makeTextPanel("da club"), BorderLayout.NORTH);
    club.add(clubMenu(), BorderLayout.CENTER);
        tabbedPane.addTab("Club Menu", club);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

    JPanel player = new JPanel();
    player.setLayout(new BorderLayout());
    player.add(makeTextPanel("da player"), BorderLayout.NORTH);
    player.add(playerMenu(), BorderLayout.CENTER);
        tabbedPane.addTab("Player Menu", player);
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

        return tabbedPane;
    }

Also, in your create*Buttons() methods I am not sure why you wrap the
created panel into the "flow" panel. It should work just fine returning
the initial "toolbar" panel.

As I said, I don't know if this is what you had in mind.

Florian

> excellent, that works better now  - thanks a lot!
>
> however, it is not adding the other panels that i want to add (in the
> createLeagueButtons method for example).  Any ideas?
>
> Zac
Stewart Gordon - 05 Nov 2004 16:42 GMT
Hello.  Welcome to newsgroups.

Newsreaders allow you to address a single message to more than one
'group simultaneously.  This is called crossposting, and is the only
appropriate method of sharing a message across two or more 'groups.

Please read
http://smjg.port5.com/faqs/usenet/xpost.html

Meanwhile, since you've multiposted this time, please pick one of the
'groups that you have multiposted to on which you wish the discussion to
continue.

Stewart.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.