Solved

Dialog  - enable false - if jtext field is NULL

Posted on 2011-09-23
12
187 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now