Retrieve a Jcombo content in another class

Asked

Viewed 23 times

0

I need to capture the content of this Jcombo that will have door (com1, com2. etc) for in the other class print set port for printing. I’m not getting.

public class Form1 extends javax.swing.JFrame {
 Imprimir imprimir;

 JComboBox JComboPorta;
/**
 * Creates new form Form1
 */
public Form1() {
    initComponents();


public JComboBox getJComboPorta() {
    return JComboPorta;
}

/// inside the Initcomponenst... initializes with the doors jComboPorta.setModel(newDefaultComboBoxModel(SerialPortList.getPortNames()));

and in the Print how-to class?

public class Imprimir {


public void imprimir_cheque() throws InterruptedException
{
         SerialPort serialPort;
         Form1 form1;
 try

    {
        form1.jComboPorta.getSelectedItem();????

        serialPort = new SerialPort ("COM3");

1 answer

0

If they are distinct classes, that is, they are in different files, and it is not a static element, you will need an instance of the class Form1 in Imprimir to access the component.

Assuming you enter and use the class Imprimir within the Jframe class, you could be able to access the component through the method getJComboPorta() that you created by simply changing the signature of the method as below:

public void imprimir_cheque(Form1 form1) throws InterruptedException
{
         SerialPort serialPort;
         //Form1 form1; <-- desnecessário agora
 try

    {
        Object o = form1.getJComboPorta().getSelectedItem();

        serialPort = new SerialPort ("COM3");

And when it comes to calling this method within the class Form1, you will pass the very instance of the screen, through the this:

imprimir.imprimir_cheque(this);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.