MAIN PROBLEM: In a JScrollPane with JPanel which contains a JTextArea, text wraps up if GUI is expanded but text does not wrap back when GUI is contracted. See example below
Okay I am building the GUI for an app I am currently working on and I am having a bit of a problem.
The explanation: My GUI is structured as illustrated below:
And this is what it looks like.
Upon expansion the the JTextArea inside the panelWithText expands and resizes the text as such:
But the problem is what happens when you make the GUI smaller. The "problem" is that I want the text to warp back as it was before. I did a little experimenting by implementing a ComponentListener to both the JScrollPane and the panelWithText and found out that componentResized is being called for panelWithText upon expansion but not for contraction. Is there any way to implement the behavior of the text warping back in the panelWithText Component?
PS: Apparently if I switch the JScrollPane with a regular JPanel it works. But I can't do that! I have a LOT of panelWithText to show to the user.
PS PS: Sorry here is the code I am using.
JFrameExt.java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.Window.Type;
import javax.swing.ScrollPaneConstants;
import java.awt.CardLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
public class JFrameExt extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameExt frame = new JFrameExt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrameExt() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 246, 164);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
panelWithText panelWithText_ = new panelWithText();
GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText_.gridx = 0;
gbc_panelWithText_.gridy = 0;
panel.add(panelWithText_, gbc_panelWithText_);
panelWithText panelWithText__1 = new panelWithText();
GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText__1.gridx = 0;
gbc_panelWithText__1.gridy = 1;
panel.add(panelWithText__1, gbc_panelWithText__1);
panelWithText panelWithText__2 = new panelWithText();
GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
gbc_panelWithText__2.gridx = 0;
gbc_panelWithText__2.gridy = 2;
panel.add(panelWithText__2, gbc_panelWithText__2);
panelWithText panelWithText__3 = new panelWithText();
GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
gbc_panelWithText__3.gridx = 0;
gbc_panelWithText__3.gridy = 3;
panel.add(panelWithText__3, gbc_panelWithText__3);
panelWithText panelWithText__4 = new panelWithText();
GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
gbc_panelWithText__4.gridx = 0;
gbc_panelWithText__4.gridy = 4;
panel.add(panelWithText__4, gbc_panelWithText__4);
panelWithText panelWithText__5 = new panelWithText();
GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
gbc_panelWithText__5.gridx = 0;
gbc_panelWithText__5.gridy = 5;
panel.add(panelWithText__5, gbc_panelWithText__5);
panelWithText panelWithText__6 = new panelWithText();
GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
gbc_panelWithText__6.gridx = 0;
gbc_panelWithText__6.gridy = 6;
panel.add(panelWithText__6, gbc_panelWithText__6);
setSize(300,100);
}
}
panelWithText.java
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
public class panelWithText extends JPanel {
/**
* Create the panel.
*/
public void me_resized(Dimension d){
System.out.println("CALLED..");
super.setPreferredSize(d);
}
public panelWithText() {
setBackground(Color.DARK_GRAY);
setForeground(Color.WHITE);
setLayout(new BorderLayout(0, 0));
JTextArea txtrIveBeenReading = new JTextArea();
txtrIveBeenReading.setEditable(false);
txtrIveBeenReading.setColumns(28);
txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
txtrIveBeenReading.setLineWrap(true);
txtrIveBeenReading.setWrapStyleWord(true);
txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
txtrIveBeenReading.setForeground(Color.WHITE);
txtrIveBeenReading.setBackground(Color.DARK_GRAY);
add(txtrIveBeenReading, BorderLayout.CENTER);
}
}
After a little playing around, I came up with this...
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameExt frame = new JFrameExt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static class ScrollablePane extends JPanel implements Scrollable {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(100, 100);
}
#Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 64;
}
#Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
#Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
#Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
public static class JFrameExt extends JFrame {
private JPanel contentPane;
/**
* Create the frame.
*/
public JFrameExt() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 246, 164);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new ScrollablePane();
scrollPane.setViewportView(panel);
GridBagLayout gbl_panel = new GridBagLayout();
// gbl_panel.columnWidths = new int[]{0, 0};
// gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
// gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
// gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
panelWithText panelWithText_ = new panelWithText();
GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText_.gridx = 0;
gbc_panelWithText_.gridy = 0;
gbc_panelWithText_.weightx = 1;
panel.add(panelWithText_, gbc_panelWithText_);
// panelWithText panelWithText__1 = new panelWithText();
// GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
// gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
// gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
//// gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
// gbc_panelWithText__1.gridx = 0;
// gbc_panelWithText__1.gridy = 1;
// panel.add(panelWithText__1, gbc_panelWithText__1);
//
// panelWithText panelWithText__2 = new panelWithText();
// GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
// gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__2.gridx = 0;
// gbc_panelWithText__2.gridy = 2;
// panel.add(panelWithText__2, gbc_panelWithText__2);
//
// panelWithText panelWithText__3 = new panelWithText();
// GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
// gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__3.gridx = 0;
// gbc_panelWithText__3.gridy = 3;
// panel.add(panelWithText__3, gbc_panelWithText__3);
//
// panelWithText panelWithText__4 = new panelWithText();
// GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
// gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__4.gridx = 0;
// gbc_panelWithText__4.gridy = 4;
// panel.add(panelWithText__4, gbc_panelWithText__4);
//
// panelWithText panelWithText__5 = new panelWithText();
// GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
// gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__5.gridx = 0;
// gbc_panelWithText__5.gridy = 5;
// panel.add(panelWithText__5, gbc_panelWithText__5);
//
// panelWithText panelWithText__6 = new panelWithText();
// GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
//// gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__6.gridx = 0;
// gbc_panelWithText__6.gridy = 6;
// panel.add(panelWithText__6, gbc_panelWithText__6);
setSize(300, 100);
}
}
public static class panelWithText extends JPanel {
/**
* Create the panel.
*/
public void me_resized(Dimension d) {
System.out.println("CALLED..");
super.setPreferredSize(d);
}
public panelWithText() {
setBackground(Color.DARK_GRAY);
setForeground(Color.WHITE);
setLayout(new BorderLayout(0, 0));
JTextArea txtrIveBeenReading = new JTextArea();
txtrIveBeenReading.setEditable(false);
txtrIveBeenReading.setColumns(28);
txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
txtrIveBeenReading.setLineWrap(true);
txtrIveBeenReading.setWrapStyleWord(true);
txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
txtrIveBeenReading.setForeground(Color.WHITE);
txtrIveBeenReading.setBackground(Color.DARK_GRAY);
add(txtrIveBeenReading, BorderLayout.CENTER);
}
}
}
Basically, you need a container that implements the Scrollable interface. This will allow you to specify that the container should track/match the view ports width. This will cause the container to be laid out when ever the view port changes size...
As a side note, you can use a single copy of the GridBagConstraints, when you add each new component to the container, the GridBagLayout will generate a copy of it's own. This is very powerful when you want to share properties of the GridBagConstraints between components ;)
Related
I'm trying to make a simple GUI calculator with Eclipse
The actionlistener is at the bottom, and eclipse says my ButtonAdd can't be resolved
the error is around lines 125-131, Any help is appreciated! :D
package main;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
public class GUIApp {
private JFrame frame;
public JTextField txtNumber_1;
public JTextField txtNumber;
public JTextField textField;
public JButton ButtonAdd;
public JButton ButtonSub;
public JButton ButtonMulti;
public JButton ButtonDiv;
public JButton btnRng;
String num1;
String num2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUIApp window = new GUIApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUIApp() {
initialize();
}
/**
* Initialize the contents of the frame.
* #return
*/
public 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[]{0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
ButtonAdd = new JButton("Addition");
ButtonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
num1 = txtNumber_1.getText();
num2 = txtNumber.getText();
if(ButtonAdd.getModel().isPressed())
textField.setText(num1 + num2);
}
});
GridBagConstraints gbc_ButtonAdd = new GridBagConstraints();
gbc_ButtonAdd.insets = new Insets(0, 0, 5, 5);
gbc_ButtonAdd.gridx = 0;
gbc_ButtonAdd.gridy = 0;
frame.getContentPane().add(ButtonAdd, gbc_ButtonAdd);
ButtonSub = new JButton("Subtraction");
GridBagConstraints gbc_ButtonSub = new GridBagConstraints();
gbc_ButtonSub.insets = new Insets(0, 0, 5, 5);
gbc_ButtonSub.gridx = 0;
gbc_ButtonSub.gridy = 2;
frame.getContentPane().add(ButtonSub, gbc_ButtonSub);
txtNumber_1 = new JTextField();
txtNumber_1.setText("Number 1");
txtNumber_1.setToolTipText("insert a number");
GridBagConstraints gbc_txtNumber_1 = new GridBagConstraints();
gbc_txtNumber_1.insets = new Insets(0, 0, 5, 0);
gbc_txtNumber_1.fill = GridBagConstraints.HORIZONTAL;
gbc_txtNumber_1.gridx = 3;
gbc_txtNumber_1.gridy = 2;
frame.getContentPane().add(txtNumber_1, gbc_txtNumber_1);
txtNumber_1.setColumns(10);
ButtonMulti = new JButton("Multiplication");
GridBagConstraints gbc_ButtonMulti = new GridBagConstraints();
gbc_ButtonMulti.insets = new Insets(0, 0, 5, 5);
gbc_ButtonMulti.gridx = 0;
gbc_ButtonMulti.gridy = 4;
frame.getContentPane().add(ButtonMulti, gbc_ButtonMulti);
txtNumber = new JTextField();
txtNumber.setText("Number 2");
txtNumber.setToolTipText("insert other number");
GridBagConstraints gbc_txtNumber = new GridBagConstraints();
gbc_txtNumber.insets = new Insets(0, 0, 5, 0);
gbc_txtNumber.fill = GridBagConstraints.HORIZONTAL;
gbc_txtNumber.gridx = 3;
gbc_txtNumber.gridy = 5;
frame.getContentPane().add(txtNumber, gbc_txtNumber);
txtNumber.setColumns(10);
ButtonDiv = new JButton("Division");
GridBagConstraints gbc_ButtonDiv = new GridBagConstraints();
gbc_ButtonDiv.insets = new Insets(0, 0, 5, 5);
gbc_ButtonDiv.gridx = 0;
gbc_ButtonDiv.gridy = 6;
frame.getContentPane().add(ButtonDiv, gbc_ButtonDiv);
btnRng = new JButton("RNG");
GridBagConstraints gbc_btnRng = new GridBagConstraints();
gbc_btnRng.insets = new Insets(0, 0, 0, 5);
gbc_btnRng.gridx = 0;
gbc_btnRng.gridy = 8;
frame.getContentPane().add(btnRng, gbc_btnRng);
textField = new JTextField();
textField.setToolTipText("output");
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.gridx = 3;
gbc_textField.gridy = 8;
frame.getContentPane().add(textField, gbc_textField);
textField.setColumns(10);
}
public void actionPerformed(ActionEvent arg0) {
String num1;
String num2;
num1 = txtNumber_1.getText();
num2 = txtNumber.getText();
if(ButtonAdd.getModel().isPressed())
textField.setText(num1 + num2);
}
}
The GUI was made with WindowBuilder by the way
ButtonAdd is a local reference, meaning it can only be accessed inside the initialize method. A solution to this is to declare the ButtonAdd outside the method.
JButton ButtonAdd;
public void initialize() {
ButtonAdd = new JButton("Addition");
}
public void actionPerformed(ActionEvent arg0) {
// here we can now access ButtonAdd
}
Also a tip, by java's standard naming conventions you usually start variables and references with a lower case letter, so ButtonAdd -> buttonAdd
because it is not a member variable. make it one and it works
JButton ButtonAdd = new JButton("Addition"); is inside another method therefore it is not visible to your action listener
I am doing a computer studies controlled assessment. This is an encryption/decryption program. However, I am trying to listen to a button in one class called Gui_Maker, which makes the GUI and all the swing elements. I then want to pass the information to a method in another class called Computerscience. However every time I press the button I get an error.
I am not advanced at all in Java, and it would help if any explanation gave any code I should put into my program, and explained in laymen's terms.
Here's my code:
package computerscience;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class Gui_Maker implements ActionListener {
Computerscience computerscience = new Computerscience();
JButton btnEncrypt = new JButton("Encrypt");
JButton btnDecrypt = new JButton("Decrypt");
JButton btnOpen = new JButton("Open");
protected JLabel lblEnterYourMessage;
protected JLabel lblEnterYourOffset;
protected JTextArea txtrMessage;
protected JTextArea txtrOffset;
private JFrame frame;
boolean test;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui_Maker window = new Gui_Maker();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Gui_Maker() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
btnEncrypt.addActionListener(this);
btnDecrypt.addActionListener(this);
btnOpen.addActionListener(this);
frame = new JFrame();
frame.setBounds(100, 100, 475, 240);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0 };
frame.getContentPane().setLayout(gridBagLayout);
JLabel lblEnterYourMessage = new JLabel("Enter your message here");
GridBagConstraints gbc_lblEnterYourMessage = new GridBagConstraints();
gbc_lblEnterYourMessage.anchor = GridBagConstraints.SOUTH;
gbc_lblEnterYourMessage.insets = new Insets(0, 0, 5, 5);
gbc_lblEnterYourMessage.gridx = 0;
gbc_lblEnterYourMessage.gridy = 0;
frame.getContentPane()
.add(lblEnterYourMessage, gbc_lblEnterYourMessage);
JTextArea txtrMessage = new JTextArea();
txtrMessage.setText("Message");
GridBagConstraints gbc_txtrMessage = new GridBagConstraints();
gbc_txtrMessage.insets = new Insets(0, 0, 5, 5);
gbc_txtrMessage.fill = GridBagConstraints.BOTH;
gbc_txtrMessage.gridx = 0;
gbc_txtrMessage.gridy = 1;
frame.getContentPane().add(txtrMessage, gbc_txtrMessage);
GridBagConstraints gbc_btnEncrypt = new GridBagConstraints();
gbc_btnEncrypt.fill = GridBagConstraints.VERTICAL;
gbc_btnEncrypt.insets = new Insets(0, 0, 5, 0);
gbc_btnEncrypt.gridx = 1;
gbc_btnEncrypt.gridy = 1;
frame.getContentPane().add(btnEncrypt, gbc_btnEncrypt);
JLabel lblEnterYourOffset = new JLabel("Enter your offset here");
GridBagConstraints gbc_lblEnterYourOffset = new GridBagConstraints();
gbc_lblEnterYourOffset.anchor = GridBagConstraints.SOUTH;
gbc_lblEnterYourOffset.insets = new Insets(0, 0, 5, 5);
gbc_lblEnterYourOffset.gridx = 0;
gbc_lblEnterYourOffset.gridy = 2;
frame.getContentPane().add(lblEnterYourOffset, gbc_lblEnterYourOffset);
GridBagConstraints gbc_btnDecrypt = new GridBagConstraints();
gbc_btnDecrypt.fill = GridBagConstraints.VERTICAL;
gbc_btnDecrypt.insets = new Insets(0, 0, 5, 0);
gbc_btnDecrypt.gridx = 1;
gbc_btnDecrypt.gridy = 2;
frame.getContentPane().add(btnDecrypt, gbc_btnDecrypt);
JTextArea txtrOffset = new JTextArea();
txtrOffset.setText("Offset");
GridBagConstraints gbc_txtrOffset = new GridBagConstraints();
gbc_txtrOffset.insets = new Insets(0, 0, 0, 5);
gbc_txtrOffset.fill = GridBagConstraints.BOTH;
gbc_txtrOffset.gridx = 0;
gbc_txtrOffset.gridy = 3;
frame.getContentPane().add(txtrOffset, gbc_txtrOffset);
GridBagConstraints gbc_btnOpen = new GridBagConstraints();
gbc_btnOpen.fill = GridBagConstraints.VERTICAL;
gbc_btnOpen.gridx = 1;
gbc_btnOpen.gridy = 3;
frame.getContentPane().add(btnOpen, gbc_btnOpen);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnEncrypt) {
String input_text;
int input_offset;
test = true;
input_text = txtrMessage.getText();
input_offset = Integer.parseInt(txtrOffset.getText());
computerscience.encrypt(input_text, input_offset, test);
}
if (e.getSource() == btnDecrypt) {
String input_text;
int input_offset;
test = false;
input_text = txtrMessage.getText();
input_offset = Integer.parseInt(txtrOffset.getText());
computerscience.decrypt(input_text, input_offset, test);
}
if (e.getSource() == btnOpen) {
}
}
}
These are fields in your class:
protected JTextArea txtrMessage;
protected JTextArea txtrOffset;
Here you recreate objects locally in a method
JTextArea txtrMessage = new JTextArea();
//...
JTextArea txtrOffset = new JTextArea();
And in another method you access the uninitialized fields:
input_text = txtrMessage.getText();
input_offset = Integer.parseInt(txtrOffset.getText());
Omit the types from the code in the init method.
txtrMessage = new JTextArea();
//...
txtrOffset = new JTextArea();
And so for some other JComponents, although it doesn't hurt with these:
lblEnterYourMessage = ...
lblEnterYourOffset = ...
I am trying to make a Java JDialog where symbols are displayed in a tableish way. In fact something similar to charmap for windows:
But I ran into some issues:
I need the JDialog to be re-sizable so I can't choose to have a table as display, because it needs to be dynamically scaled.(To clarify I mean not just the adjustment of the width of every cell, but the actual amount of the cells in the row/column)
The symbols obviously need to be wrapped in some sort of listener friendly component. But if I choose JButtons, everything looks wrong, because of the different character width in pixel.
Since I have to loop through the symbols to create JButtons I can only make Objects with no name. So how do I even find out which button was pressed?
here is what I've got so far:
Code Updated 05.12.2014
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class SymbolDialog extends JDialog{
private char[] math = {8704, 8707, 8708, 8710, 8712, 8713, 8715, 8716, 8721, 8723, 8728, 8730, 8734, 8743, 8744, 8745, 8746, 8747, 8776, 8793, 8800, 8801, 8804, 8805, 8834, 8835, 8836, 8837, 8838, 8839};
private final String[] sTypes = {"Math","Logic", "Arrows"};
private JPanel pan = new JPanel();
public SymbolDialog(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
System.out.println("catdch 1");
} catch (InstantiationException e1) {
System.out.println("catdch 2");
} catch (IllegalAccessException e1) {
System.out.println("catdch 3");
} catch (UnsupportedLookAndFeelException e1) {
System.out.println("catdch 4");
}
SwingUtilities.updateComponentTreeUI(this);
pan.setPreferredSize(new Dimension(428, 70));
pan.setLayout(new ColumnsFlowLayout(0,0));
// Button Build
for (int i = 0; i < math.length; i++){
pan.add(new JButton(math[i]+""));
}
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
getContentPane().setLayout(gridBagLayout);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(5,10,0,10));
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.anchor = GridBagConstraints.WEST;
gbc_panel.fill = GridBagConstraints.VERTICAL;
gbc_panel.insets = new Insets(0, 0, 5, 0);
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
getContentPane().add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblCathegorie = new JLabel("Categories:");
GridBagConstraints gbc_lblCathegorie = new GridBagConstraints();
gbc_lblCathegorie.insets = new Insets(0, 0, 0, 5);
gbc_lblCathegorie.gridx = 0;
gbc_lblCathegorie.gridy = 0;
panel.add(lblCathegorie, gbc_lblCathegorie);
JComboBox<Object> comboBox = new JComboBox<Object>(sTypes);
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.gridx = 1;
gbc_comboBox.gridy = 0;
panel.add(comboBox, gbc_comboBox);
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 1;
getContentPane().add(pan, gbc_scrollPane);
// Pack
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(true);
pack();
setTitle("Symbols");
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
//TODO sysout which symbol was clicked
System.out.println(((JButton) event.getSource()).getText());
}
public static void main(String[] args){
new SymbolDialog();
}
}
ps: I use the recommended columns_flow_layout from here
I would use JButton[][] in a GridLayout.
For adding them to the JPanel you could use for in a for loop like so:
for(int row = 0; row < ITEMS-PER-ROW; row++){
for(int col = 0; col < ITEMS-PER-COL; col++){
JButton[row][col] = new JButton(/*SignToShowAs String[][]*/)
}
}
I have 10 combo boxes and one button. Is there an easy way to make it so you set all the values of each combo box and then press this button and it stores all the values of the combo boxes?
You first have to iterate through all your JComboBox elements and get their selected state (ie: getSelectedItem() or getSelectedIndex()). Once you have determined what selection has been made, you can save the selections in a number of places to be read later, dependent on your needs:
You can save the states into a file that can be read at a later date
You can save the information in the local registry via Java Preferences API
If it is a network application, you can save the information in an SQL database
Sure! I'd propose to write yourself a new class which has both things:
class comboTrack{
private JComboBox box;
private Object value;
public comboTrack(JComboBox box){
this.box = box;
this.value = box.getSelectedItem();
}
public void update(){
this.value = box.getSelectedItem();
}
public Object getValue(){
return value;
}
}
Then, use these instead of normal Boxes. Like this:
//List of boxes (class variable):
ArrayList<comboTrack> boxes;
//Inside the button click method:
for(comboTrack box : boxes){
box.update();
}
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class ComboBoxValues extends JFrame {
private JPanel contentPane;
JComboBox comboBox;
JComboBox comboBox_1;
JComboBox comboBox_2;
JComboBox comboBox_3;
JComboBox comboBox_4;
JComboBox comboBox_5;
JComboBox comboBox_6;
JComboBox comboBox_7;
JComboBox comboBox_8;
JComboBox comboBox_9;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ComboBoxValues frame = new ComboBoxValues();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ComboBoxValues() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 5);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 0;
gbc_comboBox.gridy = 0;
contentPane.add(comboBox, gbc_comboBox);
comboBox.addItem("comboBox_0 val 1");
comboBox.addItem("comboBox_0 val 2");
comboBox_1 = new JComboBox();
GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
gbc_comboBox_1.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_1.gridx = 1;
gbc_comboBox_1.gridy = 0;
contentPane.add(comboBox_1, gbc_comboBox_1);
comboBox_1.addItem("comboBox_1 val 1");
comboBox_1.addItem("comboBox_1 val 2");
comboBox_2 = new JComboBox();
GridBagConstraints gbc_comboBox_2 = new GridBagConstraints();
gbc_comboBox_2.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_2.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_2.gridx = 0;
gbc_comboBox_2.gridy = 1;
contentPane.add(comboBox_2, gbc_comboBox_2);
comboBox_2.addItem("comboBox_2 val 1");
comboBox_2.addItem("comboBox_2 val 2");
comboBox_3 = new JComboBox();
GridBagConstraints gbc_comboBox_3 = new GridBagConstraints();
gbc_comboBox_3.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_3.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_3.gridx = 1;
gbc_comboBox_3.gridy = 1;
contentPane.add(comboBox_3, gbc_comboBox_3);
comboBox_3.addItem("comboBox_3 val 1");
comboBox_3.addItem("comboBox_3 val 2");
comboBox_4 = new JComboBox();
GridBagConstraints gbc_comboBox_4 = new GridBagConstraints();
gbc_comboBox_4.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_4.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_4.gridx = 0;
gbc_comboBox_4.gridy = 2;
contentPane.add(comboBox_4, gbc_comboBox_4);
comboBox_4.addItem("comboBox_4 val 1");
comboBox_4.addItem("comboBox_4 val 2");
comboBox_5 = new JComboBox();
GridBagConstraints gbc_comboBox_5 = new GridBagConstraints();
gbc_comboBox_5.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_5.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_5.gridx = 1;
gbc_comboBox_5.gridy = 2;
contentPane.add(comboBox_5, gbc_comboBox_5);
comboBox_5.addItem("comboBox_5 val 1");
comboBox_5.addItem("comboBox_5 val 2");
comboBox_6 = new JComboBox();
GridBagConstraints gbc_comboBox_6 = new GridBagConstraints();
gbc_comboBox_6.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_6.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_6.gridx = 0;
gbc_comboBox_6.gridy = 3;
contentPane.add(comboBox_6, gbc_comboBox_6);
comboBox_6.addItem("comboBox_6 val 1");
comboBox_6.addItem("comboBox_6 val 2");
comboBox_7 = new JComboBox();
GridBagConstraints gbc_comboBox_7 = new GridBagConstraints();
gbc_comboBox_7.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_7.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_7.gridx = 1;
gbc_comboBox_7.gridy = 3;
contentPane.add(comboBox_7, gbc_comboBox_7);
comboBox_7.addItem("comboBox_7 val 1");
comboBox_7.addItem("comboBox_7 val 2");
comboBox_8 = new JComboBox();
GridBagConstraints gbc_comboBox_8 = new GridBagConstraints();
gbc_comboBox_8.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_8.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_8.gridx = 0;
gbc_comboBox_8.gridy = 4;
contentPane.add(comboBox_8, gbc_comboBox_8);
comboBox_8.addItem("comboBox_8 val 1");
comboBox_8.addItem("comboBox_8 val 2");
comboBox_9 = new JComboBox();
GridBagConstraints gbc_comboBox_9 = new GridBagConstraints();
gbc_comboBox_9.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_9.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_9.gridx = 1;
gbc_comboBox_9.gridy = 4;
contentPane.add(comboBox_9, gbc_comboBox_9);
comboBox_9.addItem("comboBox_9 val 1");
comboBox_9.addItem("comboBox_9 val 2");
JButton btnGetValues = new JButton("Get Values");
GridBagConstraints gbc_btnGetValues = new GridBagConstraints();
gbc_btnGetValues.insets = new Insets(0, 0, 5, 0);
gbc_btnGetValues.gridx = 1;
gbc_btnGetValues.gridy = 5;
contentPane.add(btnGetValues, gbc_btnGetValues);
textArea = new JTextArea();
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.gridheight = 2;
gbc_lblNewLabel.gridwidth = 2;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 6;
contentPane.add(textArea, gbc_lblNewLabel);
textArea.setColumns(30);
textArea.setRows(5);
pack();
btnGetValues.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText(comboBox.getSelectedItem().toString()+","+
comboBox_1.getSelectedItem().toString()+","+
comboBox_2.getSelectedItem().toString()+"\n"+
comboBox_3.getSelectedItem().toString()+","+
comboBox_4.getSelectedItem().toString()+","+
comboBox_5.getSelectedItem().toString()+"\n"+
comboBox_6.getSelectedItem().toString()+","+
comboBox_7.getSelectedItem().toString()+","+
comboBox_8.getSelectedItem().toString()+"\n"+
comboBox_9.getSelectedItem().toString());
}
});
}
}
I am new to java swing, recently I try to create a swing app to format text.
When I click the maximum button, the text panel's length does not resize, and the middle panel takes large space.
And seems setResizable(false) does not work
Code
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;
private JTextArea fromTextArea;
private JTextArea toTextArea;
public MainFrame() {
super("jFormatter");
Panel mainPanel = new Panel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
setContentPane(mainPanel);
fromTextArea = createTextArea();
lines.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.LIGHT_GRAY));
lines.setEditable(false);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
lines.setFont(f);
JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
fromTextAreaScrollPanel.getViewport().add(fromTextArea);
fromTextAreaScrollPanel.setRowHeaderView(lines);
mainPanel.add(fromTextAreaScrollPanel);
// control panel
mainPanel.add(createCtrlPanel());
toTextArea = createTextArea();
mainPanel.add(createTextAreaPanel(toTextArea));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
private JPanel createCtrlPanel() {
final JComboBox jComboBox = new JComboBox(formatters.keySet().toArray());
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JButton fmtButton = new JButton("Format >>");
JPanel ctrPanel = new JPanel(new GridBagLayout());
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
//gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);
return ctrPanel;
}
private JScrollPane createTextAreaPanel(JTextArea textArea) {
JScrollPane fromTextAreaScrollPanel = new JScrollPane(textArea);
//fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
return fromTextAreaScrollPanel;
}
private JTextArea createTextArea() {
JTextArea textArea = new JTextArea(20, 40);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
textArea.setFont(f);
//fromTextArea.setLineWrap(true);
//textArea.setBackground(Color.LIGHT_GRAY);
textArea.setMargin(new Insets(0, 10, 0, 10));
return textArea;
}
public static void main(String[] args) {
new MainFrame();
}
}
result:
You should probably use BorderLayout or GridBagLayout for this. BoxLayout just lays out components one after the other at their preferred size. It doesn't make any attempt to resize the components or make them fill their parent.
Try to make a layout like this:
Code:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
gbc_scrollPane.weightx=1;
contentPane.add(scrollPane, gbc_scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.gridheight = 2;
gbc_panel.insets = new Insets(0, 0, 5, 5);
//gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JComboBox comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 2;
gbc_comboBox.gridy = 0;
gbc_comboBox.weightx=0.0;
panel.add(comboBox, gbc_comboBox);
JButton btnNewButton = new JButton(">>");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 1;
panel.add(btnNewButton, gbc_btnNewButton);
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.gridheight = 2;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 2;
gbc_scrollPane_1.gridy = 0;
gbc_scrollPane_1.weightx=1;
contentPane.add(scrollPane_1, gbc_scrollPane_1);
JTextArea textArea_1 = new JTextArea();
scrollPane_1.setViewportView(textArea_1);
pack();
}
}