0

I am new to the entire java thing and I have been attempting to make a window with 2/3 of the window being a text box and the other 3rd being an empty JPanel (For later use), but I am unsure how to execute this. I have tried Gridlayout but noticed you are unable to specify how many columns a component can take up. I don't really mind how it is done, but could someone please help me with this?

I have searched multiple other posts such as this one which had a very similar concept but got an answer that I have tried but ended in a product I didn't enjoy: Two JPanels side by side in a JFrame with one JPanel having a fixed width, What I would prefer is something like this which is locked to 2/3's on the left and 1/3 on the right: Picture of TextPanel and JPanel

Here is what I have attempted:

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class GUI {
    public static final int sizeX = 1280;
    public static final int sizeY = 680;
    public static JTextPane textBox;

    public static JPanel createGui() {
        //Create panels
        JPanel mainPanel = new JPanel();
        JPanel textPanel = new JPanel();
        JPanel inventoryPanel = new JPanel();

        //Setup panel details
        mainPanel.setPreferredSize(new Dimension(sizeX, sizeY));
        textPanel.setBackground(Color.BLACK);
        inventoryPanel.setBackground(new Color(71, 22, 22));

        //Create custom font
        try {
            Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File("TheDungeonDungeon/resources/font.ttf")).deriveFont(10f);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(customFont);

            textBox = new JTextPane();
            textBox.setFont(customFont);
        } catch (IOException|FontFormatException e) {
            System.out.println("<Error> Font failure");
        }

        SimpleAttributeSet attributeSet = new SimpleAttributeSet();  
        StyleConstants.setForeground(attributeSet, Color.white);

        textBox.setAutoscrolls(true);
        textBox.setEditable(false);
        textBox.setBackground(Color.black);
        textBox.setCharacterAttributes(attributeSet, true);  
        textBox.setText("Text");

        textPanel.add(textBox);
        inventoryPanel.add(new JLabel("Empty"));
        
        mainPanel.setLayout(new GridLayout(1,3));
        mainPanel.add(textPanel); //I want this to take up 2 columns
        mainPanel.add(inventoryPanel); //I want this to take up 1 column
        return mainPanel;
    }
}

I hope someone can help and I don't mind if it is a completely different method then the Gridlayout. Sorry if I did some things wrong or weirdly. I am new to the entire stackoverflow website and java.

  • You need to show your attempt at doing this. – WJS Jul 20 '21 at 00:39
  • 1
    You might be better off looking to use a [`JSplitPane`](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/javax/swing/JSplitPane.html) than a layout for this, but I am very suspicious of the requirement for ⅔ and ⅓. They seem quite arbitrary divisions. Better to give each panel as much space as it needs (see `pack()`) and let the layouts and natural sizes of the components do the rest. But I need clarification on *"(For later use)"*. DYM for later development before the program goes out to users, or for later in the run of the app. for a particular user? – Andrew Thompson Jul 20 '21 at 00:43
  • 1
    You don't design GUI's by hardcoding values or proportions. You add components to the panels and the panels will size themselves based on the preferred size of the components added. So in your case just use the defaault BorderLayout. Add your one panel to the BorderLayout.LINE_END. Add the JScrollPane which contains your JTextPand to the BorderLayout.CENTER. The panel on the left will remain a fixed size and the panel in the center will resize as the frame resizes. This is how you create dynamic GUI's. Also, don't use static variables. That is not how Swing components should be defined. – camickr Jul 20 '21 at 00:53

0 Answers0