-
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 !!!!
-
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.
-
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
-
Re: Button disable
Call that method before calling the setVisible() method for the frame.
-
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
-
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);
Quote:
i tried it giving error
Please copy and paste here the full text of the error message.
-
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 :)