I used Grid layout first, Then i realized i wanted to use spring layout instead. When i add Spring layout Panel nothing wants to show
public class ComplexWindow extends JFrame {
public ComplexWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 300, 420);
JPanel mainPanel = (JPanel) getContentPane();
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.setLayout(new BorderLayout());
SpringLayout layout = new SpringLayout();
JPanel textPanel = new JPanel(layout);
JLabel lblName = new JLabel("Ime:");
textPanel.add(lblName);
JTextField txtName = new JTextField();
txtName.setColumns(10);
textPanel.add(txtName);
JLabel lblSurname = new JLabel("Prezime:");
textPanel.add(lblSurname);
JTextField txtSurname = new JTextField();
txtSurname.setColumns(10);
textPanel.add(txtSurname);
layout.putConstraint(SpringLayout.EAST, lblName, 5,
SpringLayout.WEST, txtName);
layout.putConstraint(SpringLayout.SOUTH, lblName, 5,
SpringLayout.NORTH, lblSurname);
layout.putConstraint(SpringLayout.SOUTH, txtName, 5,
SpringLayout.NORTH, txtSurname);
layout.putConstraint(SpringLayout.EAST, lblSurname, 5,
SpringLayout.WEST, txtSurname);
mainPanel.add(textPanel, BorderLayout.NORTH);
}
public static void main(String [] args) {
SwingUtilities.invokeLater(() -> {
ComplexWindow window = new ComplexWindow();
window.pack();
window.setVisible(true);
});
}
}
As this documentation says, SpringLayout is not for manual laying out of components:
https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html
SpringLayout is, however, very low-level and as such you really should
only use it with a GUI builder, rather than attempting to code a
spring layout manager by hand.
And also, a utility class used in above tutorial layout.SpringUtilities is not included in JDK.
So, I recommend you use GridBagLayout for this.
Related
How do I set my labels in a specific locations? I want to have some in a row, and others in a column. Im trying to set them with .setBorder(new EmptyBorder(coordX, coordY, 20, 140)); but doesnt seems to work.
Here is the code (remember to replace those .png):
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Images extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
new Images();
}
public Images() {
setTitle("myTitle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblNombreequipo = new JLabel("trying: ");
lblNombreequipo.setBorder(new EmptyBorder(10, 40, 10, 10));
contentPane.add(lblNombreequipo, BorderLayout.PAGE_START);
JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
contentPane.add(new JScrollPane(scrollPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
for (int i = 0; i < 50; i++) {
JLabel myLabel1 = new JLabel(new ImageIcon("myImage1.png"));
myLabel1.setBorder(new EmptyBorder(100, 100, 20, 140));
scrollPanel.add(myLabel1);
JLabel myLabel2 = new JLabel(new ImageIcon("myImage2.png"));
myLabel2.setBorder(new EmptyBorder(300, 100, 20, 140));
scrollPanel.add(myLabel2);
JLabel myLabel3 = new JLabel(new ImageIcon("myImage3.png"));
myLabel3.setBorder(new EmptyBorder(100, 100, 20, 140));
scrollPanel.add(myLabel3);
}
pack();
Dimension d = getSize();
setSize(new Dimension(d.width, 250));
setLocationByPlatform(true);
setMinimumSize(getSize());
setVisible(true);
}
}
How do I set my labels in a specific locations?
You use a layout manager effectively.
JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
This says you want a single columns of components.
Read the section from the Swing tutorial on How to Use GridLayout for more information on how the layout manager works and for working examples you can download and play with.
If GridLayout isn't what you are trying to achieve then take a look at the other section from the tutorial for the various layout managers. Maybe a GridBagLayout which is more complicated to use is what you are looking for.
Im trying to set them with .setBorder(new EmptyBorder(coordX, coordY, 20, 140));
That is not what a Border is for. A Border simply adds extra space to a component. Read the section from the Swing tutorial on How to Use Borders for more information and working example.
I have the following structure in a frame:
Frame->contentPane panel->topPanel panel->table
This is the code:
private JPanel contentPane;
private JTable table;
private JPanel topPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShowList frame = new ShowList();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShowList() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 675, 433);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
topPanel = new JPanel();
topPanel.setBounds(76, 76, 491, 245);
contentPane.add(topPanel);
topPanel.setLayout(null);
table = new JTable();
table.setBounds(0, 0, 491, 245);
topPanel.add(table);
}
What I want to achieve is that when I resize the frame making the window bigger, the topPanel and the table that it is contained by this one resize also and do not stay the same size as they were before resizing the frame. I have read about Layouts but I can not make it work. Any suggestion?
Use a LayoutManager to control the size and position of the components inside of your frame. Avoid setting the layout manager of your panels as null.
I recommend you to take a look at this link: "A Visual Guide to Layout Managers" to learn more about the different kinds of layouts you can use.
In your code, for example, I would use a BorderLayout:
contentPane.setLayout(new BorderLayout());
...
contentPane.add(topPanel, BorderLayout.CENTER);
topPanel.setLayout(new BorderLayout());
...
topPanel.add(table, BorderLayout.Center);
By the way, I suppose your class ShowList extends JFrame because methods like setDefaultCloseOperation gives you an error if not, right?
I hope it helps.
I'm currently working on a browser in Java. I want to have a back button on the top left and to its right a JTextfield with the URL. I want the button to always have the same size but the textfield to change it's width to match the JFrame's width. It doesn't work with BorderLayout and I've tried this:
SpringLayout sl = new SpringLayout();
setLayout(sl);
sl.putConstraint(SpringLayout.WEST, back, 5, SpringLayout.WEST, this);
sl.putConstraint(SpringLayout.NORTH, back, 5, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.WEST, addressBar, 5, SpringLayout.EAST, back);
sl.putConstraint(SpringLayout.NORTH, addressBar, 5, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.SOUTH, back, 25, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.SOUTH, addressBar, 25, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.EAST, addressBar, 5, SpringLayout.EAST, this);
add(back);
add(addressBar);
where "back" is a JButton and addressBar a JTextField. The button seems to work but the addressBar just doen't draw at all.
Any suggestions?
There are many ways to solve this, and one in fact involves BorderLayout by nesting JPanels. Put the button into a BorderLayout.WEST position of a BorderLayout using container, but the JTextField BorderLayout.CENTER in the same container, and then put that container into the main container BorderLayout.CENTER.
GridBagLayout could also solve this, but again, often the best/simplest solution will involve nesting JPanels (for your containers), each with its own layout manager.
Edit
For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
public class BrowserFoo {
private static void createAndShowGui() {
JPanel topPanel = new JPanel(new BorderLayout(2, 2));
topPanel.add(new JButton("Back"), BorderLayout.WEST);
topPanel.add(new JTextField(20), BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(Box.createRigidArea(new Dimension(400, 400)));
JFrame frame = new JFrame("BrowserFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Note that if you re-size this GUI, the textarea and button remain in proper location.
I want to create a JFrame by hand and use spring layout to do this. But, my finally output is not good. The space between my rows is so much, and between my radio buttons too:
My code:
public final class NewUserFrame1 extends JFrame {
public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
pack();
}
public JPanel rowComponent() {
JPanel panel = new JPanel();
JLabel fnamelbl = new JLabel("First name");
JLabel lnamelbl = new JLabel("Last Name");
JLabel fntemp = new JLabel();
JLabel lntemp = new JLabel();
JTextField fntf = new JTextField(10);
JTextField lntf = new JTextField(10);
JLabel gndlnl = new JLabel("Gender");
JRadioButton malerb = new JRadioButton("Male");
JRadioButton femalerb = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(malerb);
bgroup.add(femalerb);
JLabel registnm = new JLabel("Registration ID is:");
JLabel showreglbl = new JLabel();
JLabel regtemp = new JLabel();
panel.add(fnamelbl);
panel.add(fntf);
panel.add(fntemp);
panel.add(lnamelbl);
panel.add(lntf);
panel.add(lntemp);
panel.add(gndlnl);
panel.add(malerb);
panel.add(femalerb);
panel.add(registnm);
panel.add(showreglbl);
panel.add(regtemp);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NewUserFrame1 newUserFrame1 = new NewUserFrame1();
}
});
}
}
Now:
Instead of calling setSize call pack on JFrame within your NewUserFrame1 constructor.
public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
//setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
pack();
}
Also change the parameters of SpringUtilities.makeCompactGrid method in following way:
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);//change yPad to 4 instead of 100. It sets the vertical height between two rows
Your code is not compilable (missing imports).
You wrote:
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 100);
The last argument is yPad. Change this to 10 (or lower value if you want), for example:
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 10);
But still - label will be to high etc., but it's a different issue. Keep playing with panel's size and your component's size.
In case of radio buttons - change
panel.add(malerb);
panel.add(femalerb);
To something like:
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
radioPanel.add(malerb);
radioPanel.add(femalerb);
panel.add(radioPanel);
panel.add(new JLabel());
The last line is needed because you declared your layout to have 3 columns.
Here's what I actually want to put on a panel:
First logical block:
radio button 1 text field icon button
radio button 2 text field icon button
check box
Second logical block:
Label Spinner
Button
My first decision is to make Vertical Box Layout and put there two Horizontal Box Layouts - for each logical block. But the problem is with these blocks, what layouts to choose to describe this structure? I dislike GridBagLayout - it is very composite and difficult to understand, especially when code isn't yours. For the moment I see that Flow Layout and Grid Layout can be used. But Grid Layout, for example, stretches buttons to the width of a cell and if a button is with icon only it, it looks very strange then.
Hope you can advise me something.
For the first case you can use a simple GridLayout on the JPanel with 3 Rows each having a separate JPanel with FlowLayout having constraints, FLowLayout.LEFT. Have a look at this code example :
import java.awt.*;
import javax.swing.*;
public class ExampleLayout
{
private void displayGUI()
{
JFrame frame = new JFrame("Example Layout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridLayout(0, 1, 5, 5));
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
JTextField tfield1 = new JTextField(10);
JButton button1 = new JButton("Button 1");
topPanel.add(rbut1);
topPanel.add(tfield1);
topPanel.add(button1);
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JRadioButton rbut2 = new JRadioButton("RadioButton 2", false);
JTextField tfield2 = new JTextField(10);
JButton button2 = new JButton("Button 2");
middlePanel.add(rbut2);
middlePanel.add(tfield2);
middlePanel.add(button2);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JCheckBox cbox = new JCheckBox("CheckBox 1", false);
bottomPanel.add(cbox);
contentPane.add(topPanel);
contentPane.add(middlePanel);
contentPane.add(bottomPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new ExampleLayout().displayGUI();
}
});
}
}
OUTPUT :
And for the Second case, simply add first two components to the JPanel having default Layout. And for the third components, simply add components on to a JPanel having GridBagLayout, with no constraints.
EDIT #1 :
Or you can use this approach, for your second block.
import java.awt.*;
import javax.swing.*;
public class ExampleLayout
{
private void displayGUI()
{
JFrame frame = new JFrame("Example Layout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(0, 1, 5, 5));
JPanel topPanel = new JPanel();
//topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JLabel label1 = new JLabel("Label 1", JLabel.CENTER);
JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
topPanel.add(label1);
topPanel.add(rbut1);
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new GridBagLayout());
JButton button1 = new JButton("Button 1");
middlePanel.add(button1);
basePanel.add(topPanel);
basePanel.add(middlePanel);
contentPane.add(basePanel, BorderLayout.PAGE_START);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new ExampleLayout().displayGUI();
}
});
}
}