2017-06-01 1 views
1

ma question est très simple mais je, mais je ne sais pas ce qu'il estComment ajouter un message sur un JTextArea qui disparaît quand on clique dessus ou qu'on le sélectionne?

J'ai un programme en Java, comme si

enter image description here

et je veux ajouter sur un du JTextArea court message disappeares lorsque vous cliquez dessus ou sélectionné, comme lorsque vous vous connectez sur facebook

enter image description here

le JTextArea je veux est par exemple celui ci-dessous « Démarrer ind Date: "the JTextArea s'appelle textAreaStartindDate

c'est mon code jusqu'ici. je l'ai utilisé NetBeans construit en option de conception

public class CreateEventUI extends javax.swing.JFrame { 

private static final long serialVersionUID = 7526472295622776147L; 

//GUARDAR EVENTOS EM REGISTOEVENTOS 

/** 
* Creates new form CriarEventoUI 
*/ 
public CreateEventUI() { 
    super("Create an Event"); 
    setResizable(false); 
    initComponents(); 
    setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    setVisible(true); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    labelChooseTypeOfEvent = new javax.swing.JLabel(); 
    comboBoxTypeOfEvent = new javax.swing.JComboBox<>(); 
    labelInsertTypeOfData = new javax.swing.JLabel(); 
    labelTitle = new javax.swing.JLabel(); 
    labelDescription = new javax.swing.JLabel(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    textAreaDescription = new javax.swing.JTextArea(); 
    textFieldTitle = new javax.swing.JTextField(); 
    labelStartingDate = new javax.swing.JLabel(); 
    labelStartSubmissionDate = new javax.swing.JLabel(); 
    labelEndDate = new javax.swing.JLabel(); 
    labelEndSubmissionDate = new javax.swing.JLabel(); 
    labelPlace = new javax.swing.JLabel(); 
    textFieldPlace = new javax.swing.JTextField(); 
    buttonConfirm = new javax.swing.JButton(); 
    buttonCancel = new javax.swing.JButton(); 
    textFieldStartingDate = new javax.swing.JTextField(); 
    jTextField3 = new javax.swing.JTextField(); 
    jTextField2 = new javax.swing.JTextField(); 
    jTextField4 = new javax.swing.JTextField(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    labelChooseTypeOfEvent.setText("Choose the type of event"); 

    comboBoxTypeOfEvent.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Congress" , "Exposition"})); 

    labelInsertTypeOfData.setText("insert the data"); 

    labelTitle.setText("Title"); 

    labelDescription.setText("Description"); 

    textAreaDescription.setColumns(20); 
    textAreaDescription.setRows(5); 
    jScrollPane1.setViewportView(textAreaDescription); 

    labelStartingDate.setText("Starting date:"); 

    labelStartSubmissionDate.setText("starting date for submissions:"); 

    labelEndDate.setText("End date:"); 

    labelEndSubmissionDate.setText("Ending date for submissions:"); 

    labelPlace.setText("place"); 

    buttonConfirm.setText("Confirm"); 
    buttonConfirm.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      buttonConfirmActionPerformed(evt); 
     } 
    }); 

    buttonCancel.setText("Cancel"); 
    buttonCancel.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      buttonCancelActionPerformed(evt); 
     } 
    }); 

    textFieldStartingDate.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      textFieldStartingDateActionPerformed(evt); 
     } 
    }); 

    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(25, 25, 25) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(labelInsertTypeOfData) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(labelChooseTypeOfEvent) 
        .addGap(18, 18, 18) 
        .addComponent(comboBoxTypeOfEvent, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addGroup(layout.createSequentialGroup() 
          .addComponent(labelTitle) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
          .addComponent(textFieldTitle)) 
         .addComponent(labelDescription) 
         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE) 
         .addGroup(layout.createSequentialGroup() 
          .addComponent(labelPlace) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
          .addComponent(textFieldPlace))) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(18, 18, 18) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addComponent(labelStartSubmissionDate) 
           .addComponent(labelStartingDate) 
           .addComponent(textFieldStartingDate, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)) 
          .addGap(40, 40, 40) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addComponent(labelEndDate) 
           .addComponent(labelEndSubmissionDate) 
           .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(173, 173, 173) 
          .addComponent(buttonConfirm) 
          .addGap(87, 87, 87) 
          .addComponent(buttonCancel))))) 
      .addContainerGap(13, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(28, 28, 28) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(labelChooseTypeOfEvent) 
       .addComponent(comboBoxTypeOfEvent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(23, 23, 23) 
      .addComponent(labelInsertTypeOfData) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(labelTitle) 
         .addComponent(textFieldTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(labelDescription) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(labelStartingDate) 
         .addComponent(labelEndDate)) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(52, 52, 52) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
           .addComponent(labelStartSubmissionDate) 
           .addComponent(labelEndSubmissionDate)) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
           .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
          .addComponent(textFieldStartingDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(labelPlace) 
       .addComponent(textFieldPlace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(buttonConfirm) 
       .addComponent(buttonCancel)) 
      .addContainerGap(17, Short.MAX_VALUE)) 
    ); 

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

private void buttonConfirmActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
}            

private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {            
    dispose(); 
}            

private void textFieldStartingDateActionPerformed(java.awt.event.ActionEvent evt) {              
    // TODO add your handling code here: 
}              

// Variables declaration - do not modify      
private javax.swing.JButton buttonCancel; 
private javax.swing.JButton buttonConfirm; 
private javax.swing.JComboBox<String> comboBoxTypeOfEvent; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTextField jTextField2; 
private javax.swing.JTextField jTextField3; 
private javax.swing.JTextField jTextField4; 
private javax.swing.JLabel labelChooseTypeOfEvent; 
private javax.swing.JLabel labelDescription; 
private javax.swing.JLabel labelEndDate; 
private javax.swing.JLabel labelEndSubmissionDate; 
private javax.swing.JLabel labelInsertTypeOfData; 
private javax.swing.JLabel labelPlace; 
private javax.swing.JLabel labelStartSubmissionDate; 
private javax.swing.JLabel labelStartingDate; 
private javax.swing.JLabel labelTitle; 
private javax.swing.JTextArea textAreaDescription; 
private javax.swing.JTextField textFieldPlace; 
private javax.swing.JTextField textFieldStartingDate; 
private javax.swing.JTextField textFieldTitle; 
// End of variables declaration     

}

sans utiliser l'évidence

+0

Pouvez-vous également ajouter du code? –

+0

recherche de filigrane textfield/textarea ou [champ de texte avec le message par défaut] (https://stackoverflow.com/questions/14916255/how-to-display-a-default-message-in-jtextfield-java) – matoni

+0

JTextArea ou JTextField (ou les deux)? – Pshemo

Répondre

0

En général, vous pouvez utiliser un ActionListener ou MouseListener. Pour vous n'avez pas publié de code, nous ne pouvons pas vous aider exactement. Mais peut-être que ce message aide: Onclick for TextField

+0

J'ai déjà édité avec le code, désolé je veux éviter le .setText évident ("") quand je sélectionne le JTextArea – skidils

+0

Donc, si .setText ("") est trop évident, que voulez-vous exactement? –

0

Ajoutez un texte par défaut lors de l'initialisation de JTextArea. Puis ajoutez un FocusListener. comme ci-dessous:

textAreaDescription = new javax.swing.JTextArea("Description"); 
//jScrollPane1.setViewportView(textAreaDescription); 
textAreaDescription.addFocusListener(new FocusListener() { 
    public void focusGained(FocusEvent e) { 
     textAreaDescription.setText(""); 
    } 

    public void focusLost(FocusEvent e) { 
     textAreaDescription.setText("Description"); 
    } 
});