Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: JFrame with a J anel on the Right

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default JFrame with a J anel on the Right

    I am wanting to add a panel to the right of the JFrame, but it isn't showing up when I do this:

    		getFrame().getContentPane().add(new Hiscores(), "West");

    What am I doing wrong? Hiscores is a class that extend JPanel by the way.

    	private static void loadClient() throws Exception {
    		System.out.println("Loading client...");
    		RS2Loader loader = new RS2Loader();
    		Applet a = null;
    		try {
    			a = (Applet) loader.loadClass("client").newInstance();
    			a.setPreferredSize(new Dimension(765, 503));
    			a.setStub(loader);
    			a.init();
    			a.start();
    		} catch (Exception e) {
    			JOptionPane.showMessageDialog(
    					null,
    					"Error loading client - delete gamepack.jar in: "
    							+ System.getProperty("user.home")
    							+ ".jagex_cache_32 and reload this applet.");
    			e.printStackTrace();
    		}
     
    		final JTabbedPane pane = new JTabbedPane(JTabbedPane.TOP);
     
    		JComponent panelComponent = new JPanel(false);
    		panelComponent.setLayout(new GridLayout());
    		panelComponent.add(a);
     
    		pane.setOpaque(true);
    		pane.setForeground(new Color(64, 58, 58, 150));
     
    		JComponent panel1 = new JPanel(false);
    		panel1.setLayout(new GridLayout());
    		panel1.add(new Calculator());
     
    		JComponent panel2 = new JPanel(false);
    		panel2.setLayout(new GridLayout());
    		panel2.add(new Hiscores());
     
    		pane.addTab("Client", panelComponent);
    		pane.addTab("Combat Calc.", panel1);
    		pane.addTab("Hiscores", panel2);
    		pane.setFont(font);
     
    		getFrame().setIconImage(
    				new ImageIcon(Hiscores.class.getClassLoader().getResource(
    						"skill_icon/icon.png")).getImage());
    		getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		getFrame().getContentPane().add(new Hiscores(), "West");
     
    		getFrame().setContentPane(pane);
     
     
    		getFrame().setResizable(false);
    		getFrame().pack();
     
    		getFrame().setVisible(true);
     
    		Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(new Runnable() {
     
    			@Override
    			public void run() {
     
    				long milliseconds = System.currentTimeMillis() - START_TIME;
    				String time = (int) ((milliseconds / (1000*60*60*24)) % 7) + ":" + ((int) ((milliseconds / (1000 * 60 * 60)) % 24)) + ":" + ((int) ((milliseconds / (1000 * 60)) % 60)) + ":" + ((int) (milliseconds / 1000) % 60);
    				getFrame().setTitle("RuneScape 07 (" + time + ")");
    			}
    		}, 1, 1, TimeUnit.SECONDS);
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JFrame with a J anel on the Right

    Please make a small complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: JFrame with a J anel on the Right

    Quote Originally Posted by Norm View Post
    Please make a small complete program that compiles, executes and shows the problem.
    PM'ed it to you.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JFrame with a J anel on the Right

    Please post the code for a small complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: JFrame with a J anel on the Right

    package wbot.nl;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
     
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
     
    import wbot.nl.Hiscores.JTextFieldLimit;
     
    public class tEST {
     
    	private static JFrame frame = new JFrame();
     
    	public static void main(String... args) {
    		final JTabbedPane pane = new JTabbedPane(JTabbedPane.TOP);
     
    		JComponent panelComponent = new JPanel(false);
    		panelComponent.setLayout(new GridLayout());
    		//panelComponent.add(a);
     
    		pane.setOpaque(true);
    		pane.setForeground(new Color(64, 58, 58, 150));
     
    		JComponent panel1 = new JPanel(false);
    		panel1.setLayout(new GridLayout());
    		//panel1.add(new Calculator());
     
    		JComponent panel2 = new JPanel(false);
    		panel2.setLayout(new GridLayout());
    		//panel2.add(new Hiscores());
     
    		pane.addTab("Client", panelComponent);
    		pane.addTab("Combat Calc.", panel1);
    		pane.addTab("Hiscores", panel2);
     
    		getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		getFrame().getContentPane().setLayout(new BorderLayout()); 
    		getFrame().getContentPane().add(new Thing(), BorderLayout.EAST);
     
    		getFrame().setContentPane(pane);
     
    		getFrame().pack();
     
    		getFrame().setVisible(true);
     
    	}
     
    	/**
    	 * @return the frame
    	 */
    	public static JFrame getFrame() {
    		return frame;
    	}
     
    	/**
    	 * @param frame the frame to set
    	 */
    	public void setFrame(JFrame frame) {
    		this.frame = frame;
    	}
     
    }
    package wbot.nl;
     
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
     
    public class Thing extends JPanel {
     
    	private JTextField nameField;
     
    	public Thing() {
    		setBounds(100, 100, 422, 523);
    		this.setBorder(new EmptyBorder(5, 5, 5, 5));
    		this.setLayout(null);
     
    		JLabel lblNewLabel = new JLabel("Hiscores for:");
    		lblNewLabel.setBounds(54, 16, 79, 16);
    		this.add(lblNewLabel);
     
    		nameField = new JTextField();
    		nameField.setText("No Allies");
    		nameField.setBounds(151, 10, 114, 28);
    		this.add(nameField);
    		nameField.setColumns(10);
    	}
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JFrame with a J anel on the Right

    What is supposed to happen when the code is executed? I get a small frame in upper left with 3 tabs? jammed into a vertical alignment. Where is the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: JFrame with a J anel on the Right

    Quote Originally Posted by Norm View Post
    What is supposed to happen when the code is executed? I get a small frame in upper left with 3 tabs? jammed into a vertical alignment. Where is the problem?
    There's supposed to be a panel on the EAST.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JFrame with a J anel on the Right

    Try doing some debugging. Remove/comment out adding the tabbed pane and only add the Thing object.
    Change the values of the sizes and locations used to position the components until the Thing object and its contents are displayed. Doing your own layout will be a problem using setBounds etc.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: JFrame with a J anel on the Right



    --- Update ---

    I've switched to a boxlayout but there is a random spacing between the two:



    		JPanel panel = new JPanel();
    		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    		Hiscores hiscores = new Hiscores();
     
    		panel.add(a);
    		panel.add(hiscores);

Similar Threads

  1. problem with my JFrame; JFrame not closing and stay in background
    By golominator in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2012, 08:20 AM
  2. I'm new to JFrame and need some help
    By Toldry in forum What's Wrong With My Code?
    Replies: 25
    Last Post: August 27th, 2012, 11:19 AM
  3. using objects from one jFrame on a different jFrame
    By Taskeen in forum AWT / Java Swing
    Replies: 3
    Last Post: September 14th, 2011, 08:51 AM
  4. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 11:24 AM
  5. JFrame
    By M3ss1ah in forum AWT / Java Swing
    Replies: 3
    Last Post: February 23rd, 2011, 05:00 PM