Java GUI problem... contents of JFrame is invisible...

Reply

Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

Java GUI problem... contents of JFrame is invisible...

 
0
  #1
Jun 12th, 2005
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.


  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.JSlider;
  6. import javax.swing.JTabbedPane;
  7.  
  8. public class test{
  9.  
  10. JFrame mainFrame;
  11. JTabbedPane JTP;
  12. JPanel panel1, panel2;
  13. JSlider JS;
  14.  
  15. public test(){
  16.  
  17. mainFrame = new JFrame();
  18.  
  19. panel1 = new JPanel();
  20. panel1.setBackground(Color.black);
  21. panel1.setForeground(Color.white);
  22.  
  23. JS = new JSlider();
  24. JS.setForeground(Color.white);
  25. JS.setMinimum(0);
  26. JS.setMaximum(100);
  27. JS.setMinorTickSpacing(5);
  28. JS.setMajorTickSpacing(10);
  29.  
  30. JS.setPaintTicks(true);
  31. JS.setPaintLabels(true);
  32.  
  33. panel2 = new JPanel();
  34. panel2.setBackground(Color.black);
  35. panel2.setForeground(Color.white);
  36. panel2.setLayout(new BorderLayout());
  37. panel2.add(JS, BorderLayout.CENTER);
  38.  
  39. JTP = new JTabbedPane();
  40. JTP.add("Tab1", panel1);
  41. JTP.add("Tab2", panel2);
  42. JTP.setForeground(Color.green);
  43. mainFrame.getContentPane().add(JTP);
  44. mainFrame.setVisible(true);
  45. mainFrame.setTitle("Test");
  46. mainFrame.setSize(400,400);
  47. }
  48.  
  49. public static void main(String[] args){
  50. test T = new test();
  51. T.mainFrame.repaint();
  52. T.panel1.repaint();
  53.  
  54. }
  55.  
  56. }


Any help would be much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Java GUI problem... contents of JFrame is invisible...

 
0
  #2
Jun 12th, 2005
i'm not sure, but i think you need to write setContentPane(container);
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

Re: Java GUI problem... contents of JFrame is invisible...

 
0
  #3
Jun 12th, 2005
... explain plz.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Java GUI problem... contents of JFrame is invisible...

 
0
  #4
Jun 12th, 2005
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.

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class test extends JFrame{
  5.  
  6. JTabbedPane JTP;
  7. JPanel panel1, panel2;
  8. JSlider JS;
  9.  
  10. public test(){
  11.  
  12. panel1 = new JPanel();
  13. panel1.setBackground(Color.black);
  14. panel1.setForeground(Color.white);
  15.  
  16. JS = new JSlider();
  17. JS.setForeground(Color.white);
  18. JS.setMinimum(0);
  19. JS.setMaximum(100);
  20. JS.setMinorTickSpacing(5);
  21. JS.setMajorTickSpacing(10);
  22.  
  23. JS.setPaintTicks(true);
  24. JS.setPaintLabels(true);
  25.  
  26. panel2 = new JPanel();
  27. panel2.setBackground(Color.black);
  28. panel2.setForeground(Color.white);
  29. panel2.setLayout(new BorderLayout());
  30. panel2.add(JS, BorderLayout.CENTER);
  31.  
  32. JTP = new JTabbedPane();
  33. JTP.add("Tab1", panel1);
  34. JTP.add("Tab2", panel2);
  35. JTP.setForeground(Color.green);
  36.  
  37. Container content = getContentPane();
  38. content.add(JTP);
  39. setContentPane(content);
  40. setTitle("Test");
  41. setSize(400,400);
  42. setVisible(true);
  43. }
  44.  
  45. public static void main(String[] args){
  46. test T = new test();
  47.  
  48. }
  49.  
  50. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

Re: Java GUI problem... contents of JFrame is invisible...

 
0
  #5
Jun 13th, 2005
excellent. Thanks server_crash.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Java GUI problem... contents of JFrame is invisible...

 
0
  #6
Jun 13th, 2005
You're very welcome. If you need anything else, just let me know.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC