JTextField werkt niet in JWindow?

Status
Niet open voor verdere reacties.

Juzzz

Gebruiker
Lid geworden
15 jan 2008
Berichten
281
In mijn gehele project gebruik ik JTextFields, maar nu sta ik echt perplex.
Ik krijg het niet voor elkaar om een JTextField te gebruiken in een JWindow.

Ik kan hem toevoegen, er code matig tekst in stoppen maar ik kan het niet selecteren of er in typen.

Iemand een idee?

De code heb ik even aangepast dat hij makkelijk te draaien is voor de gene die mij wilt helpen.
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;

public class TestPopup
{
	private static final int WIDTH = 300, HEIGHT = 180;
	private static final Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
	
	private static JLabel descriptionLabel,priorityLabel, descriptionLengthLabel;
	private static JTextField descriptionField;
	private static JComboBox priorityBox;
	private static JButton cancelButton;
	
	private static final int beginY = 30;
	private static final int beginX = 20;
	private static final int labelWith = 100;
	private static final int fieldWith = 150;
	private static final int labelHeight = 25;
	private static final int fieldHeight = 25;
	private static final int fieldSpace = 5;
	
	public static void newRiskItem()
	{
		
		final JWindow newRiskWindow = new JWindow();
		
		newRiskWindow.setBounds(center.x-WIDTH/2, center.y-HEIGHT/2, WIDTH, HEIGHT);
		newRiskWindow.setLayout(new BorderLayout());
		newRiskWindow.setBackground(Color.WHITE);
		
		//INIT
		//TOP
		JPanel topPanel = new JPanel(new FlowLayout());
		topPanel.add(new JLabel("new Item"));
		
		newRiskWindow.add(topPanel, BorderLayout.NORTH);
		
		//CENTER
		//init
		
		descriptionLabel = new JLabel("description");
		descriptionLabel.setBounds(beginX, beginY, labelWith, labelHeight);
		
		descriptionLengthLabel = new JLabel("0");
		descriptionLengthLabel.setBounds(beginX + labelWith + fieldWith, beginY, 10, labelHeight);
		
		priorityLabel = new JLabel("priority");
		priorityLabel.setBounds(beginX, beginY + labelHeight + fieldSpace, labelWith, labelHeight);
		
		//fields
		descriptionField = new JTextField("test", 10);
		descriptionField.setBounds(beginX + labelWith, beginY, fieldWith, fieldHeight);
		descriptionField.addKeyListener(new KeyListener()
		{	
			@Override
			public void keyPressed( KeyEvent e )
			{
				int length = descriptionField.getText().length();
				descriptionLengthLabel.setText(""+length);
				descriptionLengthLabel.setBackground(Color.BLACK);
				
				if(length > 255) descriptionLengthLabel.setBackground(Color.RED);
			}
			
			@Override
			public void keyTyped( KeyEvent e )
			{}
			
			@Override
			public void keyReleased( KeyEvent e )
			{}
		});
		
		initPriorityBox(beginX + labelWith, beginY + fieldHeight + fieldSpace, fieldWith, fieldHeight);
		
		JPanel centerPanel = new JPanel(null);
		centerPanel.add(descriptionLabel);
		centerPanel.add(descriptionField);
		centerPanel.add(descriptionLengthLabel);
		centerPanel.add(priorityLabel);
		centerPanel.add(priorityBox);
		
		newRiskWindow.add(centerPanel, BorderLayout.CENTER);
		
		//BOTTOM
		JPanel bottomPanel = new JPanel(new FlowLayout());
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(
	            new ActionListener() 
	            {
	                public void actionPerformed(ActionEvent e) 
	                {
	                	newRiskWindow.dispose();
	                }
	            });
		
		bottomPanel.add(cancelButton);
		
		newRiskWindow.add(bottomPanel, BorderLayout.SOUTH);
		
		newRiskWindow.setVisible(true);	
	}
	
	private static void initPriorityBox(int x, int y, int width, int height)
	{
		priorityBox = new JComboBox();
		priorityBox.setBounds(x, y, width, height);
		priorityBox.addItem("LOW");
		priorityBox.addItem("MEDIUM");
		priorityBox.addItem("HIGH");
	}
	
	public static void main(String[] args)
	{
		TestPopup.newRiskItem();
	}
}

Het zal vast iets zijn waar ik overheen kijk, maar als iemand mij kan helpen graag.

Alvast bedankt!
 
Opgelost

Heb het gevonden!

Het blijkt dus dat een JWindow geen focus kan krijgen.

Ik heb ook een oplossing gevonden. Als je in plaats van een JWindows een JFrame gebruikt en dan de setUndecorated(true); methode aanroept, kan de balk weggehaald worden.

alsnog bedankt.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan