To add JButton to JTextArea - java

How can I add JButton on JTextArea?
I have such a code, when I print name of country in JTextField some information using REST displays on JTextArea. But I want to use for this purpose JButton. When user click JButton information will be start searching and then display.
public class Client extends JPanel implements ActionListener{
protected JTextField textField;
protected JTextArea textArea;
protected static JButton search;
public Client() {
super(new GridBagLayout());
search = new JButton("Search");
search.setBounds(100,190,60,30);
textField = new JTextField(20);
textField.addActionListener(this);
textArea = new JTextArea(10, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
My program show such a window, I want to place my button on the bottom of the window (on the picture).

I don't know what are you expecting exactly, but I wrote this code, I'm not use your code, cause I can't run it, but I think this is that you want.
JFrame gui = new JFrame("Button on bottom.");
JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField();
textfield.setText("Australia");
JTextArea textarea = new JTextArea();
textarea.setText("Australia, -27, 133. AUD");
JButton button = new JButton("Button on bottom.");
button.setFont(new java.awt.Font("Dialog", 0, 15));
button.setBorderPainted(false);
button.setFocusable(false);
button.setForeground(new java.awt.Color(255, 255, 255));
button.setBackground(new java.awt.Color(0, 140, 255));
panel.add(textfield, BorderLayout.PAGE_START);
panel.add(textarea);
panel.add(button, BorderLayout.PAGE_END);
gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
gui.setSize(300, 300);
gui.setLocationRelativeTo(null);
gui.add(panel);
gui.setVisible(true);}}

Related

Java swing GUI restructuring - which layouts would work

I am currently working on the user interface for a quiz game. It currently looks like this:
I would like it to look like this:
However, I'm not sure about how to structure the screen accordingly, particularly the following things:
Splitting the screen into s several ones (header, where the the timer is... positioned to to the right), ensuring timer is on the right hand side
Creating a sidebar for scores
Creating a main area for questions
Centering all the elements slightly so that there is some padding around it
Not losing the elements drawn in paintComponent
Which type of layouts would work best from the outset?
My code is as follows (note that the bulk of the work is done in createWindow and paintComponent is how I draw the responses on screen):
final class Gui extends JFrame {
static String message;
static String answer;
private String alertMessage;
private String guesses;
private Display display;
private JTextArea textArea;
private JButton startButton;
private JLabel timerLabel;
private JButton nextButton;
private int badGuesses;
private boolean gameOver;
private Timer timer;
private ArrayList<JButton> alphabetButtons = new ArrayList<>();
Gui() {
createWindow();
}
public enum GuiText {
START("Start"),
QUIT("Quit"),
SUBMIT("Submit"),
RESET("Reset"),
SEND("Send"),
NEXT(">"),
PREVIOUS("<"),
PAUSE("Pause");
private String guiText;
GuiText(String guiText) {
this.guiText = guiText;
}
#Override
public String toString() {
return guiText;
}
}
/**
* This class defines the panel that occupies the large central area in the
* main panel. The paintComponent() method in this class is responsible for
* drawing the content of that panel. It shows everything that that the user
* is supposed to see, based on the current values of all the instance variables.
*/
private class Display extends JPanel {
Display() {
setPreferredSize(new Dimension(1000, 250));
setBackground(new Color(0x00bcda));
setFont(new Font("Tahoma", Font.BOLD, 20));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
((Graphics2D) g).setStroke(new BasicStroke(3));
if (message != null) {
g.setColor(Color.DARK_GRAY);
g.drawString(message, 30, 120);
}
if (alertMessage != null) {
g.setColor(Color.DARK_GRAY);
g.drawString(alertMessage, 30, 150);
}
if (gameOver) {
alertMessage = "Click on \"Next\" to play again.";
} else {
g.drawString("Guesses remaining: " + (3 - badGuesses), 770, 40);
}
g.setColor(Color.DARK_GRAY);
if (answer != null) {
for (int i = 0; i < answer.length(); i++) {
if (String.valueOf(answer.charAt(i)).trim().length() > 0) {
g.drawLine(30 + i * 70, 210, 70 + i * 70, 210);
if (guesses.indexOf(answer.charAt(i)) >= 0) {
g.drawString(String.valueOf(answer.charAt(i)), 45 + i * 70, 195);
}
}
}
}
}
}
/**
* The constructor that creates the main panel, which is represented
* by this class. It makes all the buttons and subpanels and adds
* them to the main panel.
*/
private void createWindow() {
setJMenuBar(menuBarCreator());
// The ActionListener that will respond to button clicks.
ButtonHandler buttonHandler = new ButtonHandler();
// Create the subpanels and add them to the main panel.
display = new Display();
setLayout(new BorderLayout(3, 3));
add(display, BorderLayout.CENTER);
// Add timer panel
JPanel timerPanel = new JPanel();
timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
timerPanel.setBackground(new Color(0x00bcda));
add(timerPanel, BorderLayout.PAGE_START);
// Add timer label
timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
timerLabel.setHorizontalAlignment(JLabel.RIGHT);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
timerLabel.setForeground(Color.black);
timerPanel.add(timerLabel, c);
// Add left panel
JPanel leftPanel = new JPanel(new GridBagLayout());
c = new GridBagConstraints();
leftPanel.setBorder(new EmptyBorder(25, 25, 25, 5));
leftPanel.setBackground(new Color(0x00bcda));
add(leftPanel, BorderLayout.WEST);
// Add previous button
JButton previousButton = new JButton(String.valueOf(GuiText.PREVIOUS));
previousButton.setFont(new Font("Arial", Font.PLAIN, 60));
previousButton.addActionListener(buttonHandler);
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0;
c.gridy = 0;
leftPanel.add(previousButton, c);
// Add right panel
JPanel rightPanel = new JPanel(new GridBagLayout());
c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
rightPanel.setBorder(new EmptyBorder(25, 25, 25, 25));
rightPanel.setBackground(new Color(0x00bcda));
add(rightPanel, BorderLayout.EAST);
// Add next button
nextButton = new JButton(String.valueOf(GuiText.NEXT));
nextButton.setFont(new Font("Arial", Font.PLAIN, 60));
nextButton.addActionListener(buttonHandler);
c.gridx = 1;
c.gridy = 0;
rightPanel.add(nextButton, c);
// Add actual timer
initialiseTimer();
// Add bottom panel
JPanel bottomPanel = new JPanel(new GridBagLayout());
GridBagConstraints bottomPanelConstraints = new GridBagConstraints();
bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
add(bottomPanel, BorderLayout.PAGE_END);
setBackground(new Color(100, 0, 0));
// Add primary button panel to bottom panel
JPanel primaryButtonPanel = new JPanel();
primaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
primaryButtonPanel.setBackground(new Color(0xFFFFFF));
c.gridx = 0;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
bottomPanel.add(primaryButtonPanel, c);
// Add text area
textArea = new JTextArea(1, 10);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
textArea.setFont(new Font("Arial", Font.PLAIN, 24));
textArea.setBackground(new Color(0xCCCCCC));
textArea.setEditable(false);
primaryButtonPanel.add(textArea, c);
// Add buttons
JButton sendButton = new JButton(String.valueOf(GuiText.SEND));
sendButton.addActionListener(buttonHandler);
sendButton.setFont(new Font("Arial", Font.PLAIN, 24));
primaryButtonPanel.add(sendButton);
// Add secondary button panel
JPanel secondaryButtonPanel = new JPanel();
secondaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
secondaryButtonPanel.setBackground(new Color(0xFFFFFF));
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
bottomPanel.add(secondaryButtonPanel, c);
// Add secondary buttons
startButton = new JButton(GuiText.START.toString());
startButton.addActionListener(buttonHandler);
startButton.setFont(new Font("Arial", Font.PLAIN, 24));
secondaryButtonPanel.add(startButton);
JButton pauseButton = new JButton(GuiText.PAUSE.toString());
pauseButton.setFont(new Font("Arial", Font.PLAIN, 24));
pauseButton.addActionListener(buttonHandler);
secondaryButtonPanel.add(pauseButton);
JButton quitButton = new JButton(GuiText.QUIT.toString());
quitButton.setFont(new Font("Arial", Font.PLAIN, 24));
quitButton.addActionListener(buttonHandler);
secondaryButtonPanel.add(quitButton);
JButton submitButton = new JButton(GuiText.SUBMIT.toString());
submitButton.setFont(new Font("Arial", Font.PLAIN, 24));
submitButton.addActionListener(buttonHandler);
secondaryButtonPanel.add(submitButton);
// Add keyboard panel
JPanel keyboardPanel = new JPanel();
keyboardPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
keyboardPanel.setBackground(new Color(0xFFFFFF));
c.gridx = 0;
c.gridy = 5;
c.fill = GridBagConstraints.HORIZONTAL;
bottomPanel.add(keyboardPanel, c);
keyboardPanel.setLayout(new GridLayout(3, 10, 5, 5));
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
JButton button = new JButton(String.valueOf(alphabet));
button.addActionListener(buttonHandler);
button.setFont(new Font("Arial", Font.PLAIN, 20));
keyboardPanel.add(button);
alphabetButtons.add(button);
}
}
For this particular set of requirements, a SpringLayout (https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html) is perfect, though it requires a lot of lines of code, and is hard to read. However it it is one of the most flexibility 'out of the box' layout managers.
SpringLayout works by applying constraints to elements, bringing them in relative position to each other (including relative to the container). This is flexible, because you are able to achieve almost all configurations where the main focus is the relative position of elements (including some padding and thelike).
Incidentally, you should also be able to achieve what you need with a GridBagLayout (https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html), but that might restrict you later on.

GridLayout is misbehaving

I am trying to write Swing by hand (yeah crazy, I know) and for some reason this part panel I create here:
private JPanel createColorSliderPanel() {
JPanel colorSliderPanel = new JPanel(new GridLayout(3, 1, 0, 5));
JPanel redSliderPanel = new JPanel(new BorderLayout());
JPanel greenSliderPanel = new JPanel(new BorderLayout());
JPanel blueSliderPanel = new JPanel(new BorderLayout());
GridLayout grid = (GridLayout) colorSliderPanel.getLayout();
redSlider = new JSlider();
greenSlider = new JSlider();
blueSlider = new JSlider();
redField = new JTextField();
greenField = new JTextField();
blueField = new JTextField();
redField.setEditable(false);
greenField.setEditable(false);
blueField.setEditable(false);
JLabel redLabel = new JLabel("Red");
JLabel greenLabel = new JLabel("Green");
JLabel blueLabel = new JLabel("Blue");
redLabel.setPreferredSize(new Dimension(64, 16));
redField.setPreferredSize(new Dimension(32, 16));
redSliderPanel.add(redLabel, BorderLayout.WEST);
redSliderPanel.add(redSlider, BorderLayout.CENTER);
redSliderPanel.add(redField, BorderLayout.EAST);
greenLabel.setPreferredSize(new Dimension(64, 16));
greenField.setPreferredSize(new Dimension(32, 16));
greenSliderPanel.add(redLabel, BorderLayout.WEST);
greenSliderPanel.add(redSlider, BorderLayout.CENTER);
greenSliderPanel.add(redField, BorderLayout.EAST);
blueLabel.setPreferredSize(new Dimension(64, 16));
blueField.setPreferredSize(new Dimension(32, 16));
blueSliderPanel.add(redLabel, BorderLayout.WEST);
blueSliderPanel.add(redSlider, BorderLayout.CENTER);
blueSliderPanel.add(redField, BorderLayout.EAST);
colorSliderPanel.add(redSliderPanel);
colorSliderPanel.add(greenSliderPanel);
colorSliderPanel.add(blueSliderPanel);
return colorSliderPanel;
}
doesn't work as intended:
It's supposed to stack the three panels I make on top of each other. I add it to my JFrame like this:
JPanel sliderPanel = createColorSliderPanel();
...
contentPane.add(sliderPanel, BorderLayout.SOUTH);
Any clue why it doesn't display properly?
The problem is, you only adding the redSlider/Field/Label to all of the other panels
redSliderPanel.add(redLabel, BorderLayout.WEST);
redSliderPanel.add(redSlider, BorderLayout.CENTER);
redSliderPanel.add(redField, BorderLayout.EAST);
greenSliderPanel.add(redLabel, BorderLayout.WEST);
greenSliderPanel.add(redSlider, BorderLayout.CENTER);
greenSliderPanel.add(redField, BorderLayout.EAST);
blueSliderPanel.add(redLabel, BorderLayout.WEST);
blueSliderPanel.add(redSlider, BorderLayout.CENTER);
blueSliderPanel.add(redField, BorderLayout.EAST);
Doing this, will remove the redSlider, redField and redLabel from the container it was previous added to meaning that it will only appear on the blueSliderPanel.
Don't use setPreferred/Minimum/MaximumSize, you don't control the metrics by which a component measures it's required sizes. Provide sizing hints to the components so that they can make better decisions.
redField = new JTextField(4);
greenField = new JTextField(4);
blueField = new JTextField(4);
Then don't add the fields to sub panels first
Then don't add your fields to separate panels first, but, you might find that something like GridBagLayout makes a better choice...
JPanel colorSliderPanel = new JPanel(new GridBagLayout());
redSlider = new JSlider();
greenSlider = new JSlider();
blueSlider = new JSlider();
redField = new JTextField(4);
greenField = new JTextField(4);
blueField = new JTextField(4);
redField.setEditable(false);
greenField.setEditable(false);
blueField.setEditable(false);
JLabel redLabel = new JLabel("Red");
JLabel greenLabel = new JLabel("Green");
JLabel blueLabel = new JLabel("Blue");
addTo(colorSliderPanel, redLabel, redSlider, redField, 0);
addTo(colorSliderPanel, greenLabel, greenSlider, greenField, 1);
addTo(colorSliderPanel, blueLabel, blueSlider, blueField, 2);
And the addTo method...
protected void addTo(JPanel parent, JLabel label, JSlider slider, JTextField field, int gridY) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = gridY;
parent.add(label, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx++;
gbc.weightx = 1;
parent.add(slider, gbc);
gbc.gridx++;
gbc.weightx = 0;
parent.add(field, gbc);
}

How to get JButton to fill BoxLayout vertically?

It's easiest to explain with a picture, so here it goes. This is how it looks right now:
I am trying to get all JButtons to to be same size, i.e to fill out the BoxLayout completely vertically.
Here is my code:
public class TestarBara extends JFrame implements ActionListener{
JButton heyy;
public static void main(String[] args){
new TestarBara();
}
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
public TestarBara(){
super("knapparnshit");
panel.setLayout(new GridLayout(3,3,2,2));
for(int x=1; x < 10; x++){
String y = Integer.toString(x);
JButton button = new JButton(y);
button.addActionListener(this);
panel.add(button);
}
add(panel, BorderLayout.CENTER);
JButton b1 = new JButton("this");
JButton b2 = new JButton("does");
JButton b3 = new JButton("not");
JButton b4 = new JButton("work");
panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
panel2.add(b1);
panel2.add(Box.createRigidArea(new Dimension(0,4)));
panel2.add(b2);
panel2.add(Box.createRigidArea(new Dimension(0,4)));
panel2.add(b3);
panel2.add(Box.createRigidArea(new Dimension(0,4)));
panel2.add(b4);
panel2.setBorder(BorderFactory.createBevelBorder(1));
add(panel2, BorderLayout.WEST);
Dimension dim = panel2.getPreferredSize();
b1.setPreferredSize(dim);
b2.setPreferredSize(dim);
b3.setPreferredSize(dim);
b4.setPreferredSize(dim);
setResizable(true);
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e) {
Object button = e.getSource();
if(button instanceof JButton){
((JButton) button).setEnabled(false);
((JButton) button).setText("dead");
Toolkit.getDefaultToolkit().beep();
}
}
}
What do I need to do so the JButtons all are the same size, and all of them going all the way to the left?
The problem is BoxLayout will honour the preferredSize of the individual components, you'd be better off with a layout manager that provided you with more control, like GridBagLayout, for example...
panel2.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(0, 0, 4, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
panel2.add(b1, gbc);
panel2.add(b2, gbc);
panel2.add(b3, gbc);
gbc.insets = new Insets(0, 0, 0, 0);
panel2.add(b4, gbc);
Or GridLayout...
panel2.setLayout(new GridLayout(0, 1, 0, 4));
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);

Putting JLabel on top of component

I need to add JLabel object to specific component of my panel. I use setLabelFor method but this one adds label nex to component. How to change it to set label on top of component?
TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleField);
sampleLabel.setLabelFor(sampleField);
panel.add(sampleLabel);
^ this one puts sampleLabel next to sampleField. How to put sampleLabel on top of sampleField?
Thanks from advance.
#Edit:
I use something like this:
public TabBody()
{
setLayout(new BorderLayout());
nameField = new TextField();
//nameField.setSize(new Dimension(200, 200));
//revalidate();
nameLabel = new JLabel("name test");
amountLabel = new JLabel("amount test");
amountField = new TextField();
unitsBox = new JComboBox(units);
unitsBox.setSelectedIndex(3);
nameLabel.setLabelFor(nameField);
amountLabel.setLabelFor(amountField);
add(nameLabel, BorderLayout.NORTH);
add(amountLabel, BorderLayout.NORTH);
add(nameField);
add(amountField);
add(unitsBox);
}
and outcome is:
And I need something like this:
Your JPanel use FlowLayout by default, that place component one by one in a row. Because of you have that effect.
You need to use a proper LayoutManager for example use BorderLayout :
import java.awt.BorderLayout;
import java.awt.TextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Example {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleField);
sampleLabel.setLabelFor(sampleField);
panel.add(sampleLabel,BorderLayout.NORTH);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
If you need to place components in grid, one by one for example try to use GridBagLayout
JPanel panel = new JPanel(new GridBagLayout());
JTextField sampleField = new JTextField(5);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleLabel,c);
c.gridy++;
panel.add(sampleField,c);
c.gridy++;
panel.add(new JLabel("sample text 2"),c);
c.gridy++;
panel.add(new JTextField(5),c);
c.gridy++;
panel.add(new JTextField(5),c);
manage positions with gridx and gridy properties of GridBagConstraints
.
You must use Layout for arranging your components. For JPanel by deafult it is FlowLayout which arranges your component next to one-another. You can use GridLayout or GridBagLayout
Here is the sample code:
TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.setLayout(new GridBagLayout());
sampleLabel.setLabelFor(sampleField);
GridBagConstraints = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
panel.add(sampleLabel, gbc);
gbc.gridY=1;
panel.add(sampleField, gbc);

JScrollPane not properly stretching horizontal distance in GridBagLayout

I've seen other posts on this subject, but the solutions they found do not apply to me. I am setting a weighted value and using the c.fill = GridBagConstraints.BOTH constraints as well.
I'm including the whole GUI code I have, just in case my mistake is coming form something other than the GridBagLayout.
I want the scrollable text block on the right to expand the remaining space within the GUI and I have set all the variables that should be attributed to that and yet it still isn't working. What am I doing wrong?
My result:
import java.awt.*;
import javax.swing.*;
public class TestCode extends JFrame {
JTextArea textArea = new JTextArea ();
JComboBox <String> typeComboBox;
JTextField searchField;
JTextField fileField;
public TestCode(){
setTitle ("GUI Test");
setSize (600, 300);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
JScrollPane scrollPane = new JScrollPane(textArea);
JButton readButton = new JButton("Read File");
JButton displayButton = new JButton("Display");
JButton searchButton = new JButton("Search");
searchField = new JTextField(10);
fileField = new JTextField(15);
typeComboBox = new JComboBox <String> ();
typeComboBox.addItem("Index");
typeComboBox.addItem("Type");
typeComboBox.addItem("Name");
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
container.setPreferredSize(new Dimension(250, 100));
JPanel filePanel = new JPanel();
filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
filePanel.add(new JLabel("Source file", SwingConstants.LEFT));
JPanel filePanelTop = new JPanel();
filePanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
filePanelTop.add(fileField);
JPanel filePanelBottom = new JPanel();
filePanelBottom.setLayout(new FlowLayout(FlowLayout.RIGHT));
filePanelBottom.add(readButton);
filePanelBottom.add(displayButton);
filePanel.add(filePanelTop);
filePanel.add(filePanelBottom);
filePanel.setMaximumSize(filePanel.getPreferredSize());
filePanel.setBorder(BorderFactory.createTitledBorder("Import File"));
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS));
searchPanel.add(new JLabel("Search target", SwingConstants.LEFT));
JPanel searchPanelTop = new JPanel();
searchPanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
searchPanelTop.add(searchField);
searchPanelTop.add(typeComboBox);
searchPanel.add(searchPanelTop);
searchPanel.add(searchButton);
searchPanel.setMaximumSize(searchPanel.getPreferredSize());
searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects"));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
container.add(filePanel, c);
c.gridx = 0;
c.gridy = 1;
container.add(searchPanel, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTHWEST;
container.add(scrollPane, c);
add(container, BorderLayout.WEST);
validate();
} // end method toString
public static void main(String[] args){
TestCode run = new TestCode();
}
} // end class Treasure
//add(container, BorderLayout.WEST);
add(container);
The West contrains the components to their preferred width. The default is the CENTER which allows components to expand to fill the space available.
Also, the main structure of you code is wrong. You should be adding all the component to the frame first and then invoke:
frame.pack();
frame.setVisible(true);
Then there is no need for the validate().

Categories

Resources