Results 1 to 8 of 8
  1. #1
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Question Combo box help in SWING

    Hi. This is part of a larger assignment but at the moment I am trying to get the combo box to display for me.

    I have not used SWING before and I'm getting two errors in the code on serialized classes.

    Any hints on how to proceed are appreciated, here's my drafted code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    
    
    public class Homework extends JFrame
    {
    	public Homework()
    	{
    		setSize(400, 200);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    	
    	//JComboBox (positioned left)
    	//JPanel for the drawing area (positioned right)
    	
    	
    	public class DrawingPanelDemo extends JPanel implements ActionListener 
    	{
    			
    		JPanel drawingPanel;
    		
    		public DrawingPanelDemo()
    		{
    			//super(new BorderLayout());
    			
    			
    		    String[] shapeStrings = {"Rectangle", "Oval", "Line", "Rounded Rectangle", "Filled Rectangle"};
    		    
    		    JComboBox shapeList = new JComboBox(shapeStrings);
    		    shapeList.setSelectedIndex(1);
    		    shapeList.addActionListener(this);
    		    
    		    
    			drawingPanel = new JPanel();
    			
    			
    		}
    		
    		//listen to the combo box.
    		public void actionPerformed(ActionEvent e)
    		{
    			JComboBox comboBox = (JComboBox)e.getSource();
    			String shapeList = (String)comboBox.getSelectedItem();
    			//updateLabel (shapeList);
    		}
    	}
    	/*
    	public void paint (Graphics g) 
    	{
    		int x1;
    		int x2;
    		int y1;
    		int y2;
    		
    		
    		x1 = getX();
    		y1 = getY();
    				
    		x2 = evt.getX();
    		y2 = evt.getY();
    		
    	    int width = x2 - x1;
    	    int height = y2 - y1;
    	    int arcWidth = 10;
    	    int arcHeight = 10;
    		
    		Graphics2D g2 = (Graphics2D) g;
    	    
    	    g.setColor(Color.BLACK);
    	    g.fillRect(x1, y1, width, height);
    	    g.drawRect(x1, y1, width, height);
    	    //g.drawOval(x1, y1, width, height);
    	    g.drawLine(x1, y1, x2, y2);
    	    g.drawOval(x1, y1, arcWidth, arcHeight);
    	    g.drawRoundRect(x1, y1, width, height, arcWidth, arcHeight);
    	    
    	    
    	    //addWindowListener()
    	    
    	    
    	    // draw Rectangle2D.Double
    	    g2.draw(new Rectangle2D.Double(x1, y1, x2, y2));
    	    
    	    
    	    // draw RoundRectangle2D.Double
    	    g2.draw(new RoundRectangle2D.Double(x1, y1, x2, y2, 10, 10));
    	    
    	    // draw Ellipse2D.Double
    	    g2.draw(new Ellipse2D.Double(x1, y1, x2, y2));
    	    
    	}
    */
    	
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame();
    		frame.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e){
    				System.exit(0);
    			}
    		});
    		
    		Homework app = new Homework();
    	}
    
    }
    "All animals are equal, but some are more equal than others."
    - George Orwell

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

    Default

    You would help much by posting your errors. The code compiles fine for me as I get no errors. I do get several warnings though (big difference!), one being that "the serializable class Homework does not does not declare a static final serialVersionUID field of the type long". You can ignore this or place the annotation @SuppressWarnings("serial") above your class declaration like so:

    Java Code:
    @SuppressWarnings("serial")
    public class Homework extends JFrame {

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

    Default

    Also, you never create a DrawingPanelDemo object and never add this object to the displaying JFrame. Also, you your DrawingPanelDemo class has a JComboBox, but never adds it to anything (such as the DrawingPanelDemo's JPanel), so it will never show. Also, your main method creates an extra JFrame at the start for no apparent reason.

    You would benefit by going through the Sun Swing tutorial without delay:
    Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  4. #4
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Default

    Ok cool I'll go try that and see what I can come up with. I meant "warnings" not "errors", the code does compile, you are right.

    I went through the SWING tutorial but I think I need something better than that.

    Thanks.
    "All animals are equal, but some are more equal than others."
    - George Orwell

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I went through the SWING tutorial but I think I need something better than that.
    What is better than a working example? All you have to do is download the code run it and understand the code. Then you compare your code with the working code to see whats different.

    By the way the painting code is not correct. Again the Swing tutorial on Custom Painting has a working example that you can compare your code with to see whats different.

    Start with working examples and make changes and you will have less problems. Then you read the API to fill in the blanks when you want to do something different that isn't covered in the tutorial.

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

    Default

    Quote Originally Posted by WarmRegards View Post
    Ok cool I'll go try that and see what I can come up with. I meant "warnings" not "errors", the code does compile, you are right.
    Cool.

    I went through the SWING tutorial but I think I need something better than that.
    You seem to have gone through it rather quickly. :rolleyes:

    I seriously know of no better resource for Swing, and I recommend that you keep going through it, and keep working on your code. Remember to work on only one problem at a time. For instance right now if I were you, I'd work on trying to get a JComboBox displayed, and that's it. Then once you've succeeded with that, work on getting the user's selection. As camickr notes above, the tutorials have some not so bad working sample code.

    Please feel free to on back if you have any specific questions on your new code.

    Best of luck!

  7. #7
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Default

    Ok so I got it to work using "BorderLayout" but then I borked it when i switched it to FlowLayout.

    Here is that piece of code

    Java Code:
    	public DrawFrame()
    	  {
      		setTitle("DrawFrame");
      		
      	        // setLayout(new FlowLayout(FlowLayout.LEFT));
      		
      		/* Create dropdown */
                    shapeCombo = new JComboBox();
      		shapeCombo.setLayout(new FlowLayout(FlowLayout.LEADING));
            
                    shapeCombo.setEditable(false);
      		shapeCombo.addItem("Rectangle");
      		shapeCombo.addItem("Oval");
      		shapeCombo.addItem("Line");
      		shapeCombo.addItem("Rounded Rectangle");
      		shapeCombo.addItem("Filled Rectangle");
      		
      		/* Left and right panels, one is a dropdown, one is a draw panel */
      		JPanel drawPanel = new JPanel();
      		drawPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
    		MyMouseListener drawPanelListener = new               MyMouseListener(drawPanel);
    		MyComboBoxListener comboBoxListener = new MyComboBoxListener(drawPanel);
    		  
      		shapeCombo.addActionListener(comboBoxListener);
        	        drawPanel.addMouseListener(drawPanelListener);
        	
        	        /* Add these to the JFrame */
        	        add(shapeCombo);
        	        add(drawPanel);
        	        //add(shapeCombo, FlowLayout.LEFT_ALIGNMENT);
        	        //add(drawPanel, FlowLayout.TOP_ALIGNMENT);
        	
        	
      		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
      	}
    now the problem is I have a combo box (shapeCombo) and a drawing panel (JPanel drawPanel). The idea is, left side of the screen has the drop down box and you pick a shape to draw, and right of the combo box is the drawing panel so you paint on there.

    Using the alignment I have, the whole 400x200 screen is simply a drawing panel and combo box is gone. My guess is that this has to do with the fact that im creating two different flow layouts, one to apply to shapeCombo.setLayout and another to drawPanel.setLayout.

    I'm tinkering with this, any suggestions on what I should be looking at here?
    "All animals are equal, but some are more equal than others."
    - George Orwell

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Ok so I got it to work using "BorderLayout" but then I borked it when i switched it to FlowLayout.
    You really need to read the Swing tutorial on using layout managers. You are NOT using a FlowLayout, you are still using a BorderLayout.

    Java Code:
    shapeCombo.setLayout(new FlowLayout(FlowLayout.LEADING));
    That line makes no sense. You don't set the layout of a combo box, you set the layout of a JPanel.

    Java Code:
    drawPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
    You never add any components to your "drawPanel" so there is no need to use a layout manager on the panel.

    Java Code:
    add(shapeCombo);
    add(drawPanel);
    The default layout manager for the content pane of the frame is a BorderLayout. When you don't specify a constraint the component get added to the "CENTER", but you can only have one component in the center so guess which one gets displayed?

    Read the tutorial, download the examples and play with them. That is how you learn. Almost everly line of code here is wrong which indicates you don't understand layout managers. So start with code that works and experiment. The FlowLayout and BorderLayout are the two easiest layout managers to understand so take the time to learn them properly.

Similar Threads

  1. combo box help... plus hi this is my first post
    By okabeer in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2009, 06:05 PM
  2. Combo Box Population
    By barusk in forum Java Applets
    Replies: 1
    Last Post: 03-20-2009, 01:48 PM
  3. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 03:14 PM
  4. graph + combo box
    By abelah in forum New To Java
    Replies: 7
    Last Post: 10-13-2008, 05:36 PM

Posting Permissions

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