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

jkteaterAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
When you create you
ProjectDialog you extend JDialog

class ProjectDialog extends JDialog {

ProjectDialog(JFrame parent, String title, boolean modal){
super(parent, title, modal);
//populate your GUI

this.setVisible(true);
}



}


and in your calling class (ssupposedly extending JFrame)
in the action method of your button

you just say

ProjectDialog pd = new ProjectDialog(this, "My Project dialog", true);


The booolean modal will deteremin if you dialog
will be modal (will blokc access to your parent JFrame, while it is opne
modal = true) or not modal (will not block JFrame)


0
 
for_yanCommented:
So what is your question?

Do you see some error?

Please, post the whole class, so that it could be compiled and run.
0
 
for_yanCommented:
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()
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jkteaterAuthor Commented:
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.
0
 
jkteaterAuthor Commented:
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

0
 
for_yanCommented:
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





0
 
for_yanCommented:


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);


0
 
jkteaterAuthor Commented:
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();
}

0
 
for_yanCommented:
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.
0
 
jkteaterAuthor Commented:
I am going to start a new question on a different question I have now.  Then get back to this one
0
 
jkteaterAuthor Commented:
off for the weekend - be back Monday
0
 
for_yanCommented:
Good luck. Have  agood weekend.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.