Recuper @ mac d'un pc et afficher dans jtext field

Résolu
zina - 11 sept. 2013 à 14:03
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 - 12 sept. 2013 à 15:26
bonjour
je suis débutant en java
j'ai une classe qui recuperer @ mac d'un ordinateur
je voulais afficher cette @ dans jtextfield d'une fenetre
voila la classe @ mac
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package macadresse;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
/**
*
* @author NewMedia
*/
public class MacAdresse {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// Obtenir l'adresse IP de la machine locale
InetAddress address = InetAddress.getLocalHost();

NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();

// Afficher l'adresse Mac
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : "");

}
}
}
et la classe frame
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package macadresse;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;

/**
*
* @author NewMedia
*/
public class NewJFrame extends javax.swing.JFrame {

/**
* Creates new form NewJFrame
*/
public NewJFrame() {

initComponents();
}

/**
* 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() {

jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
jColorChooser1 = new javax.swing.JColorChooser();
jColorChooser2 = new javax.swing.JColorChooser();
jColorChooser3 = new javax.swing.JColorChooser();
jColorChooser4 = new javax.swing.JColorChooser();
ADRESS = new javax.swing.JPanel();
adr_mac = new javax.swing.JTextField();
code_mac = new javax.swing.JTextField();
ok = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
annuler = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();

jMenu1.setText("jMenu1");

jMenu2.setText("jMenu2");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

ADRESS.setForeground(new java.awt.Color(0, 153, 204));

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

code_mac.setText("CODE ACCEE");
code_mac.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
code_macActionPerformed(evt);
}
});

ok.setText("ENTRER");
ok.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
okMouseEntered(evt);
}
});

jLabel1.setText("CODE ");

jLabel2.setText("CODE ACCEE");

annuler.setText("RESET");
annuler.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
annulerMouseClicked(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
annulerMouseExited(evt);
}
});

jLabel3.setText("BIENVENU MON CLIENT");

javax.swing.GroupLayout ADRESSLayout = new javax.swing.GroupLayout(ADRESS);
ADRESS.setLayout(ADRESSLayout);
ADRESSLayout.setHorizontalGroup(
ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ADRESSLayout.createSequentialGroup()
.addGap(102, 102, 102)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ADRESSLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(ADRESSLayout.createSequentialGroup()
.addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(annuler, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(ADRESSLayout.createSequentialGroup()
.addGroup(ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(code_mac, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(adr_mac, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(40, 40, 40))
);
ADRESSLayout.setVerticalGroup(
ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ADRESSLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3)
.addGap(12, 12, 12)
.addGroup(ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(adr_mac, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(code_mac, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(ADRESSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ok, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(annuler, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31))
);

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

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

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

}

private void adr_macActionPerformed(java.awt.event.ActionEvent evt) {
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : "");

}

MacAdresse a=new MacAdresse();
if (evt.getSource()==a)
{
String s1 = adr_mac.getText();
}
} catch (UnknownHostException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SocketException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}







}

private void annulerMouseExited(java.awt.event.MouseEvent evt) {
System.exit(0);
}

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

}

private void okMouseEntered(java.awt.event.MouseEvent evt) {
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : "");

}

MacAdresse a=new MacAdresse();
if (evt.getSource()==a)
{
String s1 = adr_mac.getText();
}
} catch (UnknownHostException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SocketException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

try {
// select Look and Feel
UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
// start application
new NewJFrame();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
catch (Exception ex) {
ex.printStackTrace();

} //* 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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */

}
// Variables declaration - do not modify
private javax.swing.JPanel ADRESS;
private javax.swing.JTextField adr_mac;
private javax.swing.JButton annuler;
private javax.swing.JTextField code_mac;
private javax.swing.JColorChooser jColorChooser1;
private javax.swing.JColorChooser jColorChooser2;
private javax.swing.JColorChooser jColorChooser3;
private javax.swing.JColorChooser jColorChooser4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JButton ok;
// End of variables declaration
}
je voulais une aide !!!! merci

5 réponses

BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 103
11 sept. 2013 à 16:25
Hello,
Merci d'éditer ton message afin de rajouter les balises code pour plus de lisibilité.
Merci également de nous préciser ton problème, ton point bloquant,... Parce là, en l'état, je ne sais pas quoi te dire de plus
0
zaynoubagl Messages postés 58 Date d'inscription mercredi 11 septembre 2013 Statut Membre Dernière intervention 19 octobre 2014
11 sept. 2013 à 16:31
je suis entraine de securiser mon application java
la methode et de recuperer l'adresse mac et convertir en decimale puis afficher dans jtextfield et l'utilisateur demande l'autre code (@ mac en decimale) puis aprés la vrifiaction je lance execution de l'application
0
zaynoubagl Messages postés 58 Date d'inscription mercredi 11 septembre 2013 Statut Membre Dernière intervention 19 octobre 2014
11 sept. 2013 à 16:33
package macadresse;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

public class MacAdresse {

public static void main(String[] args) throws Exception {
// Obtenir l'adresse IP de la machine locale
InetAddress address = InetAddress.getLocalHost();

NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();

// Afficher l'adresse Mac
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : "");

}
}
}
0
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 103
11 sept. 2013 à 16:40
Euh...c'est quoi que tu n'as pas compris dans ce que j'ai écris?
0
zaynoubagl Messages postés 58 Date d'inscription mercredi 11 septembre 2013 Statut Membre Dernière intervention 19 octobre 2014
11 sept. 2013 à 16:47
adr_mac.setText ( adr_mac.getText () + System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""));
ne retourne pas @ mac
elle affiche
java.io.PrintStream@cb36fa dans le jtextfield
0
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 103
11 sept. 2013 à 17:00
Après un rapide test, ceci fonctionne chez moi:

import java.net.InetAddress;
import java.net.NetworkInterface;

public class MacAdress {

    /**
     * @param args
     */
    public static void main(final String[] args) {

        try {
            // Obtenir l'adresse IP de la machine locale
            final InetAddress address = InetAddress.getLocalHost();

            final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            final byte[] mac = ni.getHardwareAddress();

            // Afficher l'adresse Mac
            for (int i = 0; i < mac.length; i++) {
                System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : "");
            }
        } catch (final Exception e) {
            System.out.println("exception");
        }
    }
}
0
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 103
Modifié par BunoCS le 11/09/2013 à 17:18
Ceci devrait fonctionner:
try {
    final InetAddress address = InetAddress.getLocalHost();
    final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
    final byte[] mac = ni.getHardwareAddress();

    final StringBuilder sb = new StringBuilder("");
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X", mac[i]));
    }

    adr_mac.setText(sb.toString());

} catch (final Exception e) {
     // todo
}


@+
Buno, Modo CS-CCM
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
zaynoubagl Messages postés 58 Date d'inscription mercredi 11 septembre 2013 Statut Membre Dernière intervention 19 octobre 2014
11 sept. 2013 à 17:26
merci bcp je suis debutant en java merci merci
ca fonctionne mais j'ai un autre probleme quant je voulais transformer cette chaine hexadecimale en decimal
elle prend sauf les 7 premier numero
voici la classe
902B3478E864
elle faire la transformation sauf pour
902B347
//////////////////////
package macadresse;

import java.io.*;
import java.lang.*;

public class HexadecimalToDecimal{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Hexadecimal number:");
String str= bf.readLine();
int i= Integer.parseInt(str, 32);

System.out.println("Decimal:="+ i);
}
}
//////////////////////////////
j
0
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 103
12 sept. 2013 à 09:20
Hello,
0o...l'hexa, c'est une base 16 et non 32 ;)

int i= Integer.parseInt(str, 16); // devrait corriger le souci

0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 103
12 sept. 2013 à 15:26
N'hésites à marquer la discussion comme résolue si c'est le cas, merci ;)
0
Rejoignez-nous