Search code examples
javauser-interfacejpaneljtextarea

JTextArea & JPanel different classes one window...is it possible?


So i have two classes:

public class TimeServer extends JPanel implements TimeVariables
{

public TimeServer() 
{

    JTextArea serverLog = new JTextArea();
    // Create a scroll pane to hold text area
    JScrollPane scrollPane = new JScrollPane(serverLog);

    JPanel serverWin = new JPanel();

    serverWin.add(scrollPane);

}

and another class that has:

private JPanel mainWin = new JPanel();
private JPanel gridArea = new JPanel(); //hold cells

TimeServer serverWin = new TimeServer();

public class TimClient extends JFrame implements Runnable, TimeVariables 
{
    his.add(mainWin, BorderLayout.CENTER);
    mainWin.setLayout(new BorderLayout());
   // mainWin.setLayout(new GridLayout(1,2));

    //Create Grid
    gridArea.setLayout(new GridLayout(10, 10, 2, 2));

    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
            gridArea.add(cell[i][j] = new Cell(i, j, this));

    gridArea.setBorder(new LineBorder(Color.black, 1));

    jlblTitle.setHorizontalAlignment(JLabel.CENTER);
    jlblTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
    jlblTitle.setBorder(new LineBorder(Color.black, 1));
    jlblStatus.setBorder(new LineBorder(Color.black, 1));

    mainWin.add(gridArea, BorderLayout.CENTER);
    mainWin.add(serverWin, BorderLayout.PAGE_END);
    // Place the panel and the labels to the frame

    setLayout(new BorderLayout()); // implicit anyway

    add(jlblTitle, BorderLayout.NORTH);
    add(mainWin, BorderLayout.CENTER);
    add(jlblStatus, BorderLayout.SOUTH);
}

public static void main(String[] args) {

    // Create a frame
    TimClient frame = new TimClient("Time Client");
    //frame.getContentPane().add(BorderLayout)

    // Display the frame
    frame.setSize(620, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

all i want to do is keep the server log and grid in the JPanel next to one another, could you explain how?

also how do i add the JPanel in the server class to the one in client?

Here's a sketch of how i'd like it to look: sketch


Solution

  • Your problems above appear to stem from a bad Swing code practice, one that seems to be reinforced by Swing code generators (although I'm not sure if you're currently using this tool) and the official Swing tutorials, and that is:

    • First and foremost, you should avoid having your Swing GUI classes extend JFrame as that unnecessarily paints your GUI code into a corner that requires a bit of effort to get out of.
    • Instead gear your Swing GUI code towards making JPanels, panels which now can easily be placed into other JPanels, or into JFrames, JDialogs, JOptionPanes, swapped in CardLayouts,... wherever they are needed.
    • Instead, create, fill, and pack your JFrames when they are needed.

    So I suggest that you do just this:

    • change your code above so that the classes do not extend JFrames, and instead create JPanels,
    • Create a master JPanel that uses a BorderLayout
    • Add your grid to the above, BorderLayout.CENTER
    • Add your JTextArea-containing JScrollPane and its JPanel into the master JPanel in the BorderLayout.PAGE_END position.
    • Create your JFrame to hold and display the master JPanel.

    If you've done a good job of separated your GUI code from your logic code, then it should be easy to re-write the GUI code while keeping your same logic (or "model") code.


    Edit 2
    Regarding your changed question where now the first class extends JPanel, simply add that JPanel to your JFrame in the BorderLayout.LINE_END end (also known as BorderLayout.EAST) position.