2016-05-03 8 views
0

Wie kann ich die Schaltfläche mit Java (NetBeans) deaktivieren und aktivieren. Nehmen wir an, ich habe eine Schaltfläche Name LiveMonitoring und einige Ausgaben von ipadress auf jtexttarea. was ich will, ist Live-Monitoring sollte in diesem Umfang deaktiviert werden, bis ich einige ipaddress in Textarea auswählen. Ich mache es wie First Ich habe den Knopf durch LiveMonitoring.setEnabled (false) deaktiviert und sobald ich einen Text auswählendeaktivieren und aktivieren Sie die Schaltfläche mit Java

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

    } 

aber es isn'y ermöglicht die Taste erneut, obwohl ich die IP-Adresse ausgewählt haben

+0

Können Sie den vollständigen Code angeben? – Kaushal28

+0

Ich habe meine Frage @ Kaushal28 bearbeitet –

Antwort

0

Entfernen Sie einfach den ActionListener auf der Schaltfläche, bis sich etwas in der JTextArea befindet. Sie können auch Tasten deaktivieren, aber es macht die Schaltfläche etwas ausgegraut, daher bevorzuge ich die Methode zum Entfernen des ActionListener.

JButton.setEnabled(false); 
Verwandte Themen