The Artima Developer Community
Sponsored Link

Java Answers Forum
My tabbed panes won't appear. :^(

4 replies on 1 page. Most recent reply: May 23, 2002 2:53 PM by Ben Trafford

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Ben Trafford

Posts: 7
Nickname: btrafford
Registered: May, 2002

My tabbed panes won't appear. :^( Posted: May 21, 2002 7:24 PM
Reply to this message Reply
Advertisement
Okay...

More fun "Ben learns Swing!" questions for y'all.

I have two files: we'll call them TestGUI and TestGUITabbedPanes.

TestGUI has a nice border layout, and everything shows up wonderfully, except for my tabbed panes.

TestGUI has some code that reads as follows:

    /** Initialize Pane */
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout);
    this.setSize(new Dimension(400, 300));
    this.setTitle("TestGUI");
    statusBar.setText("TestGUI Alpha");
    contentPane.add(toolBar, BorderLayout.NORTH);
    contentPane.add(mainScreen, BorderLayout.CENTER);
    contentPane.add(statusBar, BorderLayout.SOUTH);


Now, in the TestGUITabbedPanes class, I have all the code for the tabbed panes. The class is properly imported into TestGUI and instantiated (as
mainScreen
), and I get no compiler or runtime errors. But my tabbed panes won't show up!

ARG!

Help!


Tarba Dan

Posts: 11
Nickname: dtarba
Registered: May, 2002

Re: My tabbed panes won't appear. :^( Posted: May 22, 2002 1:42 AM
Reply to this message Reply
Please give us more details. You could also include the code for the TestGUITabbedPanes class.

Ben Trafford

Posts: 7
Nickname: btrafford
Registered: May, 2002

Re: My tabbed panes won't appear. :^( Posted: May 22, 2002 4:52 PM
Reply to this message Reply
> Please give us more details. You could also include
> the code for the TestGUITabbedPanes class.

Okeydokey. This'll be longish, so bear with me.

TestGUIMainFrame
The main frame of the application, where the tabbed panes should be loading.
The place where I've assumed the tabbed panes should be loading is in this file, where I've written:
contentPane.add(mainScreen, BorderLayout.CENTER);

package test;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import test.TestGUITabbedPanes;
 
public class testGUIMainFrame extends JFrame {
	JPanel contentPane;
	// JDesktopPane contentPane;
 
	/** Tabbed Pane */
  TestGUITabbedPanes mainScreen = new TestGUITabbedPanes();
 
	/** Menu Bar */
  JMenuBar menuBar = new JMenuBar();
 
  /** Tool Bar */
  /** Look for more icons for toolbar */
  JToolBar toolBar = new JToolBar();
  JButton openFileButton = new JButton();
  JButton closeFileButton = new JButton();
  JButton helpButton = new JButton();
  ImageIcon openFileButtonImage;
  ImageIcon closeFileButtonImage;
  ImageIcon helpButtonImage;
 
  /** Status Bar */
  JLabel statusBar = new JLabel();
 
  /** Border Layout */
  BorderLayout borderLayout = new BorderLayout();
 
 	/** Menus and Menu Items */
  /** File Menu */
  JMenu menuFile = new JMenu();
  	JMenuItem menuFileNew = new JMenuItem();
  	JMenuItem menuFileOpen = new JMenuItem();
  	JMenuItem menuFileClose = new JMenuItem();
  	JMenuItem menuFileSave = new JMenuItem();
  	JMenuItem menuFilePrint = new JMenuItem();
  	JMenuItem menuFileExit = new JMenuItem();
 
	/** Edit Menu */
	JMenu menuEdit = new JMenu();
		JMenuItem menuEditUndo = new JMenuItem();
		JMenuItem menuEditCut = new JMenuItem();
		JMenuItem menuEditCopy = new JMenuItem();
		JMenuItem menuEditPaste = new JMenuItem();
 
	/** Options Menu */
	JMenu menuOptions = new JMenu();
		JMenuItem menuOptionsSkins = new JMenuItem();
		JMenuItem menuOptionsWebUpdate = new JMenuItem();
		JMenuItem menuOptionsPreferences = new JMenuItem();
 
  /** Help Menu */
  JMenu menuHelp = new JMenu();
  	JMenuItem menuHelpIndex = new JMenuItem();
  	JMenuItem menuHelpContents = new JMenuItem();
  	JMenuItem menuHelpAbout = new JMenuItem();
 
  /** Construct the frame */
  public TestGUIMainFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /** Component initialization */
  private void jbInit() throws Exception  {
    /** Images */
    openFileButtonImage = new ImageIcon(test.TestGUIMainFrame.class.getResource("images/openFile.gif"));
    closeFileButtonImage = new ImageIcon(test.TestGUIMainFrame.class.getResource("images/closeFile.gif"));
    helpButtonImage = new ImageIcon(test.TestGUIMainFrame.class.getResource("images/help.gif"));
    setIconImage(Toolkit.getDefaultToolkit().createImage(test.TestGUIMainFrame.class.getResource("images/herosmall.jpg")));
 
    /** Initialize Pane */
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout);
    this.setSize(new Dimension(400, 300));
    this.setTitle("TestGUI");
    statusBar.setText("TestGUI Alpha");
    contentPane.add(toolBar, BorderLayout.NORTH);
    contentPane.add(mainScreen, BorderLayout.CENTER);
    contentPane.add(statusBar, BorderLayout.SOUTH);
 
    /** Initialize Menus with Action Listeners */
    /** File Menu */
    menuFile.setText("File");
			menuFileNew.setText("New");
			menuFileOpen.setText("Open");
			menuFileClose.setText("Close");
			menuFileSave.setText("Save");
			menuFilePrint.setText("Print");
			menuFile.addSeparator();
			menuFileExit.setText("Exit");
			menuFileExit.addActionListener(new ActionListener()  {
				public void actionPerformed(ActionEvent e) {
					menuFileExit_actionPerformed(e);
				}
			});
 
		/** Edit Menu */
		menuEdit.setText("Edit");
			menuEditUndo.setText("Undo");
			menuEditCut.setText("Cut");
			menuEditCopy.setText("Copy");
			menuEditPaste.setText("Paste");
 
		/** Options Menu */
		menuOptions.setText("Options");
			menuOptionsSkins.setText("Skins");
			menuOptionsWebUpdate.setText("Web Update");
			menuOptionsPreferences.setText("Preferences");
 
    /** Help Menu */
    menuHelp.setText("Help");
			menuHelpIndex.setText("Index");
			menuHelpContents.setText("Contents");
			menuHelpAbout.setText("About");
			menuHelpAbout.addActionListener(new ActionListener()  {
				public void actionPerformed(ActionEvent e) {
					menuHelpAbout_actionPerformed(e);
				}
			});
	/** End Initializing Menus */
 
    /** Icons */
    openFileButton.setIcon(openFileButtonImage);
    openFileButton.setToolTipText("Open File");
    closeFileButton.setIcon(closeFileButtonImage);
    closeFileButton.setToolTipText("Close And Exit");
    helpButton.setIcon(helpButtonImage);
    helpButton.setToolTipText("Help");
    toolBar.add(openFileButton);
    toolBar.add(closeFileButton);
    toolBar.add(helpButton);
 
    /** Add Menu Items */
    /** File Menu */
    menuFile.add(menuFileNew);
    menuFile.add(menuFileOpen);
    menuFile.add(menuFileClose);
    menuFile.add(menuFileSave);
    menuFile.add(menuFilePrint);
    menuFile.add(menuFileExit);
 
		/** Edit Menu */
		menuEdit.add(menuEditUndo);
		menuEdit.add(menuEditCut);
		menuEdit.add(menuEditCopy);
		menuEdit.add(menuEditPaste);
 
		/** Options Menu */
		menuOptions.add(menuOptionsSkins);
		menuOptions.add(menuOptionsWebUpdate);
		menuOptions.add(menuOptionsPreferences);
 
    /** Help Menu */
    menuHelp.add(menuHelpIndex);
    menuHelp.add(menuHelpContents);
    menuHelp.add(menuHelpAbout);
 
    /** Menu Bar */
    menuBar.add(menuFile);
    menuBar.add(menuEdit);
    menuBar.add(menuWizards);
    menuBar.add(menuTools);
    menuBar.add(menuOptions);
    menuBar.add(menuHelp);
    this.setJMenuBar(menuBar);
 
 
  	/** Quick Keys */
		menuFileNew.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_N, ActionEvent.CTRL_MASK));
 
		menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_O, ActionEvent.CTRL_MASK));
 
		menuFileSave.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_S, ActionEvent.CTRL_MASK));
 
		menuFilePrint.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_P, ActionEvent.CTRL_MASK));
 
		menuFileExit.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_X, ActionEvent.ALT_MASK));
 
		menuEditUndo.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
 
		menuEditCut.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_X, ActionEvent.CTRL_MASK));
 
		menuEditCopy.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_C, ActionEvent.CTRL_MASK));
 
	 	menuEditPaste.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_V, ActionEvent.CTRL_MASK));
 
		menuHelpContents.setAccelerator(KeyStroke.getKeyStroke(
						KeyEvent.VK_H, ActionEvent.CTRL_MASK));
  }
 
  /** Actions */
 
	/** File | New action performed */
 
	/** File | Open action performed */
 
	/** File | Close action performed */
 
	/** File | Save action performed */
 
 
	/** File | Print action performed */
 
  /** File | Exit action performed */
  public void menuFileExit_actionPerformed(ActionEvent e) {
    System.exit(0);
  }
 
  /** Help | About action performed */
 
  /** Overridden so we can exit when window is closed */
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      menuFileExit_actionPerformed(null);
    }
  }
}


TestGUITabbedPanes
This is the class where the tabbed panes are setup.
package test;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
class TestGUITabbedPanes	extends	JPanel
{
 
	JTabbedPane tabbedPane;
	JPanel panel1;
	JPanel panel2;
	JPanel panel3;
 
	public TestGUITabbedPanes()
	{
		// Create the tab pages
		createPage1();
		createPage2();
		createPage3();
 
		// Create a tabbed pane
		tabbedPane = new JTabbedPane();
		tabbedPane.addTab("Test Tab 1", panel1);
		tabbedPane.addTab("Test Tab 2", panel2);
		tabbedPane.addTab("Test Tab 3", panel3);
	}
 
	public void createPage1()
	{
		panel1 = new JPanel();
		panel1.setLayout(null);
 
		JLabel label1 = new JLabel("Username:");
		label1.setBounds( 10, 15, 150, 20 );
		panel1.add(label1);
 
		JTextField field = new JTextField();
		field.setBounds(10, 35, 150, 20);
		panel1.add(field);
 
		JLabel label2 = new JLabel( "Password:" );
		label2.setBounds( 10, 60, 150, 20 );
		panel1.add( label2 );
 
		JPasswordField fieldPass = new JPasswordField();
		fieldPass.setBounds( 10, 80, 150, 20 );
		panel1.add( fieldPass );
	}
 
	public void createPage2()
	{
		panel2 = new JPanel();
		panel2.setLayout( new BorderLayout() );
 
		panel2.add( new JButton( "North" ), BorderLayout.NORTH );
		panel2.add( new JButton( "South" ), BorderLayout.SOUTH );
		panel2.add( new JButton( "East" ), BorderLayout.EAST );
		panel2.add( new JButton( "West" ), BorderLayout.WEST );
		panel2.add( new JButton( "Center" ), BorderLayout.CENTER );
	}
 
	public void createPage3()
	{
		panel3 = new JPanel();
		panel3.setLayout( new GridLayout( 3, 2 ) );
 
		panel3.add( new JLabel( "Field 1:" ) );
		panel3.add( new TextArea() );
		panel3.add( new JLabel( "Field 2:" ) );
		panel3.add( new TextArea() );
		panel3.add( new JLabel( "Field 3:" ) );
		panel3.add( new TextArea() );
	}
 
}


Phew! Sorry for the spam, but there it is. Any help, again, would be greatly appreciated. I'm sure I'm just being stupid.

Tarba Dan

Posts: 11
Nickname: dtarba
Registered: May, 2002

Re: My tabbed panes won't appear. :^( Posted: May 23, 2002 2:26 AM
Reply to this message Reply
My suggestions are :

Don't resize the JFrame ( in your code this.setSize(...)), because the JFrame is resizing itself depending on the components that it contains.
For each component you add on the frame use the
setPrefferdSize() function (inherited from JComponent)
to resize it.

Use GridLayout or GridBagLayout instead of BorderLayout because you can arrange your components more easily.

I think that the component won't appear because it's size is too small.

If you follow my suggestions please tell me if it works
Sorry for my english.
Tarba Dan

Ben Trafford

Posts: 7
Nickname: btrafford
Registered: May, 2002

Re: My tabbed panes won't appear. :^( Posted: May 23, 2002 2:53 PM
Reply to this message Reply
> My suggestions are :
snip!
> Use GridLayout or GridBagLayout instead
> of BorderLayout because you can arrange your
> components more easily.

Good idea. Thanks!

> I think that the component won't appear because it's
> size is too small.

No, the default size is fine. When I use JTextArea in the same location, it appears. I think I'm instantiating the tabbed panes improperly. Arg.

> If you follow my suggestions please tell me if it
> works

It didn't solve the tabbed pane problem, but the GridBagLayout works much better, thanks!

> Sorry for my english.

Your English is fine! Wow. I should be so lucky to speak more than one language that well. All I can do is swear in French, like most Canadians. ;^)

Flat View: This topic has 4 replies on 1 page
Topic: How do you draw a rectangle with dotted or hashed lines? Previous Topic   Next Topic Topic: FTP question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use