简体   繁体   中英

Java Swing GUI alignment problem

Greetings,

I am learning rapidly about Java, however something I can really break my leg over is Java GUI programing. coming from an easy life of C#, this is a complete challenge to get my UI working. I know absolute layouts are out the question here.

I am facing the following problem.

In my content pane(BorderLayout) I have a JScrollPanel with a JTable and a menu on the top of the application.

The problem is, I want to add a component to the south of the border layout(JScrollPane is attached to center), and I want to add a scrollable JTextArea to it.

My tought was, add a new JPanel, give it a layout(dont know which one is best in this case.. suggestions are welcome) add a JScrollpanel to that panel and ontop of that add a JTextArea. I have tried this and graphically wise, it worked. However, as clumsy as it MIGHT be programmed, the JTextArea is only big enough in height much like a JTextField.

I want it to be ATLEAST 4 lines high and scrollable. Here is my GUI code:

setTitle("Game Manager");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 620, 498);


    JMenuBar mbMainMenu = new JMenuBar();
    setJMenuBar(mbMainMenu);

    JMenu mnFile = new JMenu("File");
    mbMainMenu.add(mnFile);
    contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JScrollPane scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.CENTER);

    tblGames = new JTable(new GamesTableModel(mainController.retrieveGames()));
    tblGames.setShowVerticalLines(true);
    tblGames.setShowHorizontalLines(true);
    tblGames.setPreferredScrollableViewportSize(new Dimension(500, 70));
    tblGames.setFillsViewportHeight(true);
    tblGames.setAutoCreateRowSorter(true);
    tblGames.getTableHeader().setReorderingAllowed(false);
    scrollPane.setViewportView(tblGames);

So underneath the JTable, I want to have a JtextArea in a JScrollPane that is more then one line high.

I am at a complete loss for a fix on this.

Any tips are welcome, not just on the gui itself, also on naming conventions.

This project is just for self teaching, cause our school wants to teach C# rather then Java. Java feels more natural to me then C#.. It types away more nicely in my opinion.. enough about the dabbling.

Please leave your comments/tips/answer ^^

Your help is highly appreciated!

SOLVED

solution:

    JScrollPane gameDescriptionScrollPane = new JScrollPane();
contentPane.add(gameDescriptionScrollPane, BorderLayout.SOUTH);

taGameDescription = new JTextArea();
taGameDescription.setRows(4);

Thanks : mklhmnn

Lesson of the day: Read Documentation carefully, not just eye over it and think youve read it!

You have two options:

  • create the JTextArea with the preferred column or row count or
  • set the minimum and preferred size of the surrounding JScrollPane

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM