Java swing help needed


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Java swing help needed

  1. #1
    Join Date
    Jan 2007
    Posts
    60

    Java swing help needed

    I am trying to learn how to create user interfaces using Java Swing and I've been able to add combo boxs, text fields, labels etc but now i'm stuck.

    I am trying to add a panel (or at least something that I can add graphics to such as circles or squares....anything) but when I run my program the graphics aren't showing.

    How do i add a panel so that the graphics show. What i did get to show was a tiny little square when the background color was set to purple but nothing you can put anything onto.

    I have tried creating a class to extend the JPanel but it doesn't work even thought it compiles.

    This is just a test program to help me learn

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    class SwingWindow extends JFrame
    {
    	public SwingWindow()
    	{
    		super("Hello from Java Swing");
    		setSize(300, 100);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		
    		Container contentArea = getContentPane();
    		contentArea.setBackground(Color.white);
    		
    		FlowLayout flowManager = new FlowLayout();
    		contentArea.setLayout(flowManager);
    		
    		JComboBox islands = new JComboBox();
    		islands.addItem("Corfu");
    		islands.addItem("Kefalonia");
    		islands.addItem("Crete");
    		islands.addItem("Rhodes");
    		islands.addItem("Paxos");
    		islands.addItem("Mykonos");
    		
    		contentArea.add(islands);										
    		
    		JButton playButton = new JButton("Play");
    		contentArea.add(playButton);
    		
    		JButton pauseButton = new JButton("Pause");
    		contentArea.add(pauseButton);
    		
    		JButton stopButton = new JButton("Stop");
    		contentArea.add(stopButton);
    		
    		JLabel textLabel = new JLabel("Fun programming :");
    		contentArea.add(textLabel);
    		
    		JTextField text = new JTextField("", 15);
    		contentArea.add(text);
    		
    		CustomPanel panel = new CustomPanel();
    		contentArea.add(panel);
    		
    		setContentPane(contentArea);				
    	}
    	
    	class CustomPanel extends JPanel
    	{
    		public void paintComponent(Graphics painter)
    		{
    			painter.setColor(Color.white);
    			painter.fillRect(0,0,getSize().width, getSize().height);
    			Font currentFont = new Font("Serif",Font.BOLD,24);
    			painter.setFont(currentFont);
    			
    			Color customPurple = new Color(128,0,128);
    			painter.setColor(customPurple);
    			painter.drawString("A painted panel", 20, 40);
    		}
    	}
    }
    
    public class HelloSwing
    {
    	public static void main(String[] args)
    	{
    		SwingWindow Hello = new SwingWindow();
    	}
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    59
    Add:
    setVisible(true);

    Below:
    setContentPane(contentArea);
    My own java video tutorials, feel free to watch it.
    http://www.engineeringserver.com/for...amming-b311.0/ Need java help? For beginning and intermediate java coders!

  3. #3
    Join Date
    Jan 2007
    Posts
    60
    I tried adding that but it didn't make any difference. I also set the colour of the extened panel to red and all it does is display a tiny red square

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 06:58 AM
  2. Java Programmers needed! Northeast Ohio
    By SillyEagle in forum Careers
    Replies: 0
    Last Post: 10-24-2006, 03:03 PM
  3. Replies: 2
    Last Post: 06-14-2006, 04:16 PM
  4. Anchorage and Juneau AK - Java Devs needed
    By akrecruiter in forum Careers
    Replies: 0
    Last Post: 08-05-2005, 09:12 PM
  5. j#.not
    By Brad O'Hearne in forum Talk to the Editors
    Replies: 2
    Last Post: 11-05-2001, 10:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center