Results 1 to 3 of 3
  1. #1
    byte237 is offline Member
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    5
    Rep Power
    0

    Default Components not showing

    Hi.
    I'm trying to add some components in several tabs, but they do not seem to be showing. I've tried to change around a lot of things and re-writing the whole thing, but I still can't get it to work.
    It needs to be said that I'm writing Java as an hobby so I've not studied it, so you'd propably see many flaws.

    Here's my sources:

    core.java
    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class core {
    	private JTabbedPane tab = new JTabbedPane();
    	private Component[] tabComponents = {new start(), new quests()};
    	private String[] tabCaptions = {"Start", "Quests"};
    	public static void main(String[] args) {
    		core Engine = new core();		
    	}
    	public core(){
    		JFrame window = new JFrame("Window");
    		for(int i = 0; i <= tabComponents.length - 1; i++) {
    			tab.addTab(tabCaptions[i], new JScrollPane(tabComponents[i]));
    		}
    		window.add(tab, BorderLayout.CENTER);		
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setSize(500, 300);
    		window.setVisible(true);
    	}
    	
    }
    start.java
    PHP Code:
    import javax.swing.*;
    
    public class start extends JComponent{	
    	public start() {
    		add(new JLabel("Start"));
    	}
    }
    PHP Code:
    import javax.swing.*;
    
    public class quests extends JComponent{
    	public quests() {
    		add(new JLabel("Quests"));
    	}
    }
    (I used the PHP BB tags for the colouring, just not to confuse anyone)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem may be one of Swing layouts. If I remember correctly, a JComponent uses a null layout as its default layout manager, meaning you are responsible for the location and position of every component you add to it. If you were using a JPanel on the other hand, it would be using a FlowLayout as default, and you'd see your JLabel. Another solution is to set the layout yourself:

    Java Code:
    // please note that class names should be capilatlized.
    class Quests extends JComponent {
      public Quests() {
        setLayout(new FlowLayout());
        add(new JLabel("Quests"));
      }
    }

  3. #3
    byte237 is offline Member
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your problem may be one of Swing layouts. If I remember correctly, a JComponent uses a null layout as its default layout manager, meaning you are responsible for the location and position of every component you add to it. If you were using a JPanel on the other hand, it would be using a FlowLayout as default, and you'd see your JLabel. Another solution is to set the layout yourself:

    Java Code:
    // please note that class names should be capilatlized.
    class Quests extends JComponent {
      public Quests() {
        setLayout(new FlowLayout());
        add(new JLabel("Quests"));
      }
    }
    Thank you very much! I really appreciate it.

Similar Threads

  1. Showing
    By bostonstate in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 07:49 PM
  2. Need Help showing text in JTextArea
    By GuyFawkes in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2008, 09:19 AM
  3. Showing how to add Actions for KeyStrokes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:17 PM
  4. Error Messages Not Showing Up
    By nvidia in forum Web Frameworks
    Replies: 0
    Last Post: 04-07-2008, 10:41 PM
  5. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-07-2007, 11:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •