Im new to java swing, and Ive got a little problem. So I've got a class Flight.java. In there I have a method displaySeat2D(). First I had this done with the scanner. Now I have to use swing. So basically I made text fields to take in number of rows and seats. Now Im trying to display this in my JPanel. Not realy sure how to display this. Id try to keep it simple and display it like in Eclipse console with number "0".
For example if I enter 4 rows and 4 seats in a row It would be displayed like:

0000
0000
0000
0000

This is the code.

In eclipse console the method and and display work fine.
FLIGHT class

public void displaySeat2D(){

for (int i = 0; i < arraySeatPassenger.length; i++) {line
    System.out.println("");
         for (int j = 0; j < arraySeatPassenger[i].length; j++) {// seat
                if (arraySeatPassenger[i][j]== null) {
                     System.out.print("0");//
                } else {
                    System.out.print("1");
                }
            }
        }System.out.println("");

    }

Now that Im working with swing I have to make some UI that accepts rows and seats in a row. I made this already
UserInterface class User interface example

btnConfirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                flight.setRowNr(Integer.parseInt(tf_SetRows.getText()));
                flight.setSeatNr(Integer.parseInt(tf_SetSeats.getText()));
                flight.setaArraySeatPassenger(new Passenger[flight.getRowNr()][flight.getSeatNr()]);
                lblDisplay.setText("U have seats left: "+String.valueOf(flight.getRowNr()*flight.getSeatNr()));
            System.out.println();
            }
        });

So what I dont know now is, how to display it like in Eclipse console. When I press on a button Id like this public void displaySeat2D() method to display it like I wrote it, up there.

This ofcourse displays it in eclipse console. So the question from my side is how to display it in some TextArea?

btnSeatDisplay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){

                flight.displaySeat2D();

            }
        });

Tnx for your help in advance Im new to Swing and Java so I apologize for such a silly question.

Recommended Answers

All 8 Replies

to display you don't need a returnValue, you just need to call the setText method of that JTextArea.

Tnx for responding. The problem here is.. how to display flight.displaySeat2D(); in my JPanel. If I do it like labeldisplay.setText(flight.displaySeat2D()); It obviously doesnt work because it wants me to set the return type to string.

ah, ok, I missunderstood. well, that is pretty normal, wouldn't you agree? you are trying to show something, you have to tell that JTextArea what it is you want to show. you say to the setText method: I want you to take the result of the displaySeat2D as text to print, but you don't have any result, since it's return type is void. either you change that method, or you add a second one.

public String displaySeat2D(){
String returnValue = "";
for (int i = 0; i < arraySeatPassenger.length; i++) {line
    returnValue += "/n";
         for (int j = 0; j < arraySeatPassenger[i].length; j++) {// seat
                if (arraySeatPassenger[i][j]== null) {
                     returnValue +="0";
                } else {
                    returnValue+="1";
                }
            }
        }returnValue+="/n";

        return returnValue;
    }

would be something like that, I guess ..

JTextArea.append() ???, not clear from description(s), answers, nor from code

that's not his problem. he's trying:

myTextArea.setText(thisMethodHasVoidAsReturnType());

which quite normally doesn't work.

stultuske's idea is ok.Tnx.. I just need to make rows separate so it writes them in a new row. Cuz now its like 0000/n0000/n0000/n. When it should be like if its 4x4.
0000
0000
0000
0000

use JList (When it should be like if its 4x4. == JTable for 2D array???) without lines for a.m. structure

Sry mKorbel dont quite understand what ur trying to say with this JList. Its 2D array cuz its accepts seats and rows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.