Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag for United States of America

asked on

Dialog - enable false - if jtext field is NULL

I am creating a dialog with 4 jtext fields as well as 4 buttons.  I will attach the method creating this.  

My first jtext field is called Current Project.  If current Project is null, I need to gray out the rest of the jtext fields and disable all the buttons except the change button next to current project.  When the change button is pushed I want to call a class named ProjectDialog.


private JPanel currentItems(){
	
      JLabel eLabel = new JLabel("Current Project");
	  JLabel threeLabel = new JLabel("3D Translators");
	  JLabel twoLabel = new JLabel("2D Translators");
	  JLabel emailLabel = new JLabel("Emails");
	  final JTextField curProject = new JTextField(20);
	  final JTextField cur3dTrans = new JTextField(20);
	  final JTextField cur2dTrans = new JTextField(20);
	  final JTextField curEmails = new JTextField(20);
	
	  final JButton projectButton = new JButton("Change");
     //projectButton.setBackground(Color.red);
     //projectButton.setForeground(Color.red); 
      projectButton.setPreferredSize(new Dimension(100, 20));
      projectButton.setEnabled(true);
      projectButton.addActionListener(
      new ActionListener() {
         public void actionPerformed( ActionEvent event )
         {
        	dispose();   
         }           
      });
      final JButton threeDButton = new JButton("Change");
      //projectButton.setBackground(Color.red);
      //projectButton.setForeground(Color.red); 
      threeDButton.setPreferredSize(new Dimension(100, 20));
      threeDButton.setEnabled(true);
      threeDButton.addActionListener(
      new ActionListener() {
         public void actionPerformed( ActionEvent event )
         {
        	dispose();   
         }           
      });
      final JButton twoDButton = new JButton("Change");
      //projectButton.setBackground(Color.red);
      //projectButton.setForeground(Color.red); 
      twoDButton.setPreferredSize(new Dimension(100, 20));
      twoDButton.setEnabled(true);
      twoDButton.addActionListener(
      new ActionListener() {
         public void actionPerformed( ActionEvent event )
         {
         	dispose();   
         }           
      });
      final JButton emailButton = new JButton("Change");
      //projectButton.setBackground(Color.red);
      //projectButton.setForeground(Color.red); 
      emailButton.setPreferredSize(new Dimension(100, 20));
      emailButton.setEnabled(true);
      emailButton.addActionListener(
      new ActionListener() {
         public void actionPerformed( ActionEvent event )
        {
        	dispose();   
        }           
      });
		
	 JPanel p = new JPanel();
     p.setBorder(BorderFactory.createLineBorder(Color.green));
     GridBagLayout gridbag = new GridBagLayout();
     p.setLayout(gridbag);
    
     gbc.insets = new Insets(5,10,10,5);      
     set_gbc(0, 0, 1, 1,GridBagConstraints.HORIZONTAL );
     gridbag.setConstraints(eLabel, gbc);
     p.add(eLabel);
    
     set_gbc(1, 0, 1, 1,GridBagConstraints.HORIZONTAL );
     gridbag.setConstraints(threeLabel, gbc);
     p.add(threeLabel);
    
     set_gbc(2, 0, 1, 1,GridBagConstraints.HORIZONTAL );
     gridbag.setConstraints(twoLabel, gbc);
     p.add(twoLabel);
    
     set_gbc(3, 0, 1, 1,GridBagConstraints.HORIZONTAL );
     gridbag.setConstraints(emailLabel, gbc);
     p.add(emailLabel);
    
     set_gbc(0, 1, 1, 1,GridBagConstraints.HORIZONTAL);
     gridbag.setConstraints(curProject, gbc);
     p.add(curProject);
    
     set_gbc(1, 1, 1, 1,GridBagConstraints.HORIZONTAL);
     gridbag.setConstraints(cur3dTrans, gbc);
     p.add(cur3dTrans);
    
     set_gbc(2, 1, 1, 1,GridBagConstraints.HORIZONTAL);
     gridbag.setConstraints(cur2dTrans, gbc);
     p.add(cur2dTrans);
    
     set_gbc(3, 1, 1, 1,GridBagConstraints.HORIZONTAL);
     gridbag.setConstraints(curEmails, gbc);
     p.add(curEmails);
    
     set_gbc(0, 2, 1, 1, GridBagConstraints.BOTH);
     gridbag.setConstraints(projectButton, gbc);
     p.add(projectButton);
    
     set_gbc(1, 2, 1, 1, GridBagConstraints.BOTH);
     gridbag.setConstraints(threeDButton, gbc);
     p.add(threeDButton);
    
     set_gbc(2, 2, 1, 1, GridBagConstraints.BOTH);
     gridbag.setConstraints(twoDButton, gbc);
     p.add(twoDButton);
    
     set_gbc(3, 2, 1, 1, GridBagConstraints.BOTH);
     gridbag.setConstraints(emailButton, gbc);
     p.add(emailButton);
     return p;
   }// currentItems()

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

So what is your question?

Do you see some error?

Please, post the whole class, so that it could be compiled and run.
jTextField shoud not be null
It may have empty text, but it is not null,
once you created it
You still can call jTextField.getText()
Avatar of jkteater

ASKER

jTextField right now is just a input field.  But once I get all the code written for the project - it will be a populated field.  When the user creates the dialog all the fields will be populated.  I don't have the backend code written to go get the data and put it in the fields.  So while I wait for that code to get completed, I want to go ahead and get the logic of graying out the buttons and fields and create my next dialog class.
public void createDialog() {
	  Component currentPanel = currentItems();
   	  centerPanel = new JPanel();
      centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
      centerPanel.setPreferredSize(new Dimension(750, 350));
            
      centerPanel.add(Box.createRigidArea(new Dimension(5, 0)));
      centerPanel.add(currentPanel);
      centerPanel.add(Box.createHorizontalGlue());
      centerPanel.add(Box.createRigidArea(new Dimension(0, 2)));
      
      
      getContentPane().add(centerPanel);
      
      addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent event) {
           Component c = event.getComponent();
           c.setSize(Math.max((int)centerPanel.getPreferredSize().getWidth()/2, c.getWidth()),
                     Math.max((int)centerPanel.getPreferredSize().getHeight()/2, c.getHeight()));
        }
      });
  	  	
      this.pack();
      setLocation(getParent().getLocation(null));        
              
      //Display the window.
      centerPanel.setVisible(true);
        
   }// end createDialog()

Open in new window

There is not much about disabling the buttons and etxtfileds
You just use method setEnabled(true) or setEnabled(false);

It has been my experience with the button though, that
i needed to remove the listeners if I want the buttons
stop responding to clicks.
setEnabled(false) affect the appearance - still it responds to the events

And openeing the dDialog - weel you may write you custom class extend JDialog
and in the cnostructor make sure iit craetes all elements
and then calls setVisible(true)

Then form calling class you just ccall the constructor - ad you dialog will pop-up.
I think you did already a lot of such stuff







So I do such things if I want to disable the button:

Submit.removeMouseListener(this);
Submit.setEnabled(false);


For JTextfiled you proabbly
want to use
setEditable(false);


ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How would put the logic around the code?  Should I write another method that has all the needed changes to the fields and buttons.  Place a if statement at the start of currentItems()

If curProject is null {
   disableProject();
}

In the constructor you set them up disabled as your cyrretnt project is probably initially null
Then when some even happanes when a user sets the project to be not null - at that momemt
you call setenabled(true) on all neecssary elements
Keep these elements as instance varaibles, so you can access them from any method, event handler, etc.
I am going to start a new question on a different question I have now.  Then get back to this one
off for the weekend - be back Monday
Good luck. Have  agood weekend.