Jump to content
  • Advertisement
Sign in to follow this  
KenWill

Java Swing focusGained not working

This topic is 975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I did this before and it worked as intended but now i just can't seem to get it working. I know my code is not the problem because i tested it on the same panel and it worked. For me it looks like focusGained function is never called and that's why its not working. Here is the code:

	public void initialize(JPanel editPanel, JPanel propertiesPanel)
	{
		this.editPanel = panel;
		this.propertiesPanel = propertiesPanel;
		
		panel = new JPanel();
		initializeProperties();
		panel.setBounds(0, 0, 340, 340);
		panel.setLayout(null);
		panel.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		panel.addMouseMotionListener(new MouseMotionListener(){

			@Override
			public void mouseDragged(MouseEvent me) {
				xPos = panel.getX() + me.getX() - panel.getWidth()/2;
				yPos = panel.getY() + me.getY() - panel.getHeight()/2;
				
				// CHECK IF OUT OF BOUNDS
				if(xPos < 0)
				{
					xPos = 0;
				}
				else if(xPos >= editPanel.getWidth() - panel.getWidth())
				{
					xPos = editPanel.getWidth() - panel.getWidth();
				}
				
				if(yPos < 0)
				{
					yPos = 0;
				}
				else if(yPos >= editPanel.getHeight() - panel.getHeight())
				{
					yPos = editPanel.getHeight() - panel.getHeight();
				}
				panel.setLocation(xPos, yPos);
				
			}

			@Override
			public void mouseMoved(MouseEvent me) {
				
			}
			
		});
		panel.addFocusListener(new FocusListener(){

			@Override
			public void focusGained(FocusEvent e) {
			//	propertiesPanel.removeAll();
				
				propertiesPanel.add(widthLabel);
				propertiesPanel.add(widthField);
				
				propertiesPanel.add(heightLabel);
				propertiesPanel.add(heightField);
				
				propertiesPanel.add(backgroundColorLabel);
				propertiesPanel.add(backgroundColorField);
				
				propertiesPanel.add(backgroundImageLabel);
				propertiesPanel.add(backgroundImageField);
				
				propertiesPanel.add(transparentCheck);
				
				propertiesPanel.add(borderTypeLabel);
				propertiesPanel.add(borderTypeComboBox);

				propertiesPanel.add(borderWidthLabel);
				propertiesPanel.add(borderWidthField);
				
				propertiesPanel.add(borderRadiusLabel);
				propertiesPanel.add(borderRadiusField);
				
				propertiesPanel.add(paddingLabel);
				propertiesPanel.add(paddingField);
				
				propertiesPanel.repaint();
				
			}

			@Override
			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		editPanel.add(panel);
		editPanel.repaint();
		
	}
	
	private void initializeProperties()
	{
		widthLabel = new JLabel("Width");
		widthLabel.setBounds(24, 11, 46, 14);
		
		widthField = new JTextField();
		widthField.setBounds(126, 8, 60, 20);
		widthField.setColumns(10);
		
		heightLabel = new JLabel("Height");
		heightLabel.setBounds(24, 36, 46, 14);
		
		heightField = new JTextField();
		heightField.setBounds(126, 33, 60, 20);
		heightField.setColumns(10);
		
		backgroundColorLabel = new JLabel("Background Color");
		backgroundColorLabel.setBounds(10, 61, 86, 14);
		
		backgroundColorField = new JTextField();
		backgroundColorField.setBounds(126, 58, 60, 20);
		backgroundColorField.setColumns(10);
		
		backgroundImageLabel = new JLabel("Background Image");
		backgroundImageLabel.setBounds(10, 86, 101, 14);
		
		backgroundImageField = new JTextField();
		backgroundImageField.setBounds(106, 83, 80, 20);
		backgroundImageField.setColumns(10);
		
		transparentCheck = new JCheckBox("Transparent");
		transparentCheck.setBounds(24, 107, 97, 23);
		
		borderTypeLabel = new JLabel("Border Type");
		borderTypeLabel.setBounds(10, 137, 86, 14);
		
		borderTypeComboBox = new JComboBox();
		borderTypeComboBox.setBounds(106, 134, 80, 20);
		
		borderWidthLabel = new JLabel("Border Width");
		borderWidthLabel.setBounds(10, 162, 63, 14);
		
		borderWidthField = new JTextField();
		borderWidthField.setBounds(126, 159, 60, 20);
		borderWidthField.setColumns(10);
		
		borderRadiusLabel = new JLabel("Border Radius");
		borderRadiusLabel.setBounds(10, 187, 86, 14);
		
		borderRadiusField = new JTextField();
		borderRadiusField.setBounds(126, 184, 60, 20);
		borderRadiusField.setColumns(10);
		
		paddingLabel = new JLabel("Padding");
		paddingLabel.setBounds(10, 212, 46, 14);
		
		paddingField = new JTextField();
		paddingField.setBounds(126, 209, 60, 20);
		paddingField.setColumns(10);
	}
Edited by UAas

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!