Reputation: 11
Having a bit of trouble with JTextArea, and specifically the scaling of it. Whenever I try to readjust the size of instructionsarea it really messes with all of my other components. I think this is due to some weird BorderLayout interactions, but I'm not sure. Finding a way to keep the current components while also having an instructionsarea that's a good size would be amazing.
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
public class CoffeeShop extends JFrame {
private JLabel coffeemenu, seasonaldrinks, instructions;
private JPanel westpanel, westpanel2;
private JComboBox menubox, seasonalmenubox;
private JTextArea instructionsarea;
public CoffeeShop(){
coffeemenu = new JLabel("Coffee Menu:");
seasonaldrinks = new JLabel("Seasonal Drinks:");
instructions = new JLabel("List Any Special Instructions:");
instructionsarea = new JTextArea(3, 1);
westpanel = new JPanel();
westpanel2 = new JPanel();
westpanel.add(coffeemenu);
westpanel2.add(westpanel);
add(westpanel2, BorderLayout.WEST);
westpanel.setLayout(new GridLayout(6,1));
String menuboxoption [] = {"Choose Below:","Drip Coffee", "Cookie Butter Latte", "Caramel Latte",
"Matcha Latte", "Craft Matcha", "Classic Latte", "Vanilla Latte", "Hazlenut Latte"};
String seasonalmenuboxoption [] = {"Choose Below:", "Iced Pumpkin Butter Latte","Iced Autumn Spice Latte", "Crisp Apple Sparkling Cider",
"Iced Melting Monster Latte", "Hot Pumpkin Cookie Butter Latte"};
menubox = new JComboBox(menuboxoption);
seasonalmenubox = new JComboBox(seasonalmenuboxoption);
westpanel.add(menubox);
westpanel.add(seasonaldrinks);
westpanel.add(seasonalmenubox);
westpanel.add(instructions);
westpanel.add(instructionsarea);
setTitle("Commonwealth Joe's");
setSize(900, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args){
CoffeeShop run = new CoffeeShop();
}
}
Upvotes: 1
Views: 53
Reputation: 347334
So, two things, first, always wrap your JTextArea
in a JScrollPane
, things will get very ... weird ... otherwise. Second, while using multiple containers is a good idea for complex layouts, in this case, I might consider just using one container and use a different layout (other then GridLayout
, for example ...
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class CoffeeShop extends JFrame {
private JLabel coffeemenu, seasonaldrinks, instructions;
private JPanel westpanel;
private JComboBox menubox, seasonalmenubox;
private JTextArea instructionsarea;
public CoffeeShop() {
coffeemenu = new JLabel("Coffee Menu:");
seasonaldrinks = new JLabel("Seasonal Drinks:");
instructions = new JLabel("List Any Special Instructions:");
instructionsarea = new JTextArea(3, 1);
String menuboxoption[] = {"Choose Below:", "Drip Coffee", "Cookie Butter Latte", "Caramel Latte",
"Matcha Latte", "Craft Matcha", "Classic Latte", "Vanilla Latte", "Hazlenut Latte"};
String seasonalmenuboxoption[] = {"Choose Below:", "Iced Pumpkin Butter Latte", "Iced Autumn Spice Latte", "Crisp Apple Sparkling Cider",
"Iced Melting Monster Latte", "Hot Pumpkin Cookie Butter Latte"};
menubox = new JComboBox(menuboxoption);
seasonalmenubox = new JComboBox(seasonalmenuboxoption);
westpanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(8, 8, 8, 8);
westpanel.add(coffeemenu, gbc);
westpanel.add(menubox, gbc);
westpanel.add(seasonaldrinks, gbc);
westpanel.add(seasonalmenubox, gbc);
westpanel.add(instructions, gbc);
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
westpanel.add(new JScrollPane(instructionsarea), gbc);
add(westpanel, BorderLayout.WEST);
setTitle("Commonwealth Joe's");
setSize(900, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
CoffeeShop run = new CoffeeShop();
}
}
Have a look at How to Use GridBagLayout for more details
Upvotes: 3