简体   繁体   English

使用Java禁用和启用按钮

[英]disable and enable the button using java

How can i disable and enable the button using java (netbeans).Let Suppose i have a button name LiveMonitoring and have some output of ipadrress on jtextarea. 我如何使用Java(netbeans)禁用和启用按钮。假设我有一个名为LiveMonitoring的按钮,并且在jtextarea上有一些ipadrress输出。 what i want is Live Monitoring should be disabled to that extent till i select some ipaddress present on textarea. 我想要的是应该禁用Live Monitoring,直到我选择textarea上存在的ipaddress。 I am doing it like First i have disabled the button by LiveMonitoring.setEnabled(false) and as soon as i select some text 我这样做就像首先我已经通过LiveMonitoring.setEnabled(false)禁用了按钮,并且一旦我选择了一些文本

 package zeeshannisar210;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Home extends javax.swing.JFrame {
    JCheckBox sh1;
    static Socket csocket;
    public Home() 
    {
        initComponents();
    }





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

        jPanel2 = new javax.swing.JPanel();
        Home = new javax.swing.JButton();
        LiveMonitoring = new javax.swing.JButton();
        FileView = new javax.swing.JButton();
        Logs = new javax.swing.JButton();
        Logout = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        Logs1 = new javax.swing.JButton();
        Logs2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(70, 130, 180));

        Home.setBackground(new java.awt.Color(70, 130, 180));
        Home.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        Home.setText("Home");
        Home.setBorderPainted(false);
        Home.setFocusCycleRoot(true);
        Home.setFocusPainted(false);
        Home.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                HomeMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                HomeMouseReleased(evt);
            }
        });
        Home.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                HomeStateChanged(evt);
            }
        });
        Home.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HomeActionPerformed(evt);
            }
        });

        LiveMonitoring.setBackground(new java.awt.Color(70, 130, 180));
        LiveMonitoring.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        LiveMonitoring.setText("Live Monitoring");
        LiveMonitoring.setBorderPainted(false);
        LiveMonitoring.setFocusCycleRoot(true);
        LiveMonitoring.setFocusPainted(false);
        LiveMonitoring.setFocusable(false);
        LiveMonitoring.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                LiveMonitoringMouseReleased(evt);
            }
        });
        LiveMonitoring.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LiveMonitoringActionPerformed(evt);
            }
        });

        FileView.setBackground(new java.awt.Color(70, 130, 180));
        FileView.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        FileView.setText("File View");
        FileView.setBorderPainted(false);
        FileView.setFocusCycleRoot(true);
        FileView.setFocusPainted(false);

        Logs.setBackground(new java.awt.Color(70, 130, 180));
        Logs.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        Logs.setText("Logs");
        Logs.setBorderPainted(false);
        Logs.setFocusCycleRoot(true);
        Logs.setFocusPainted(false);

        Logout.setBackground(new java.awt.Color(70, 130, 180));
        Logout.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        Logout.setText("Logout");
        Logout.setBorderPainted(false);
        Logout.setFocusCycleRoot(true);
        Logout.setFocusPainted(false);
        Logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogoutActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Handwriting", 1, 24)); // NOI18N
        jLabel1.setText("        Ease Serve");

        Logs1.setBackground(new java.awt.Color(70, 130, 180));
        Logs1.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        Logs1.setText("Help");
        Logs1.setBorderPainted(false);
        Logs1.setFocusCycleRoot(true);
        Logs1.setFocusPainted(false);

        Logs2.setBackground(new java.awt.Color(70, 130, 180));
        Logs2.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        Logs2.setText("Contact Us");
        Logs2.setBorderPainted(false);
        Logs2.setFocusCycleRoot(true);
        Logs2.setFocusPainted(false);

        jTextArea1.setBackground(new java.awt.Color(173, 216, 230));
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(Home, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(146, 146, 146)
                .addComponent(LiveMonitoring, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(154, 154, 154)
                .addComponent(FileView, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(179, 179, 179)
                .addComponent(Logs, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(189, 189, 189)
                .addComponent(Logs1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
                .addComponent(Logs2)
                .addGap(84, 84, 84))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Logout, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39))
            .addComponent(jScrollPane1)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(549, 549, 549)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46)
                        .addComponent(Logout, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Home, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LiveMonitoring, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(FileView, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Logs, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Logs1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Logs2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        
    private void HomeActionPerformed(java.awt.event.ActionEvent evt) {                                     
    jTextArea1.setText("Clients ID\t\t\tClients Name\t\t\tClients IPAddress\n");
    int i=0;

    Connection conn=MySqlConnect.ConnectDB();
    String Sql="Select * from clients";
    try
    {
        PreparedStatement pst=conn.prepareStatement(Sql);
        ResultSet rs=pst.executeQuery();


        while(rs.next())
        {
            String str=rs.getString("hostname");
            String str1=rs.getString("ipaddress");
            i=i+1;
            jTextArea1.append(i+"\t\t\t"+str+"\t\t\t"+str1+"\n");
        }

    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,e);
    } 
     LiveMonitoring.setEnabled(false);



    }                                    



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

    new LiveMOnitoring().setVisible(true);
    this.dispose();
    }                                              

    private void LogoutActionPerformed(java.awt.event.ActionEvent evt) {                                       
    new Login().setVisible(true);
    this.dispose();       
    }                                      

    private void LiveMonitoringMouseReleased(java.awt.event.MouseEvent evt) {                                             
    }                                            

    private void HomeMouseReleased(java.awt.event.MouseEvent evt) {                                   
        if (jTextArea1.getSelectedText() != null)
    { 
        LiveMonitoring.setEnabled(true);
        String s = jTextArea1.getSelectedText();

    }
    }                                  

    private void HomeStateChanged(javax.swing.event.ChangeEvent evt) {                                  
        // TODO add your handling code here:
    }                                 

    private void HomeMouseClicked(java.awt.event.MouseEvent evt) {                                  
        if (jTextArea1.getSelectedText() != null)
    { 
        LiveMonitoring.setEnabled(true);
        String s = jTextArea1.getSelectedText();

    }
    }                                 

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Home().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    public javax.swing.JButton FileView;
    public javax.swing.JButton Home;
    javax.swing.JButton LiveMonitoring;
    public javax.swing.JButton Logout;
    public javax.swing.JButton Logs;
    public javax.swing.JButton Logs1;
    public javax.swing.JButton Logs2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}
);

    } 

but it isn'y enabling the button again although i have selected the ipaddress 但是,尽管我选择了ipaddress,但它并没有再次启用该按钮

Just remove the ActionListener on the button until there is something in the JTextArea. 只需删除按钮上的ActionListener,直到JTextArea中有内容即可。 You can also disable buttons but it makes the button look slightly grayed out hence I prefer the method of removing the ActionListener. 您也可以禁用按钮,但是它会使按钮看起来略显灰色,因此我更喜欢删除ActionListener的方法。

JButton.setEnabled(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM