Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Button disable

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Button disable

    hello friends

    i am using netbeans and i developing an application .. the thing is that when i execute my code then i want to make my few buttons disabled
    and that should be done without any button action .. means when programms executes then it must disable some of the buttons initially ....
    how to do that plz help !!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Button disable

    Look at the API doc for the classes you are using and see it there are any methods to call that will do what you want. Then call those methods when you want them to be executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Button disable

    their is a method called setEnabled(false); but that can be done only with the button action in netbeans .. how to make disable before any click when my frame appears for the first time in netbeans
    thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Button disable

    Call that method before calling the setVisible() method for the frame.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Button disable

    here is my code

    public class NewJFrame extends javax.swing.JFrame {

    static int count =0;
    int cnt=0;
    public NewJFrame() {

    initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    t1 = new javax.swing.JTextField();
    t2 = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    b1 = new javax.swing.JButton();
    b2 = new javax.swing.JButton();
    b3 = new javax.swing.JButton();
    jLabel6 = new javax.swing.JLabel();
    l2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jLabel1.setText("Sample Text");

    jLabel2.setText("Enter the Text as shown above");

    b1.setText("Submit");
    b1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    b1ActionPerformed(evt);
    }
    });

    b2.setText("Start/Next ");
    b2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    b2ActionPerformed(evt);
    }
    });

    b3.setText("Exit");
    b3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    b3ActionPerformed(evt);
    }
    });

    jLabel6.setText("Check your Speed");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(19, 19, 19)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(layout.createSequentialGroup()
    .addGap(35, 35, 35)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addComponent(l2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap())
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel6)
    .addGap(0, 0, Short.MAX_VALUE))))
    .addGroup(layout.createSequentialGroup()
    .addGap(72, 72, 72)
    .addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(79, 79, 79)
    .addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(91, 91, 91)
    .addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel6)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 35, Short.MAX_VALUE)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(l2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(37, 37, 37)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(b1)
    .addComponent(b2)
    .addComponent(b3))
    .addContainerGap(53, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
    count++;
    t1.setTransferHandler(null);

    if(count==1)
    {

    t1.setText("Honorificabilitudinitatibus");
    t2.setText("");
    l2.setText("");
    t1.setEditable(false);
    }
    else if(count==2)
    {

    t1.setText("Antidisestablishmentarianism");
    t2.setText("");
    l2.setText("");
    t1.setEditable(false);
    }
    else if(count==3)
    {
    t1.setText("Floccinauccinihilipilification");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==4)
    {

    t1.setText("Supercalifragilisticexpialidocious");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==5)
    {

    t1.setText("Hepaticocholangiocholecystenterostomie s");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==6)
    {

    t1.setText("Osseocaynisanguineoviscericartilaginin ervomedullary");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==7)
    {

    t1.setText("Poltergeist");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==8)
    {

    t1.setText("Placebo");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==9)
    {

    t1.setText("Juggernaut");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==10)
    {

    t1.setText("calisthenics");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==11)
    {

    t1.setText("reverberate");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==12)
    {

    t1.setText("Lamprophony");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==13)
    {

    t1.setText("Finnimbrun");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==14)
    {

    t1.setText("Mesonoxian");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==15)
    {

    t1.setText("Nihilarian");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==16)
    {

    t1.setText("voluptuous");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==17)
    {

    t1.setText("Pulveratricious");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==18)
    {

    t1.setText("Scopperloit");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==19)
    {

    t1.setText("Zabernism");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==20)
    {

    t1.setText("farthingale");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==21)
    {

    t1.setText("fata morgana");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==22)
    {

    t1.setText("quadragintesimal");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==23)
    {

    t1.setText("quadricentennial");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==24)
    {

    t1.setText("question extraordinaire");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==25)
    {

    t1.setText("quinquagenarian");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==26)
    {

    t1.setText("quod erat demonstrandum");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }

    else if(count==27)
    {

    t1.setText("quincunx");
    t1.setEditable(false);
    t2.setText("");
    l2.setText("");
    }
    else if(count==28)
    {

    t1.setText("COMPLETED !!!!!!!!!!!");
    t1.setEditable(false);
    t2.setText("COMPLETED !!!!!!!!!!!");
    t2.setEditable(false);
    l2.setText("Total Correct :"+cnt+"");
    b1.setEnabled(false);
    b2.setEnabled(false);
    }
    }

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {

    if(t1.getText().equalsIgnoreCase(t2.getText()))
    {
    l2.setText("Correct");
    cnt++;
    }
    else
    {
    l2.setText("Wrong");

    }
    }

    private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
    System.exit(0);

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new NewJFrame().setVisible(true);


    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton b1;
    private javax.swing.JButton b2;
    private javax.swing.JButton b3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel l2;
    private javax.swing.JTextField t1;
    private javax.swing.JTextField t2;
    // End of variables declaration
    }
    plzz specify where to put to disable Button b1 ,,, because wen i tried it giving error
    Thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Button disable

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    You can call methods on an object any time after you have created an instance of it.
    Try adding the call to the method right after an instance is created:
    b1 = new ....
    b1.setEnabled(false);

    i tried it giving error
    Please copy and paste here the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Button disable

    Thanx Norm

    Actually in Netbeans we usually cannot edit the code that is written in the initComponents(); method so to add that code before any click is not possible .. but i found that we can edit the property in the property window..... there i did and found my solution
    Thnx for your support and valuable time

Similar Threads

  1. Disable Broser Back Button In ServerSide.
    By Srikanthjwp in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: November 15th, 2011, 12:09 AM
  2. disable a button ?
    By dime111 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 28th, 2011, 02:58 PM
  3. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM
  4. how to disable forward and backward buttons using js
    By Raneesh in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: February 3rd, 2011, 04:40 AM
  5. Insert master or gloabal java script to disable globally onclick
    By karnesb in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2008, 04:10 PM