I'm learning in Java, and I'm trying do this settext work, but I'm not getting
public static void main(String args[]) throws Exception { try { (new Visual()).connect("COM4", "1"); (new Visual()).connect("COM6", "2"); } catch ( Exception e ) { e.printStackTrace(); } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Visual().setVisible(true); } }); }
void connect ( String portName, String linha ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
status_porta2.setText(portName);
.....
}
}
}
I'm searching change a label text in this function, but isn't working.. What are happening? Thanks
The problem is, you are creating multiple instances of Visual
, two which you open a connection and one you show on the screen.
None of these have any connection to each other, meaning what's on the screen isn't what's being managed/manipulated.
Instead, the instance you use to connect to the port should be the instance you show...
For example...
public static void main(String args[]) throws Exception
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Visual v1 = new Visual();
v1.connect("COM4", "1");
v1.setVisible(true);
Visual v2 = new Visual();
v2.connect("COM6", "2");
v2.setVisible(true);
} catch ( Exception e ) {
e.printStackTrace();
}
}
});
}