Solved

Dialog  - enable false - if jtext field is NULL

Posted on 2011-09-23
12
197 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:jkteater
  • 7
  • 5
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36589113
So what is your question?

Do you see some error?

Please, post the whole class, so that it could be compiled and run.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36589129
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
 

Author Comment

by:jkteater
ID: 36589232
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jkteater
ID: 36589248
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36589298
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36589337


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
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 36589366
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
 

Author Comment

by:jkteater
ID: 36589416
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36589452
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
 

Author Comment

by:jkteater
ID: 36589480
I am going to start a new question on a different question I have now.  Then get back to this one
0
 

Author Comment

by:jkteater
ID: 36589738
off for the weekend - be back Monday
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36589756
Good luck. Have  agood weekend.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question