Fala Galera,
Estou precisano fazer algumas alterações num codigo aqui, e não estou conseguindo deixar os botões (configureSensor1Button, configureSensor2Button e openPortsButton)
Preciso que eles ficam desabilitado quando nos metodos:
- getConfigurationOne() e getConfigurationTwo() a a serialPortName forem iguais
Segue abaixo o codigo:
package br.com.inclinare.presentation;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import br.com.inclinare.communication.SerialCommunication;
import br.com.inclinare.data.BaudRate;
import br.com.inclinare.data.DataBits;
import br.com.inclinare.data.Parity;
import br.com.inclinare.data.SerialConfiguration;
import br.com.inclinare.data.StopBits;
public class ConfigurePortsDialog extends JDialog {
class ConfigurePortsPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = -419570781909658L;
private JComboBox baudRateCombo;
private JComboBox dataBitsCombo;
private JComboBox parityCombo;
private JComboBox serialPortCombo;
private JComboBox stopBitsCombo;
public ConfigurePortsPanel(List<String> serialPortsAvaliables) {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel label1 = new JLabel("Input Port:");
c.gridx = 0;
c.gridy = 0;
add(label1, c);
serialPortCombo = new JComboBox(serialPortsAvaliables.toArray());
serialPortCombo.setSelectedIndex(0);
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 0;
add(serialPortCombo, c);
c.gridwidth = 1;
JLabel label2 = new JLabel("Port Config:");
c.gridx = 0;
c.gridy = 1;
add(label2, c);
JLabel label3 = new JLabel("Baud Rate:");
c.gridx = 1;
c.gridy = 2;
add(label3, c);
baudRateCombo = new JComboBox(BaudRate.values());
baudRateCombo.setSelectedIndex(5);
c.gridx = 2;
c.gridy = 2;
add(baudRateCombo, c);
JLabel label4 = new JLabel("Parity:");
c.gridx = 1;
c.gridy = 3;
add(label4, c);
parityCombo = new JComboBox(Parity.values());
parityCombo.setSelectedIndex(0);
c.gridx = 2;
c.gridy = 3;
add(parityCombo, c);
JLabel label5 = new JLabel("Data Bits:");
c.gridx = 1;
c.gridy = 4;
add(label5, c);
dataBitsCombo = new JComboBox(DataBits.values());
dataBitsCombo.setSelectedIndex(3);
c.gridx = 2;
c.gridy = 4;
add(dataBitsCombo, c);
JLabel label6 = new JLabel("Stop Bits:");
c.gridx = 1;
c.gridy = 5;
add(label6, c);
stopBitsCombo = new JComboBox(StopBits.values());
stopBitsCombo.setSelectedIndex(0);
c.gridx = 2;
c.gridy = 5;
add(stopBitsCombo, c);
JLabel espace = new JLabel(" ");
c.gridx = 0;
c.gridy = 6;
add(espace, c);
}
public JComboBox getBaudRateCombo() {
return baudRateCombo;
}
public JComboBox getDataBitsCombo() {
return dataBitsCombo;
}
public JComboBox getParityCombo() {
return parityCombo;
}
public JComboBox getSerialPortCombo() {
return serialPortCombo;
}
public JComboBox getStopBitsCombo() {
return stopBitsCombo;
}
}
/**
*
*/
private static final long serialVersionUID = 8419262389512118580L;
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
JFrame frame = new JFrame("OS1x00 Digital Compass");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = frame.getContentPane();
container.setLayout(new BorderLayout());
final ConfigurePortsDialog configurePorts = new ConfigurePortsDialog(frame,
SerialCommunication.getSerialPortNames());
configurePorts.pack();
configurePorts.setLocationRelativeTo(frame);
final ConfigureSensorDialog configureSensor = new ConfigureSensorDialog(frame);
configurePorts.pack();
configurePorts.setLocationRelativeTo(frame);
final DataDialog dataDialog = new DataDialog(frame);
dataDialog.pack();
dataDialog.setLocationRelativeTo(frame);
JButton configureButton = new JButton("Configure Ports");
container.add(configureButton, BorderLayout.NORTH);
configureButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
configurePorts.setVisible(true);
}
});
JButton openPortsButton = new JButton("Open Ports");
container.add(openPortsButton, BorderLayout.SOUTH);
openPortsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataDialog.setSerialConfigurations(configurePorts
.getConfigurationOne(), configurePorts
.getConfigurationTwo());
dataDialog.setVisible(true);
}
});
JButton configureSensor1Button = new JButton("Configure Sensor 1");
container.add(configureSensor1Button, BorderLayout.WEST);
configureSensor1Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
configureSensor.setSerialConfiguration(configurePorts
.getConfigurationOne());
configureSensor.setVisible(true);
}
});
JButton configureSensor2Button = new JButton("Configure Sensor 2");
container.add(configureSensor2Button, BorderLayout.EAST);
configureSensor2Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
configureSensor.setSerialConfiguration(configurePorts
.getConfigurationTwo());
configureSensor.setVisible(true);
}
});
// Display the window.
frame.setSize(800, 600);
frame.pack();
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private ConfigurePortsPanel panel1;
private ConfigurePortsPanel panel2;
public ConfigurePortsDialog(JFrame frame, List<String> serialPortsAvaliables) {
super(frame, true);
setTitle("Setup");
// Handle window closing correctly.
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setSize(600, 800);
setResizable(false);
initializeComponents(serialPortsAvaliables);
/*if (getConfigurationOne().getSerialPortName() == getConfigurationTwo().getSerialPortName())
configureSensor1Button.setEnabled(false);
else
configureSensor1Button.setEnabled(true);*/
}
/** This method clears the dialog and hides it. */
public void clearAndHide() {
setVisible(false);
}
public SerialConfiguration getConfigurationOne() {
return new SerialConfiguration((String) panel1.getSerialPortCombo()
.getSelectedItem(), (BaudRate) panel1.getBaudRateCombo()
.getSelectedItem(), (Parity) panel1.getParityCombo()
.getSelectedItem(), (DataBits) panel1.getDataBitsCombo()
.getSelectedItem(), (StopBits) panel1.getStopBitsCombo()
.getSelectedItem());
}
public SerialConfiguration getConfigurationTwo() {
return new SerialConfiguration((String) panel2.getSerialPortCombo()
.getSelectedItem(), (BaudRate) panel2.getBaudRateCombo()
.getSelectedItem(), (Parity) panel2.getParityCombo()
.getSelectedItem(), (DataBits) panel2.getDataBitsCombo()
.getSelectedItem(), (StopBits) panel2.getStopBitsCombo()
.getSelectedItem());
}
private void initializeComponents(List<String> serialPortsAvaliables) {
final JPanel panel = new JPanel(new GridBagLayout());
final GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
panel1 = new ConfigurePortsPanel(serialPortsAvaliables);
c.gridx = 0;
c.gridy = 0;
panel.add(panel1, c);
panel2 = new ConfigurePortsPanel(serialPortsAvaliables);
c.gridx = 0;
c.gridy = 1;
panel.add(panel2, c);
JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (panel1.getSerialPortCombo().getSelectedIndex() == panel2
.getSerialPortCombo().getSelectedIndex()) {
JOptionPane.showMessageDialog(ConfigurePortsDialog.this,
"The serial port can't be the same.", "Try again",
JOptionPane.ERROR_MESSAGE);
} else {
clearAndHide();
}
}
});
c.gridx = 0;
c.gridy = 2;
panel.add(okButton, c);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearAndHide();
}
});
c.gridx = 1;
c.gridy = 2;
panel.add(cancelButton, c);
// Make this dialog display it.
setContentPane(panel);
}
}
Abraços
Danilo