Hi all...

I have a JFrame, which contains a JTabbed Pane, which contains two JPanels.

The problem is that when I run the program, a completely grey frame shows up, and the contents only becomes visible when I click on the border of the frame.

Here is the code. Compile & run to see what I mean. I already tried repainting the contents of the frame in the main program, didn't work.

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;

public class test{
	
	JFrame mainFrame;
	JTabbedPane JTP;
	JPanel panel1, panel2;
	JSlider JS;	
	
	public test(){
		
		mainFrame = new JFrame();
		
		panel1 = new JPanel();
		panel1.setBackground(Color.black);
		panel1.setForeground(Color.white);
				
		JS = new JSlider();
		JS.setForeground(Color.white);
		JS.setMinimum(0);
		JS.setMaximum(100);
		JS.setMinorTickSpacing(5);
		JS.setMajorTickSpacing(10);
				
		JS.setPaintTicks(true);
		JS.setPaintLabels(true);
		
		panel2 = new JPanel();
		panel2.setBackground(Color.black);
		panel2.setForeground(Color.white);
		panel2.setLayout(new BorderLayout());
		panel2.add(JS, BorderLayout.CENTER);
				
		JTP = new JTabbedPane();
		JTP.add("Tab1", panel1);
		JTP.add("Tab2", panel2);
		JTP.setForeground(Color.green);
		mainFrame.getContentPane().add(JTP);
		mainFrame.setVisible(true);
		mainFrame.setTitle("Test");
		mainFrame.setSize(400,400);
	}
	
	public static void main(String[] args){
		test T = new test();
		T.mainFrame.repaint();
		T.panel1.repaint();
		
	}
		
}

Any help would be much appreciated. :)

Recommended Answers

i'm not sure, but i think you need to write setContentPane(container);

Jump to Post

There was a few problems. I've got a working version below that you can look at. If you have an older version of java it may not run correctly. Now, you'll have to extend JFrame to get some of the methods I'm using, which I prefer to extend JFrame anyways.

Jump to Post

All 5 Replies

i'm not sure, but i think you need to write setContentPane(container);

... explain plz.

There was a few problems. I've got a working version below that you can look at. If you have an older version of java it may not run correctly. Now, you'll have to extend JFrame to get some of the methods I'm using, which I prefer to extend JFrame anyways.

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

public class test extends JFrame{

	JTabbedPane JTP;
	JPanel panel1, panel2;
	JSlider JS;	
	
	public test(){
		
		panel1 = new JPanel();
		panel1.setBackground(Color.black);
		panel1.setForeground(Color.white);
				
		JS = new JSlider();
		JS.setForeground(Color.white);
		JS.setMinimum(0);
		JS.setMaximum(100);
		JS.setMinorTickSpacing(5);
		JS.setMajorTickSpacing(10);
				
		JS.setPaintTicks(true);
		JS.setPaintLabels(true);
		
		panel2 = new JPanel();
		panel2.setBackground(Color.black);
		panel2.setForeground(Color.white);
		panel2.setLayout(new BorderLayout());
		panel2.add(JS, BorderLayout.CENTER);
				
		JTP = new JTabbedPane();
		JTP.add("Tab1", panel1);
		JTP.add("Tab2", panel2);
		JTP.setForeground(Color.green);
		
		Container content = getContentPane();
		content.add(JTP);
		setContentPane(content);
		setTitle("Test");
		setSize(400,400);
		setVisible(true);
	}
	
	public static void main(String[] args){
		test T = new test();
		
	}
		
}

excellent. Thanks server_crash. :D

You're very welcome. If you need anything else, just let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.