0

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!

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Sounds very logical.. im fraid however, that it doesnt stretch properly if i maximise the application. is this the case? And my excuses for not properly reading the documentation about JTextArea and its properties. I will try both options right now. See what works. thanks :)
I have just succesfully tried out what you suggested. I think my mind was too fixated, coming from C# that gui building is alot harder, but its surprisingly easy. Thank you very much.
If you put the JTextArea in the south location of the BorderLayout it will not resize - only the center component resizes. You either have to use a different layout manager or a JSplitPane if the user should be able to resize.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.