Reputation: 793
I've never used GridBagLayout but I'm sure it could be something else. Currently I'm trying to add a TextArea to act as a console viewport window for any code being executed. As it stands, the TextArea is minuscule and actually can't see any text.
I tried consulting this page for one of the suggestions but I was still unable to correctly get the JTextArea to show.
The code is pasted below, let me know if you need anything else. I've also attached an additional picture showing what it's currently doing. Edit: setup(); is an empty body.
@SuppressWarnings("serial")
public abstract class MainComponent extends JPanel {
protected String componentName = "DEMO";
private JLabel title;
private GridBagConstraints gbc;
private Insets spacing;
private Font buttonFont;
/*
* Redirection manipulation for the project.
*/
private JTextArea localConsole;
public MainComponent(Dimension dim) {
setup();
/* Set main body */
setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
spacing = new Insets(100,5,100,5);
buttonFont = new Font("Consolas", Font.ITALIC, 22);
/* Set title */
title = new JLabel(componentName);
title.setFont(new Font("Consolas", Font.BOLD, 48));
gbc.fill = GridBagConstraints.CENTER;
gbc.gridx = 1;
gbc.gridy = 0;
add(title, gbc);
JButton run = new JButton("Run Project");
run.setFont(buttonFont);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = spacing;
gbc.gridx = 0;
gbc.gridy = 2;
add(run, gbc);
JButton open = new JButton("Open Codebase");
open.setFont(buttonFont);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = spacing;
gbc.gridx = 1;
gbc.gridy = 2;
add(open, gbc);
JButton exit = new JButton("Exit Program");
exit.setFont(buttonFont);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = spacing;
gbc.gridx = 2;
gbc.gridy = 2;
add(exit, gbc);
/* Setup console for output */
localConsole = new JTextArea();
DefaultCaret caret = (DefaultCaret) localConsole.getCaret();
caret.setUpdatePolicy(2);
localConsole.setEditable(false);
localConsole.setFont(new Font("Consolas", Font.PLAIN, 16));
localConsole.setWrapStyleWord(true);
localConsole.setSize(new Dimension(400, 200));
JScrollPane scrollPane = new JScrollPane(localConsole);
scrollPane.setVerticalScrollBarPolicy(22);
scrollPane.setSize(new Dimension(400, 200));
gbc.fill = GridBagConstraints.CENTER;
gbc.insets = new Insets(200, 0, 20, 0);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 5;
gbc.gridheight = 2;
add(scrollPane, gbc);
}
Upvotes: 0
Views: 888
Reputation: 324118
Giving us a picture of what it currently does doesn't really help because we don't know what you are trying to achieve to its hard to make an exact suggestion.
So what is see is an attempt to:
So I would suggest that you never need to use a single layout manager. Many times a combination of layout managers is easier.
So I would suggest you:
Page_START
of the BorderLayoutCENTER
of the BorderLayout.So the basic code would be:
setLayout( new BorderLayout() );
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(run);
buttonsPane.add((open);
buttonsPanel.add(exit);
add(buttonsPanel, BorderLayout.PAGE_START);
JTextArea textArea = new JTextArea(10, 30); // give text area a default preferred size
add(new JScrollPane(textArea), BorderLayout.CENTER);
Much simpler and less code than trying to use a GridBagLayout.
However, if you want the practice of using a GridBagLayout then first read the Swing tutorial on How to Use GridBagLayout for more information on the constraints.
The basic code might be something like:
gbc.gridx = 0;
gbc.gridy = 0;
add(run, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
add(open, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
add(exit, gbc);
localConsole = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(localConsole);
gbc.fill = GridBagConstraints.CENTER;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3; // try this with 1 and 2 to see the difference.
add(scrollPane, gbc);
That is you can't just randomly use gridx/gridy/gridwidth/gridheight values.
Components need to be displayed in a grid. You can't leave gaps in the grid.
So if you want buttons in a row, you need to increment gridx sequentially for the same gridy value.
If you want the text area below the buttons you increment the gridy sequentially and start with a new gridx value.
Upvotes: 1