[JAVA + NETBEANS] "disobedient" buttons :)

6 views
Skip to first unread message

Mariano

unread,
Dec 8, 2006, 5:44:20 AM12/8/06
to
Then i make a DB query on one columnm now I've maked 4 buttons to
browse the result set, First, Previous, Next, Last.

If I click button "last", i've to show the last record of rs, so it can
usefull to disable the last button and next button -
last.setEnabled(false) and next.setEnabled(false) -., similar discours
must be happen if i've reach the first record.

All seems to work fine, the buttons are disabled (grey color) when it's
need, but if i go with mouse over the disabled button, it will take its
usual color. How can I resolve it?

this is a part of code:
________________________________________________________
public void setModalita(int modo) {
modalita=modo;
switch (modo)
{
case EORS:
/*case end of rs, buttons: last and next disabled,
previous and first enabled*/
first.setEnabled(true);
previous.setEnabled(true);
next.setEnabled(false);
last.setEnabled(false);
break;
[...]
}
}

[...]

// HANDLER BOTTON "LAST"
// if i click last, will reach end of rs so disable next and last
private void lastActionPerformed(java.awt.event.ActionEvent evt) {

try {
if (!rs.isLast()) {
rs.last();
edit.setText(rs.getString("Descrizione"));
setModalita(EORS);
}
} catch (Exception e) {
System.out.println("Error: "+e.getMessage());
}
}
________________________________________________________

what is wrong??

Andrew Thompson

unread,
Dec 8, 2006, 7:59:56 AM12/8/06
to
Mariano wrote:

Sub: [JAVA + NETBEANS] "disobedient" buttons :)

Notes:

1) that your own web-interface to usenet cannot
properly format thread subjects containing '[' apparently..
<http://groups.google.com/group/comp.lang.java.programmer/msg/3f015a49109f7641>
..so it is probably best to avoid using them.

2) What a load of old cobblers, even jokingly.
You should learn how to use Java before you go
blaming the GUI components.

(snip)
> ...How can I resolve it?


>
> this is a part of code:

I doubt it is the correct part - I always recommend
SSCCE's, which we can compile and run....

> what is wrong??

Here is an SSCCE showing a button acting
exactly as I would expect it to.

You might try.. removing every part of your program,
until it either equals my program, or works, then the
last line you remove was the problem, Or..
Submtiting your own SSCCE that actually compiles
to demonstrate the problem you are seeing.

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DisabledButtons {
public static void main(String[] args) {
JFrame f = new JFrame("Disabled Button");
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

Container c = f.getContentPane();
c.setLayout( new GridLayout(0,1) );

final JButton b1 = new JButton("Button 1");
c.add(b1);

final JCheckBox cb1 = new
JCheckBox("Checkbox 1", true);
cb1.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
b1.setEnabled( cb1.isSelected() );
}
} );
c.add( cb1 );

f.pack();
f.setVisible(true);
}
}
</sscce>

More information on the SSCCE..
<http://www.physci.org/codes/sscce>

Andrew T.

Mariano

unread,
Dec 8, 2006, 8:08:33 AM12/8/06
to
ty, for the suggestion, but i don't the right time to learn java, i've
make a DB project for the univeristy, with java I've to make only the
client...

I put my complete code (some name are in italian language, but i think
u should not have many problems):
=================================================
/*
* mask.java
*
* Created on 28 novembre 2006, 18.24
*/

package reprise;
import java.sql.*;
import javax.swing.JTable;

/**
*
* @author Administrator
*/
public class mask extends javax.swing.JFrame {

/** Creates new form mask */
public mask() {
initComponents();
}
final static public int AVVIO=0;
final static public int INIZIO=1;
final static public int MEDIO=2;
final static public int FINE=3;
private int modalita;

public void setModalita(int modo) {
modalita=modo;
switch (modo)
{

case AVVIO:
primo.setEnabled(false);
precedente.setEnabled(false);
ultimo.setEnabled(false);
successivo.setEnabled(false);
break;
case INIZIO:
primo.setEnabled(false);
precedente.setEnabled(false);
ultimo.setEnabled(true);
successivo.setEnabled(true);
break;
case MEDIO:
primo.setEnabled(true);
precedente.setEnabled(true);
ultimo.setEnabled(true);
successivo.setEnabled(true);
break;
case FINE:
primo.setEnabled(true);
precedente.setEnabled(true);
ultimo.setEnabled(false);
successivo.setEnabled(false);
break;
}
}

Connection con;
Statement st;
String query, conString;
ResultSet rs;

/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {
connetti = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
edit = new javax.swing.JTextField();
primo = new javax.swing.JButton();
precedente = new javax.swing.JButton();
successivo = new javax.swing.JButton();
ultimo = new javax.swing.JButton();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
connetti.setText("INVIA");
connetti.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
connettiMouseClicked(evt);
}
});

jLabel1.setText("Clicca Invia per eseguire la query!");

jButton2.setText("ESCI");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});

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

primo.setText("|<");
primo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
primoActionPerformed(evt);
}
});

precedente.setText("<");
precedente.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
precedenteActionPerformed(evt);
}
});

successivo.setText(">");
successivo.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
successivoActionPerformed(evt);
}
});

ultimo.setText(">|");
ultimo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
ultimoActionPerformed(evt);
}
});

org.jdesktop.layout.GroupLayout layout = new
org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(connetti,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(jLabel1,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(jScrollPane1,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(edit,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(primo)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(precedente)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(successivo)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(ultimo)))
.addContainerGap())
);
layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(17, 17, 17)
.add(connetti)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel1)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(edit,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(primo)
.add(precedente)
.add(ultimo)
.add(successivo)
.add(jButton2))

.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
pack();
}// </editor-fold>

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

try {
if (!rs.isFirst()) {
rs.first();
edit.setText(rs.getString("Descrizione"));
setModalita(INIZIO);
}
} catch (Exception e) {
System.out.println("Errore: "+e.getMessage());
}
}

private void ultimoActionPerformed(java.awt.event.ActionEvent evt)


{
try {
if (!rs.isLast()) {
rs.last();
edit.setText(rs.getString("Descrizione"));

setModalita(FINE);
}
} catch (Exception e) {
System.out.println("Errore: "+e.getMessage());
}
}

private void precedenteActionPerformed(java.awt.event.ActionEvent
evt) {
try {
if (!rs.isFirst()) {
rs.previous();
edit.setText(rs.getString("Descrizione"));
setModalita(MEDIO);
}
} catch (Exception e) {
System.out.println("Errore: "+e.getMessage());
}
}

private void successivoActionPerformed(java.awt.event.ActionEvent
evt) {
try {
if (!rs.isLast()) {
rs.next();
edit.setText(rs.getString("Descrizione"));
setModalita(MEDIO);
}
} catch (Exception e) {
System.out.println("Errore: "+e.getMessage());
}
}

private void connettiMouseClicked(java.awt.event.MouseEvent evt) {

initComponents();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conString="jdbc:odbc:localhost";
con=DriverManager.getConnection(conString, "mariano",
"120384");
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
query="select * from articolo order by codart ASC";
rs = st.executeQuery(query);
rs.first();
edit.setText(rs.getString("Descrizione"));
setModalita(INIZIO);
} catch (Exception e) {
System.out.println("Errore: "+e.getMessage());
}
}


private void jButton2MouseClicked(java.awt.event.MouseEvent 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 mask().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton connetti;
private javax.swing.JTextField edit;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JButton precedente;
private javax.swing.JButton primo;
private javax.swing.JButton successivo;
private javax.swing.JButton ultimo;
// End of variables declaration

}
=================================================

Andrew Thompson

unread,
Dec 8, 2006, 8:17:03 AM12/8/06
to
Mariano wrote:
> ty, for the suggestion, but i don't the right time to learn java, ...

Have you considered hiring somebody who does
(have the time, or preferably - already knows it)?

Andrew T.

Mariano

unread,
Dec 8, 2006, 8:22:21 AM12/8/06
to

Andrew Thompson ha scritto:

it's a way to say that u don't want help me?
I've the exam in the next week, i can't learn java at my best.

Bart Cremers

unread,
Dec 8, 2006, 9:43:53 AM12/8/06
to

On Dec 8, 2:08 pm, "Mariano" <mariano.calan...@gmail.com> wrote:
> ty, for the suggestion, but i don't the right time to learn java, i've
> make a DB project for the univeristy, with java I've to make only the
> client...
>

>From what I can see, the code *should* work, but I think there's a
problem with using the GroupLayout manager. I've got no clue on how it
should be used, so I can't provide you with a fix.

To prove my point, just resize the frame whenever you expect disabled
buttons and you'll see.

Regards,

Bart

Reply all
Reply to author
Forward
0 new messages