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);
}
Related
My class has 4 parts of textfields, 1 on the top, 1 on the left, 2 on the right. As I am using containers, so I think I need to put those 2 together under 1 big container so that I can set it's location in "East", but I'm not sure how...
private void display1(Container container){
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraint = new GridBagConstraints();
panel.setLayout(new GridLayout(1,1));
display1 = new JTextField("testing");
constraint.gridx = 4;
constraint.gridy = 0;
panel.add(displaytrain, constraint);
container.add(panel);
}
private void display2(Container container){
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraint = new GridBagConstraints();
panel.setLayout(new GridLayout(1,1));
display2 = new JTextField("testing");
constraint.gridx = 4;
constraint.gridy = 0;
panel.add(displaytrain, constraint);
container.add(panel);
}
private void bigDisplay(Container container){
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraint = new GridBagConstraints();
//putting the dispay1 and display2 here...?
container.add(panel, "East");
}
Do you want to insert display1 () and 2 into the created panel in bigDisplay?
If so, in each displayX, instead of adding to the container the panel you created, just return it and add it on bigDisplay to the created panel there
private JPanel display1(){
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraint = new GridBagConstraints();
panel.setLayout(new GridLayout(1,1));
display1 = new JTextField("testing");
constraint.gridx = 4;
constraint.gridy = 0;
panel.add(display1, constraint);
return panel;
}
private JPanel display2(){
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraint = new GridBagConstraints();
panel.setLayout(new GridLayout(1,1));
display2 = new JTextField("testing");
constraint.gridx = 4;
constraint.gridy = 0;
panel.add(display2, constraint);
return panel;
}
private void bigDisplay(Container container){
JPanel panel = new JPanel(new GridBagLayout());
panel.setLayout(new GridLayout(1,1));
GridBagConstraints constraint = new GridBagConstraints();
panel.add (display1 (), constraint);
panel.add (display2 (), constraint);
container.add(panel, "East");
}
I'm trying to get the config panel to take up the top of the screen, and then have the input and output panels side-by-side. I'm also trying to get the text areas to be 70 characters wide each and 30 rows tall. However, right now, the config panel isn't showing up at all, and the text areas are only 35 characters wide and 2 rows tall. I've followed all the examples and tutorials I've found. What am I doing wrong?
public class BorderWrapper {
public static void main(String[] args) {
//Create frame
JFrame frame = new JFrame("Border Wrapper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create main panel
MainPanel panel = new MainPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
//Display frame
Dimension minSize = new Dimension(650, 375);
frame.setPreferredSize(minSize);
frame.setMinimumSize(minSize);
frame.pack();
frame.setVisible(true);
}
}
public class MainPanel extends JPanel {
private static final Font INPUT_FONT = new Font("Monospaced", Font.PLAIN, 12);
private JTextArea inputArea, outputArea;
private JTextField titleField, topBorderField, sideBorderField;
public MainPanel() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//Set up config panel
JPanel configPanel = new JPanel();
configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.X_AXIS));
configPanel.setMaximumSize(new Dimension(400, 200));
titleField = new JTextField(25);
titleField.setFont(INPUT_FONT);
topBorderField = new JTextField(1);
topBorderField.setFont(INPUT_FONT);
sideBorderField = new JTextField(4);
sideBorderField.setFont(INPUT_FONT);
configPanel.add(new JLabel("Title:"));
configPanel.add(titleField);
configPanel.add(new JLabel("Top border:"));
configPanel.add(topBorderField);
configPanel.add(new JLabel("Side border:"));
configPanel.add(sideBorderField);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
add(configPanel, c);
//Set up Input panel
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputArea = new JTextArea("Type or paste your stuff here . . .");
inputArea.setFont(INPUT_FONT);
inputArea.setLineWrap(true);
inputArea.setWrapStyleWord(true);
inputArea.setColumns(75);
JScrollPane inputPane = new JScrollPane(inputArea);
inputPane.setMinimumSize(new Dimension(250, 400));
JLabel inputLabel = new JLabel("Text Box");
inputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
inputPanel.add(inputLabel);
inputPanel.add(inputPane);
inputPanel.setMinimumSize(new Dimension(250, 400));
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
add(inputPanel, c);
//Set up Output panel
JPanel outputPanel = new JPanel();
outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
outputArea = new JTextArea();
outputArea.setFont(INPUT_FONT);
outputArea.setLineWrap(true);
outputArea.setWrapStyleWord(true);
outputArea.setColumns(75);
JScrollPane outputPane = new JScrollPane(outputArea);
outputPane.setMinimumSize(new Dimension(250, 400));
JLabel outputLabel = new JLabel("Wrapped Output");
outputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
outputPanel.add(outputLabel);
outputPanel.add(outputPane);
outputPanel.setMinimumSize(new Dimension(250, 400));
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 1;
add(outputPanel, c);
}
}
Originally, I was going to try to use a BorderLayout, since it seemed that made the most sense for the layout I was trying to make, but that did an even worse job when I set them to BorderLayout.WEST and BorderLayout.EAST.
Have modified your program to use BorderLayout in the MainPanel and few other minor changes to get the desired look and feel.Check if this helps.
public class BorderWrapper {
public static void main(String[] args) {
// Create frame
JFrame frame = new JFrame("Border Wrapper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create main panel
MainPanel panel = new MainPanel();
frame.getContentPane().add(panel);
// Display frame
Dimension minSize = new Dimension(650, 375);
frame.setPreferredSize(minSize);
frame.setMinimumSize(minSize);
frame.pack();
frame.setVisible(true);
}
}
class MainPanel extends JPanel {
private static final Font INPUT_FONT = new Font("Monospaced", Font.PLAIN, 12);
private JTextArea inputArea, outputArea;
private JTextField titleField, topBorderField, sideBorderField;
public MainPanel() {
setLayout(new BorderLayout());
// Set up config panel
JPanel configPanel = new JPanel();
configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.X_AXIS));
configPanel.setMaximumSize(new Dimension(400, 200));
titleField = new JTextField(25);
titleField.setFont(INPUT_FONT);
topBorderField = new JTextField(1);
topBorderField.setFont(INPUT_FONT);
sideBorderField = new JTextField(4);
sideBorderField.setFont(INPUT_FONT);
configPanel.add(new JLabel("Title:"));
configPanel.add(titleField);
configPanel.add(new JLabel("Top border:"));
configPanel.add(topBorderField);
configPanel.add(new JLabel("Side border:"));
configPanel.add(sideBorderField);
add(configPanel, BorderLayout.NORTH);
// Set up Input panel
JPanel lowerPanel = new JPanel(new GridLayout(1, 1));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputArea = new JTextArea("Type or paste your stuff here . . .");
inputArea.setFont(INPUT_FONT);
inputArea.setLineWrap(true);
inputArea.setWrapStyleWord(true);
inputArea.setColumns(75);
JScrollPane inputPane = new JScrollPane(inputArea);
JLabel inputLabel = new JLabel("Text Box");
inputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
inputPanel.add(inputLabel);
inputPanel.add(inputPane);
lowerPanel.add(inputPanel);
// Set up Output panel
JPanel outputPanel = new JPanel();
outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
outputArea = new JTextArea();
outputArea.setFont(INPUT_FONT);
outputArea.setLineWrap(true);
outputArea.setWrapStyleWord(true);
outputArea.setColumns(75);
JScrollPane outputPane = new JScrollPane(outputArea);
JLabel outputLabel = new JLabel("Wrapped Output");
outputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
outputPanel.add(outputLabel);
outputPanel.add(outputPane);
lowerPanel.add(outputPanel);
add(lowerPanel, BorderLayout.CENTER);
}
}
I felt it convenient to use BorderLayout for this format.Anyways, you can still make few changes to the code you posted using GridBagConstraints to get the desired look.Make the below changes one by one and you will observe the differences.
1.You were aligning the MainPanel to the NORTH by using BorderLayout.But in your case the entire set of components is placed in MainPanel,so better place it in center.So instead of NORTH use below :(after this change,you will see the complete input and output panels)
MainPanel panel = new MainPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
2.You have set the dimension of the Parent frame to Dimension(height=375)
minSize = new Dimension(650, 375);
You components(configPanel=200,outputPanel=400) combined height is more than 375.Increase the height of the Parent, to about 600.
3.Instead of BoxLayout try using GridLayout for configPanel.
configPanel.setLayout(new GridLayout(1,6,5,0));
Making the above 3 changes to your existing code will get the expected output.Hope this clarifies.
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);
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);
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().