The below code creates the following GUI.
But I would like to have TextFields "A" and "C" completely fill their respective rows (so that their right corners are aligned with the right edge of the JComboBox. Help would be much appreciated!
Here's the code:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
Dimension size = new Dimension( 310, 210 );
frame.setSize(size);
frame.setPreferredSize(size);
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JLabel label1 = new JLabel( "A");
JLabel label2 = new JLabel( "B");
JLabel label3 = new JLabel( "C");
String[] opts = {"1","2","3"};
JComboBox dropdown = new JComboBox(opts);
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().
addComponent(label1).addComponent(label2).
addComponent(label3));
hGroup.addGroup(layout.createParallelGroup().
addComponent(tf1).addComponent(tf2).
addComponent(tf3));
hGroup.addGroup(layout.createParallelGroup().addComponent(dropdown));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label1).addComponent(tf1));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label2).addComponent(tf2).addComponent(dropdown));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label3).addComponent(tf3));
layout.setVerticalGroup(vGroup);
frame.add( panel, BorderLayout.NORTH );
frame.setVisible(true);
I think the best for this would be to use GridBagLayout, it is al little bit hard to use, but if you read some tutorials you'll find that it is the perfect solution for you.
GridBagLayout have weight in x and y, fill in x and y, and it is like a table.
You need to specify table with 2 columns, and three rows.
then each element need to be positioned in this table row and column, but the A and C should have gridtWidth set to 2 columns. And in the middle row you put jtextdield in one column, and jcombobox in the second.
Here you have sample program that do this:
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComboBox;
public class Example {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Example window = new Example();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Example() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] {500, 500};
gridBagLayout.rowHeights = new int[] {50, 50, 50};
gridBagLayout.columnWeights = new double[]{1.0, 1.0};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0};
frame.getContentPane().setLayout(gridBagLayout);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.weightx = 1.0;
gbc_textField.gridwidth = 2;
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 0;
gbc_textField.gridy = 0;
frame.getContentPane().add(textField, gbc_textField);
textField.setColumns(10);
textField_1 = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.weightx = 1.0;
gbc_textField_1.insets = new Insets(0, 0, 5, 5);
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 0;
gbc_textField_1.gridy = 1;
frame.getContentPane().add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
JComboBox comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.weightx = 1.0;
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 1;
gbc_comboBox.gridy = 1;
frame.getContentPane().add(comboBox, gbc_comboBox);
textField_2 = new JTextField();
GridBagConstraints gbc_textField_2 = new GridBagConstraints();
gbc_textField_2.weightx = 1.0;
gbc_textField_2.gridwidth = 2;
gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_2.gridx = 0;
gbc_textField_2.gridy = 2;
frame.getContentPane().add(textField_2, gbc_textField_2);
textField_2.setColumns(10);
}
}
It is hard to write UI by hand if you are not very familiar. Better idea is to use some UI Design tools. For example WindowBuilder in Eclipse. It can read and show your Frames and allow you to add button by just dragging it to the right place in the frame, it is a lot easier and faster. If you click in the added element you will automatically get created an action listener and you are ready to write the code for the action. I recommend this way of building UIs:)
Related
Is this the right way to prevent GridBagLayout cells from being resized relative to their contents?
gbl_panel.columnWidths = new int[] {1000, 4000, 1000};
SSCCE
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class GridBagLayoutTemplate extends JFrame {
private JPanel contentPane;
public GridBagLayoutTemplate() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.rowHeights = new int[] {1};
gbl_panel.columnWidths = new int[] {1000, 4000, 1000};
gbl_panel.columnWeights = new double[]{1.0, 4.0, 1.0};
gbl_panel.rowWeights = new double[]{1.0};
panel.setLayout(gbl_panel);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
panel.add(scrollPane, gbc_scrollPane);
JList list = new JList();
scrollPane.setViewportView(list);
list.setModel(new AbstractListModel() {
String[] values = new String[] {"qwe"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 1;
gbc_scrollPane_1.gridy = 0;
panel.add(scrollPane_1, gbc_scrollPane_1);
JList list_1 = new JList();
scrollPane_1.setViewportView(list_1);
list_1.setModel(new AbstractListModel() {
String[] values = new String[] {"qwe"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
JScrollPane scrollPane_2 = new JScrollPane();
GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
gbc_scrollPane_2.gridx = 2;
gbc_scrollPane_2.gridy = 0;
panel.add(scrollPane_2, gbc_scrollPane_2);
JList list_3 = new JList();
scrollPane_2.setViewportView(list_3);
list_3.setModel(new AbstractListModel() {
String[] values = new String[] {"qweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqwe"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutTemplate frame = new GridBagLayoutTemplate();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Interesting. I've never seen an approach like this before.
Typically the GridBagLayout is used such that:
Each component is allocated space based on its preferred size
If extra space is available then space is allocated to to each component based on the weightx (weighty) values assigned to each component.
Therefore only the extra space is allocated in the 25% 50% 25% ratio.
Your approach seems to ignore the preferred size of components and allocate space in the 25% 50% 25% ratio.
However, you would first need to make the following change to get the proper ratio:
//gbl_panel.columnWidths = new int[] {1000, 4000, 1000};
//gbl_panel.columnWeights = new double[]{1.0, 4.0, 1.0};
gbl_panel.columnWidths = new int[] {1000, 2000, 1000};
gbl_panel.columnWeights = new double[]{1.0, 2.0, 1.0};
Below is the simplified code I used to test allocating space to components on a panel that is 400 pixels wide:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutRelative extends JPanel
{
public GridBagLayoutRelative()
{
setPreferredSize( new Dimension(400, 300) );
// Components will be allocated relative widths of 25%, 50%, 25%
// (preferred width will be ignored)
GridBagLayout gbl = new GridBagLayout();
//gbl.columnWidths = new int[] {25, 50, 25}; // doesn't work
gbl.columnWidths = new int[] {250, 500, 250}; // larger value in same ratio
gbl.columnWeights = new double[] {25.0, 50.0, 25.0};
setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1.0;
gbc.gridy = 0;
JPanel red = new JPanel();
red.setBackground( Color.RED );
red.setPreferredSize( new Dimension(30, 200) );
gbc.gridx = 0;
add(red, gbc);
JPanel green = new JPanel();
green.setBackground( Color.GREEN );
green.setPreferredSize( new Dimension(40, 200) );
gbc.gridx = 1;
add(green, gbc);
JPanel blue = new JPanel();
blue.setBackground( Color.BLUE );
blue.setPreferredSize( new Dimension(50, 200) );
gbc.gridx = 2;
add(blue, gbc);
addComponentListener( new ComponentAdapter()
{
#Override
public void componentResized(ComponentEvent e)
{
System.out.println( e.getComponent().getSize() );
for (Component c: getComponents())
System.out.println( "\t" + c.getSize() );
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("GridBagLayout Relative");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GridBagLayoutRelative());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
}
}
And the results are:
java.awt.Dimension[width=100,height=300]
java.awt.Dimension[width=200,height=300]
java.awt.Dimension[width=100,height=300]
So I would say the approach works.
Note: In case you are interested, in the past I have suggested you can use the Relative Layout which was designed specifically for this type of layout and is easier to use.
Using the RelativeLayout the above behaviour would be replicated by using:
RelativeLayout rl = new RelativeLayout(RelativeLayout.X_AXIS);
rl.setFill(true);
setLayout( rl );
JPanel red = new JPanel();
red.setBackground( Color.RED );
red.setPreferredSize( new Dimension(30, 200) );
add(red, new Float(25));
JPanel green = new JPanel();
green.setBackground( Color.GREEN );
green.setPreferredSize( new Dimension(40, 200) );
add(green, new Float(50));
JPanel blue = new JPanel();
blue.setBackground( Color.BLUE );
blue.setPreferredSize( new Dimension(50, 200) );
add(blue, new Float(25));
Here's what I want the end result to look like...
I am trying to use a GridBagLayout for this but not sure if its the right choice because it's coming out all over the place. I know I shouldn't write all my code in a single constructor but here's what I have so far...
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
import static java.lang.Math.*;
public class HomeStylePizza extends JFrame{
private static final int WIDTH = 500;
private static final int HEIGHT = 400;
//GUI components
private JLabel lblEachTopping, lblPizzaSize, lblPizzaType, lblWelcome, lblYourOrder;
private JButton btnProcessSelection;
private JCheckBox cbPepproni, cbSausage, cbMushrooms, cbPineapple, cbOnion, cbBellPepper;
private JRadioButton rbSmall, rbMedium, rbLarge, rbThinCrust, rbMediumCrust, rbPan;
private ButtonGroup grp1, grp2;
private JTextArea textArea;
private CalculateButtonHandler cbHandler;
public HomeStylePizza()
{
//Create new labels
lblEachTopping = new JLabel("Each Topping: $1.50");
lblPizzaSize = new JLabel("Pizza Size");
lblPizzaType = new JLabel("Pizza Type");
lblWelcome = new JLabel("Welcome to Home Style Pizza Shop");
lblYourOrder = new JLabel("Your order:");
//Create new buttons
btnProcessSelection = new JButton("Process Selection");
cbHandler = new CalculateButtonHandler();
btnProcessSelection.addActionListener(cbHandler);
//Create new JCheckBoxes
cbPepproni = new JCheckBox("Pepproni");
cbSausage = new JCheckBox("Sausage");
cbMushrooms = new JCheckBox("Mushrooms");
cbPineapple = new JCheckBox("Pineapple");
cbOnion = new JCheckBox("Onion");
cbBellPepper = new JCheckBox("Bell Pepper");
//Create radio buttons
rbSmall = new JRadioButton("Small: $6.50");
rbMedium = new JRadioButton("Medium: $8.50");
rbLarge = new JRadioButton("Large: $10.00");
rbThinCrust = new JRadioButton("Thin Crust");
rbMediumCrust = new JRadioButton("Medium Crust");
rbPan = new JRadioButton("Pan");
//Create new TextArea
textArea = new JTextArea(6, 10);
//Set title
setTitle("Pizza Shop");
//Create new font layout
Font font = new Font("New Times Roman", Font.BOLD, 18);
//get the container
Container pane = getContentPane();
JPanel thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());
//set the layout
pane.setLayout(new GridBagLayout());
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
gridConstraints.gridwidth = 1;
gridConstraints.gridheight = 1;
gridConstraints.weightx = 50;
gridConstraints.weighty = 100;
gridConstraints.insets = new Insets(5,5,5,5);
gridConstraints.anchor = GridBagConstraints.CENTER;
gridConstraints.fill = GridBagConstraints.BOTH;
//Place components in the pane
thePanel.add(lblWelcome, gridConstraints);
gridConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
gridConstraints.gridwidth = 20;
gridConstraints.gridx = 15;
lblWelcome.setFont(new Font("New Times Roman", Font.BOLD, 18));
lblWelcome.setForeground(Color.RED);
//Create new vertical box and place a titled border around it
Box optionBox1 = Box.createVerticalBox();
optionBox1.setBorder(BorderFactory.createTitledBorder(null, "Toppings", 0, 0, new Font("times new roman", Font.PLAIN, 14), Color.RED));
//add components to optionBox1
optionBox1.add(lblEachTopping);
lblEachTopping.setForeground(Color.RED);
optionBox1.add(cbPepproni);
optionBox1.add(cbSausage);
optionBox1.add(cbMushrooms);
optionBox1.add(cbPineapple);
optionBox1.add(cbOnion);
optionBox1.add(cbBellPepper);
//add optionBox1 to WEST quadrant
thePanel.add(optionBox1, gridConstraints);
gridConstraints.anchor = GridBagConstraints.LINE_START;
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 1;
gridConstraints.gridy = 10;
//Create new vertical box and place a titled border around it
Box optionBox2 = Box.createVerticalBox();
optionBox2.setBorder(BorderFactory.createTitledBorder(null, "Pizza Size", 0, 0, new Font("times new roman", Font.PLAIN, 14), Color.RED));
//Create new ButtonGroup
grp1 = new ButtonGroup();
//add components to optionBox2 and grp1
grp1.add(rbSmall);
grp1.add(rbMedium);
grp1.add(rbLarge);
optionBox2.add(rbSmall);
optionBox2.add(rbMedium);
optionBox2.add(rbLarge);
//add optionBox2 to CENTER
thePanel.add(optionBox2, gridConstraints);
gridConstraints.anchor = GridBagConstraints.CENTER;
gridConstraints.gridx = 3;
gridConstraints.gridy = 10;
Box btnBox = Box.createHorizontalBox();
btnBox.setBorder(BorderFactory.createEmptyBorder());
btnBox.add(btnProcessSelection);
thePanel.add(btnBox);
gridConstraints.anchor = GridBagConstraints.LAST_LINE_END;
gridConstraints.gridwidth = 5;
gridConstraints.gridx = 3;
gridConstraints.gridy = 12;
//Create new vertical box and place a titled border around it
Box optionBox3 = Box.createVerticalBox();
optionBox3.setBorder(BorderFactory.createTitledBorder(null, "Pizza Type", 0, 0, new Font("times new roman", Font.PLAIN, 14), Color.RED));
//Create new ButtonGroup
grp2 = new ButtonGroup();
//add components to optionBox2 and grp1
grp2.add(rbThinCrust);
grp2.add(rbMediumCrust);
grp2.add(rbPan);
optionBox3.add(rbThinCrust);
optionBox3.add(rbMediumCrust);
optionBox3.add(rbPan);
//add optionBox3 to EAST
thePanel.add(optionBox3, gridConstraints);
gridConstraints.anchor = GridBagConstraints.LINE_END;
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 5;
gridConstraints.gridy = 10;
//Create box for lblYourOrder and textArea and
//add them to pane in SOUTH quadrant
Box orderBox = Box.createVerticalBox();
orderBox.setBorder(null);
orderBox.add(lblYourOrder);
orderBox.add(textArea);
thePanel.add(orderBox, gridConstraints);
gridConstraints.anchor = GridBagConstraints.LAST_LINE_START;
gridConstraints.gridwidth = 30;
gridConstraints.gridx = 1;
gridConstraints.gridy = 13;
pane.add(thePanel);
//set window size and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
//Center frame
this.setLocationRelativeTo(null);
this.pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//End of constructor
public static void main(String[] args)
{
HomeStylePizza menuTest = new HomeStylePizza();
}//End of main
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}//End of class
Its a bit ugly and you can ignore most of my comments I've been changing things around trying to fix it. I was using a different layout.
Edited: I went ahead and created the Pizza Shop GUI.
I used a combination of BorderLayouts and GridBagLayouts.
I grouped together the code to make it easier to follow. Grouping the code does not mean putting all of the JCheckBox initializations together. Grouping the code means that all of the elements that make up a JPanel are there, right next to each other. This way, you don't have to jump all over the code looking for the lines that define the JRadioButton rbMedium.
I used a separate GridBagConstraints for each Swing component to make it easier for me to debug the GUI.
I've left the action listener for you to code.
Here's the GUI code:
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class HomeStylePizza implements Runnable {
private static final Insets bottomInsets = new Insets(10, 10, 10, 10);
private static final Insets normalInsets = new Insets(10, 10, 0, 10);
// GUI components
private JCheckBox cbPepproni, cbSausage, cbMushrooms, cbPineapple, cbOnion,
cbBellPepper;
private JRadioButton rbSmall, rbMedium, rbLarge, rbThinCrust,
rbMediumCrust, rbPan;
private JTextArea textArea;
public static void main(String[] args) {
SwingUtilities.invokeLater(new HomeStylePizza());
} // End of main
#Override
public void run() {
JFrame frame = new JFrame("Pizza Shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
int gridy = 0;
addComponent(mainPanel, createTitlePanel(), 0, gridy++, 2, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
addComponent(mainPanel, createToppingPanel(), 0, gridy, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new BorderLayout());
eastPanel.add(createSizePanel(), BorderLayout.WEST);
eastPanel.add(new JLabel(" "), BorderLayout.CENTER);
eastPanel.add(createTypePanel(), BorderLayout.EAST);
eastPanel.add(createButtonPanel(), BorderLayout.SOUTH);
addComponent(mainPanel, eastPanel, 1, gridy++, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
addComponent(mainPanel, createTextAreaPanel(), 0, gridy++, 2, 1,
bottomInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
private JPanel createTitlePanel() {
JPanel panel = new JPanel();
JLabel lblWelcome = new JLabel("Welcome to Home Style Pizza Shop");
Font titleFont = lblWelcome.getFont().deriveFont(20F);
lblWelcome.setFont(titleFont);
lblWelcome.setForeground(Color.RED);
panel.add(lblWelcome);
return panel;
}
private JPanel createToppingPanel() {
Border redBorder = BorderFactory.createLineBorder(Color.RED, 2);
Border emptyBorder = BorderFactory.createEmptyBorder(4, 10, 4, 10);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createCompoundBorder(redBorder,
emptyBorder));
panel.setLayout(new GridLayout(0, 1));
JLabel lblEachTopping = new JLabel("Each Topping: $1.50");
lblEachTopping.setForeground(Color.RED);
panel.add(lblEachTopping);
cbPepproni = new JCheckBox("Pepperoni");
cbSausage = new JCheckBox("Sausage");
cbMushrooms = new JCheckBox("Mushrooms");
cbPineapple = new JCheckBox("Pineapple");
cbOnion = new JCheckBox("Onion");
cbBellPepper = new JCheckBox("Bell Pepper");
// add components to optionBox1
panel.add(cbPepproni);
panel.add(cbSausage);
panel.add(cbMushrooms);
panel.add(cbPineapple);
panel.add(cbOnion);
panel.add(cbBellPepper);
return panel;
}
private JPanel createSizePanel() {
Border redBorder = BorderFactory.createLineBorder(Color.RED, 2);
Border emptyBorder = BorderFactory.createEmptyBorder(4, 10, 4, 10);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createCompoundBorder(redBorder,
emptyBorder));
panel.setLayout(new GridLayout(0, 1));
JLabel lblPizzaSize = new JLabel("Pizza Size");
lblPizzaSize.setForeground(Color.RED);
panel.add(lblPizzaSize);
rbSmall = new JRadioButton("Small: $6.50");
rbMedium = new JRadioButton("Medium: $8.50");
rbLarge = new JRadioButton("Large: $10.00");
// Create new ButtonGroup
ButtonGroup group = new ButtonGroup();
group.add(rbSmall);
group.add(rbMedium);
group.add(rbLarge);
panel.add(rbSmall);
panel.add(rbMedium);
panel.add(rbLarge);
return panel;
}
private JPanel createTypePanel() {
Border redBorder = BorderFactory.createLineBorder(Color.RED, 2);
Border emptyBorder = BorderFactory.createEmptyBorder(4, 10, 4, 10);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createCompoundBorder(redBorder,
emptyBorder));
panel.setLayout(new GridLayout(0, 1));
JLabel lblPizzaType = new JLabel("Pizza Type");
lblPizzaType.setForeground(Color.RED);
panel.add(lblPizzaType);
rbThinCrust = new JRadioButton("Thin Crust");
rbMediumCrust = new JRadioButton("Medium Crust");
rbPan = new JRadioButton("Pan");
// Create new ButtonGroup
ButtonGroup group = new ButtonGroup();
// add components to optionBox2 and grp1
group.add(rbThinCrust);
group.add(rbMediumCrust);
group.add(rbPan);
panel.add(rbThinCrust);
panel.add(rbMediumCrust);
panel.add(rbPan);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
JButton btnProcessSelection = new JButton("Process Selection");
btnProcessSelection.addActionListener(new CalculateButtonHandler());
panel.add(btnProcessSelection);
return panel;
}
private JPanel createTextAreaPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel lblYourOrder = new JLabel("Your order:");
panel.add(lblYourOrder, BorderLayout.NORTH);
textArea = new JTextArea(6, 12);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
private class CalculateButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
}
}
} // End of HomeStylePizza class
Use GridBagLayout, or use netbeans it will make it easy for you, other option use GridLayout(2,1)
in it 2 panel
panel 1 will have grid layout of 1,2 in it you first list and other pnael has grid layout of 2,1 first row is your second list and the second row will be the button finally put the order info in the second row of your base layout.
Okay, so I have made a GUI with some input boxes and a combo box. I am wondering how I would go about having a listener know when the button is pressed then relay all the information in the imput boxes and combo box into different parts of the script...
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class dogedice extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JComboBox combo;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dogedice frame = new dogedice();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public dogedice() {
setTitle("DogeDice Bot");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.WEST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
//Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
JLabel userTag = new JLabel("Username:");
GridBagConstraints gbc_userTag = new GridBagConstraints();
gbc_userTag.insets = new Insets(0, 0, 0, 5);
gbc_userTag.anchor = GridBagConstraints.EAST;
gbc_userTag.gridx = 0;//Here are your x + y coords
gbc_userTag.gridy = 0;//Adding to x moves left, adding to y moves down
panel.add(userTag, gbc_userTag);
//Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
textField = new JTextField();
GridBagConstraints gbc_Username = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel startTag = new JLabel("Starting Bid:");
GridBagConstraints gbc_startTag = new GridBagConstraints();
gbc_startTag.insets = new Insets(0, 0, 0, 5);
gbc_startTag.anchor = GridBagConstraints.EAST;
gbc_startTag.gridx = 0;
gbc_startTag.gridy = 2;
panel.add(startTag, gbc_startTag);
textField = new JTextField();
GridBagConstraints gbc_StartBid = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 2;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel multTag = new JLabel("Multiplier:");
GridBagConstraints gbc_multTag = new GridBagConstraints();
gbc_multTag.insets = new Insets(0, 0, 0, 5);
gbc_multTag.anchor = GridBagConstraints.EAST;
gbc_multTag.gridx = 0;
gbc_multTag.gridy = 3;
panel.add(multTag, gbc_multTag);
textField = new JTextField();
GridBagConstraints gbc_Multiplier = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 3;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel minTag = new JLabel("Min Remaining:");
GridBagConstraints gbc_minTag = new GridBagConstraints();
gbc_minTag.insets = new Insets(0, 0, 0, 5);
gbc_minTag.anchor = GridBagConstraints.EAST;
gbc_minTag.gridx = 0;
gbc_minTag.gridy = 4;
panel.add(minTag, gbc_minTag);
textField = new JTextField();
GridBagConstraints gbc_MinRemaining = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 4;
panel.add(textField, gbc_textField);
textField.setColumns(10);
textField = new JTextField();
GridBagConstraints gbc_Password = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 1;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel passTag = new JLabel("Password:");
GridBagConstraints gbc_passTag = new GridBagConstraints();
gbc_passTag.insets = new Insets(0, 0, 0, 5);
gbc_passTag.anchor = GridBagConstraints.EAST;
gbc_passTag.gridx = 0;
gbc_passTag.gridy = 1;
panel.add(passTag, gbc_passTag);
textField = new JTextField();
GridBagConstraints gbc_Odds = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 5;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel oddsTag = new JLabel("Odds %:");
GridBagConstraints gbc_oddsTag = new GridBagConstraints();
gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
gbc_oddsTag.anchor = GridBagConstraints.EAST;
gbc_oddsTag.gridx = 0;
gbc_oddsTag.gridy = 5;
panel.add(oddsTag, gbc_oddsTag);
textField = new JTextField();
GridBagConstraints gbc_ComboBox = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 6;
panel.add(textField, gbc_textField);
textField.setColumns(10);
//This is the Combo Box
combo = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
combo.addActionListener(this);
GridBagConstraints gbc_list = new GridBagConstraints();
gbc_list.fill = GridBagConstraints.HORIZONTAL;
gbc_list.gridx = 1;
gbc_list.gridy = 7;
panel.add(combo, gbc_list);
JLabel maxTag = new JLabel("MaxBet:");
GridBagConstraints gbc_maxTag = new GridBagConstraints();
gbc_maxTag.insets = new Insets(0, 0, 0, 5);
gbc_maxTag.anchor = GridBagConstraints.EAST;
gbc_maxTag.gridx = 0;
gbc_maxTag.gridy = 6;
panel.add(maxTag, gbc_maxTag);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JButton btnConfirm = new JButton("Turn Up");
panel_1.add(btnConfirm);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea("Current Balance");
textArea.setColumns(1);
scrollPane.setViewportView(textArea);
JScrollPane scrollPanel = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textAreal = new JTextArea("Input bot information here...");
textArea.setColumns(20);
scrollPane.setViewportView(textArea);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == combo) {
System.out.println(combo.getSelectedIndex()+1);
}
}
}
It's quite straightforward to add an ActionListener to a JButton.
// ...
btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(this);
panel_1.add(btnConfirm);
// ...
Because you'll want to know when the button is triggering the callback, like so:
private JButton btnConfirm;
This way, you'll be able to tell when it's that button that triggered the actionListener() method:
if (event.getSource() == btnConfirm) {
// Handle "Turn Up" button press here
}
Start by taking a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener
Essentially you need to register an ActionListener with your button...
There are a number of ways you might be able to achieve this...
You Could
Use the more traditional, purpose class...
public class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if ("Turn Up".evt.getActionCommand()) {
// Handle Turn Up...
}
}
}
In this context, you'll be required to provide a reference to the component you want to modify so that the ActionHandler can interact with it, personally, this is best done via an interface, but that's just me...
public class ActionHandler implements ActionListener {
private dogedice dice;
public ActionHandler(dogedice dice) {
this.dice = dice;
}
public void actionPerformed(ActionEvent evt) {
//...
}
}
The benefit of this is you can plug an play the action handler, changing what the action does depending on your needs. If you use an interface instead of an implementation reference, you further decouple the action from the program
You Could
Use use an anonymous class
btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// handle turn up action...
}
});
The benefit to this is, you don't end up with "another" class, the ActionListener can reference all the internal fields of the parent class and, because you've attached it directly, you can assume that the only thing that is going to generate the ActionEvent is the btnConfirm button.
The downside is you lose the flexibility to change how the action is handled without physically modifying the code
You Could
Take advantage of the Actions API, this is a little like the first option, in that you (should normally) create another class specifically designed to handle the "Turn Up" event, but these are self contained entities. That is, they carry all the information required to configure the UI element and perform the required action when triggered.
This is really good where the action may be repeated in the UI via menus, buttons or key strokes
I've read the documentation for GridBagLayout and I can't make sense of it. I basically want to accomplish something like this:
I made some example code to help me figure this out. How can I modify this code to accomplish this?
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JLabel label = new JLabel("label");
JTextField field = new JTextField();
JLabel label2 = new JLabel("label2");
JTextField field2 = new JTextField();
JPanel jp = new JPanel();
jp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
//gbc.weightx = ??
jp.add(label, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
//gbc.weightx = ??
jp.add(field, gbc);
JPanel jp2 = new JPanel();
jp2.setLayout(new GridBagLayout());
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.fill = GridBagConstraints.BOTH;
gbc2.gridx = 0;
gbc2.gridy = 0;
gbc2.gridwidth = 1;
//gbc2.weightx = ??
jp2.add(label2, gbc2);
gbc2.gridx = 1;
gbc2.gridwidth = 2;
//gbc2.weightx = ??
jp2.add(field2, gbc2);
JPanel jpContainer = new JPanel();
jpContainer.setLayout(new BoxLayout(jpContainer, BoxLayout.Y_AXIS));
jpContainer.add(jp);
jpContainer.add(jp2);
JFrame f = new JFrame();
f.setSize(300, 100);
f.setContentPane(jpContainer);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
EDIT: Changed JTextArea to JTextField
Using GridBagLayout columnWidths you can manually adjust the widths and then set the GridBagConstraints fill to GridBagConstraints.HORIZONTAL :
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
public class Example extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Example frame = new Example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Example() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] {100, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JLabel lblNewLabel = new JLabel("jlabel");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
contentPane.add(lblNewLabel, gbc_lblNewLabel);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("jlabel2");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 1;
contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);
textField_1 = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 1;
gbc_textField_1.gridy = 1;
contentPane.add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
}
}
Of course, if you want to maintain the 1/3 Label and 2/3 JTextField, you might consider using a MigLayout as such:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MigLayoutExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MigLayoutExample frame = new MigLayoutExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MigLayoutExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[grow 33][grow 66]", "[][]"));
JLabel lblNewLabel = new JLabel("New label");
contentPane.add(lblNewLabel, "cell 0 0");
textField = new JTextField();
contentPane.add(textField, "cell 1 0,growx");
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("New label");
contentPane.add(lblNewLabel_1, "cell 0 1");
textField_1 = new JTextField();
contentPane.add(textField_1, "cell 1 1,growx");
textField_1.setColumns(10);
}
}
You only have two components on each row so you can only have two columns.
If you want the JTextArea to occupy more space then define the JTextArea like:
JTextArea textArea = new JTextArea(3, 30);
to control the size of the text area by specifying the row/columns of the text area.
I'm not sure why you are using a JTextArea. It seems like a JTextField would be more appropriate. You can also specify the columns when you create a JTextField. Check out the JTextField API.
I have designed a frame containing a few controls using the design view in Netbeans 6.9.1. Further, I have added an empty panel in which I am trying to toggle display of a couple of swing components on button click. The problem is that on button click, the panel displays nothing. The code is as follows:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JPanel txtPanel = new JPanel();
JPanel listPanel = new JPanel();
JTextField txtfield = new JTextField("ABCDEFGHIJ", 20);
txtPanel.add(txtfield);
JList<String> list = new JList<String>();
DefaultListModel<String> model = new DefaultListModel<String>();
for (int i = 0; i < userCommands.size(); i++){
model.addElement(userCommands.get(i));
}
list.setModel(model);
listPanel.add(list);
jPanel2.add(listPanel, "list");
jPanel2.add(txtPanel, "text");
//MainUI.getFrames()[0].add(jPanel2, BorderLayout.CENTER);
itemStateChanged("text");
}
Code for itemStateChanged is as follows:
public void itemStateChanged(String disp) {
CardLayout cl = (CardLayout)(jPanel2.getLayout());
cl.show(jPanel2, disp);
}
In the first piece of code, jPanel2 is dragged and dropped onto the frame containing other components, what i am trying to achieve here is that on button click, the jPanel2 should toggle between text field and list. But currently, the panel is not displaying anything on button click.
Before even considering if jPanel2 can switch between the different panels (the different cards), is jPanel2 on display at all anywhere? The only code I can see that displayes the jPanel2 is adding it to the MainUI but it is commented out?! So how do you know that the display inside jPanel2 isn't switching?
I did a quick example code for you .Have a look.You need to define the Card layout as global and try.
import javax.swing.AbstractAction;
public class TestPanel extends JPanel {
/**
* Create the panel.
*/
JPanel panel;
CardLayout cl = new CardLayout();
public TestPanel() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 155, 0, 0};
gridBagLayout.rowHeights = new int[]{94, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JButton btnNewButton = new JButton("New button");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.gridx = 0;
gbc_btnNewButton.gridy = 0;
add(btnNewButton, gbc_btnNewButton);
btnNewButton.setAction(new AbstractAction("New button") {
#Override
public void actionPerformed(ActionEvent arg0) {
JPanel txtPanel = new JPanel();
JPanel listPanel = new JPanel();
JTextField txtfield = new JTextField("ABCDEFGHIJ", 20);
txtPanel.add(txtfield);
JList<String> list = new JList<String>();
DefaultListModel<String> model = new DefaultListModel<String>();
for (int i = 0; i < 3; i++){
model.addElement("Sanjaya");
}
list.setModel(model);
listPanel.add(list);
panel.add(listPanel, "list");
panel.add(txtPanel, "text");
//MainUI.getFrames()[0].add(jPanel2, BorderLayout.CENTER);
itemStateChanged("list");
}
});
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
GridBagConstraints gbc_textArea = new GridBagConstraints();
gbc_textArea.insets = new Insets(0, 0, 5, 5);
gbc_textArea.fill = GridBagConstraints.BOTH;
gbc_textArea.gridx = 1;
gbc_textArea.gridy = 0;
add(textArea, gbc_textArea);
panel = new JPanel(cl);
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.insets = new Insets(0, 0, 5, 0);
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 2;
gbc_panel.gridy = 0;
add(panel, gbc_panel);
}
public void itemStateChanged(String disp) {
cl.show(panel, disp);
}
}