My goal is to have my components resize according to my GridBagConstraints, but for some reason, when my applet is run, the components appear, but do not fill the entire applet like I expected. here is my code:
ServerPanel:
public class ServerPanel extends JPanel{
public ServerPanel(){
setLayout(new GridBagLayout()); //use gridbag layout
GridBagConstraints gbc = new GridBagConstraints();
JButton reverse = new JButton("Reverse text");
JButton send = new JButton("Send text");
JButton clear = new JButton("Clear text");
JTextField text = new JTextField("Test");
JScrollPane scrollPane = new JScrollPane(text, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//Reverse button
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = gbc.HORIZONTAL;
add(reverse, gbc);
//send button
gbc.gridy = 1;
add(send, gbc);
//clear button
gbc.gridy = 2;
add(clear,gbc);
//text field
gbc.gridy = 3;
gbc.ipadx = 20;
gbc.ipady = 20;
gbc.fill = gbc.BOTH;
add(scrollPane, gbc);
}
}
the relevant code from ServerApplet which extends JApplet:
public void init(){
//dim = getSize();
//create the GUI in the EDT
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
//System.out.println(dim);
setLayout(new BorderLayout());
add(new ServerPanel(), BorderLayout.CENTER);
}
});
}
What happens is that the right components get created, and the panel is centered in the applet, but it does not expand to fill the entire applet. Any help is appreciated. Thanks!
When I see this:
but for some reason, when my applet is run, the components appear, but do not fill the entire applet like I expected....
used in conjunction with GridBagLayout, I look to see if the weightx and weighty fields are being set on the GridBagConstraints, because a symptom of them being left at their default values of 0 is that the components all bunch up in the center.
Solution: set these fields where and when needed. If a component should expand in the x direction, then give it a + weightx, perhaps 1.0, and likewise for weighty.
Related
I am trying to make a login page but unfortunately my formPanel's border is so out of bounds. Here in the pic you can see the titled border is way out. I need it to be more around my panel with login form:
I see that when creating this formPanel it is that big and the border just surround it. I tried with setPrefferedSize but its not working. How can I fix it?
Here is my code:
public class LoginPanel extends JPanel {
private JLabel title;
public LoginPanel() {
this.setBackground(new Color(0, 128, 43));
this.setBorder(new EmptyBorder(10, 10, 10, 10));
this.setLayout(new BorderLayout());
//adding the title
title = new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>");
title.setForeground(Color.BLACK);
JPanel titlePanel = new JPanel();
titlePanel.setBackground(new Color(0, 128, 43));
titlePanel.add(title);
this.add(titlePanel, BorderLayout.PAGE_START);
//creating the login form
JPanel formPanel = new JPanel(new GridBagLayout());
formPanel.setBackground(new Color(0, 128, 43));
//gbc
GridBagConstraints gbc = new GridBagConstraints();
//gbc for username
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(5, 0, 0, 0);
JLabel username = new JLabel("Username: ");
username.setForeground(Color.BLACK);
formPanel.add(username, gbc);
//gbc for textfield
gbc.gridx = 1;
gbc.gridy = 0;
JTextField usernameField = new JTextField(10);
formPanel.add(usernameField, gbc);
//gbc for pass
gbc.gridx = 0;
gbc.gridy = 1;
JLabel password = new JLabel("Password: ");
password.setForeground(Color.BLACK);
formPanel.add(password, gbc);
//gbc for pass field
gbc.gridx = 1;
gbc.gridy = 1;
JPasswordField passField = new JPasswordField(10);
formPanel.add(passField, gbc);
//gbc for button
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
JButton loginButton = new JButton("Login");
formPanel.add(loginButton, gbc);
//add border to the form panel
TitledBorder title = BorderFactory.createTitledBorder("Login");
formPanel.setBorder(title);
this.add(formPanel, BorderLayout.CENTER);
}
}
I tried with setPrefferedSize but its not working.
Don't try to manage the preferredSize of a component.
I would guess the problem is that you are using setSize(...) instead of pack().
You should be using pack() AFTER all the components have been added to the frame. Then all the components will be displayed at their preferred size.
Edit:
I originally misread your question and changed my original answer. The point in my original answer of using the "wrapper" panel is to give extra space to the wrapper panel, while keeping the "formPanel" at a fixed size. This way the titled border will remain around the formPanel even as the frame size is changed.
So again the basic approach for this type of solution is:
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(formPanel, new GridBagConstraints());
this.add(wrapper, BorderLayout.CENTER);
//this.add(formPanel, BorderLayout.CENTER);
I took your LoginPanel class as is, and wrote a Main class around it to start the GUI.
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LoginPanel());
frame.pack();
frame.setVisible(true);
});
}
}
Especially note, I did no explicit setting of the size, only a pack() call which resizes everything to its preferred size according to your layout.
The resulting layout looks perfectly fine to me.
So the conclusion is: Your LoginPanel is fine, but probably there is something wrong in your other code which you didn't post here.
Using GridbagLayout, I'm trying to display a JTable. Here's my code (simplified). The jpanel code that I'll put is shown after a button click.
My Panel :
optionspane = new JPanel();
optionspane.setLayout(new GridBagLayout());
optionspane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
My Panel's Children :
GridBagConstraints dgbc=new GridBagConstraints();
dgbc.gridx=0;
dgbc.gridy=0;
dgbc.weightx = 1.0;
dgbc.weighty = 1.0;
dgbc.anchor=GridBagConstraints.CENTER;
dgbc.fill=GridBagConstraints.BOTH;
dgbc.insets=new Insets(5,10,10,5);
datapane.add(ServiceInterface.eighthpanel(), dgbc);
dgbc.gridx=0;
dgbc.gridy=1;
gbc.anchor=GridBagConstraints.SOUTH;
dgbc.fill=GridBagConstraints.BOTH;
dgbc.insets=new Insets(5,0,5,5);
datapane.add(ServiceInterface.ninethpanel(), dgbc);
Here is the panel method that contains the JPanel :
public static JPanel eighthpanel() throws IOException{
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
pane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JLabel datawelcome=new JLabel("Please Enter the QoS values'");
datawelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill=GridBagConstraints.BOTH;
gbc.fill=GridBagConstraints.REMAINDER;
gbc.insets=new Insets(10,20,10,20);
pane.add(datawelcome, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx=1;
gbc.weightx=1;
gbc.fill=GridBagConstraints.BOTH;
gbc.fill=GridBagConstraints.REMAINDER;
gbc.insets=new Insets(10,20,10,20);
//pane.add(new JScrollPane(displayJTable), gbc);
pane.add(myscrol, gbc);
return pane;
}
Here is how I fill my JTable (inside a button listener):
mytableclass=new Mytable();
displayJTable = new JTable(myModel);
displayJTable.getColumnModel().getColumn(0).setPreferredWidth(60);
displayJTable.setPreferredScrollableViewportSize(new Dimension(525,250));
displayJTable.setFillsViewportHeight(true);
displayJTable.setLocation(5,5);
myscrol.add(displayJTable);
myscrol.getViewport().setViewPosition(new Point(0,0));
myscrol.setPreferredSize(new Dimension(600,400));
principalpane.setVisible(false);
datapane.setVisible(true);
}
}});
I resolved my problem. I added an other panel (default layout) and I added the scrollPane to this panel inside the button.actionlistener.
I think because of the Static characteristic of the gridbaglayout.
I am a beginner in Java Swing and I am trying to put a multiple JPanels in a JScrollPanel. The matter is, the JSCrollPannel (named jp in the code) should not fill all the JFrame but it does even if I fix a size with setSize() and a maximal size with setMaximalSize(). What is the trouble? How can I make the JSCrollPane smaller than the JFrame?
package GUI;
import java.awt.*;
import javax.swing.*;
public class MultiPanels {
private JScrollPane getContent() {
Dimension d = new Dimension(300,200);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(getPanel(d, 6, Color.red), gbc);
panel.add(getPanel(d, 4, Color.green.darker()), gbc);
panel.add(getPanel(d, 4, Color.orange), gbc);
panel.add(getPanel(d, 12, Color.blue), gbc);
panel.add(getEmptyPanel(d), gbc);
return new JScrollPane(panel);
}
private JScrollPane getPanel(Dimension d, int rows, Color color) {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(color);
GridBagConstraints gbc= new GridBagConstraints();
gbc.insets = new Insets(10,5,10,5);
gbc.weightx = 1.0;
for(int i = 0, j = 1; i < rows; i++) {
gbc.gridwidth = GridBagConstraints.RELATIVE;
panel.add(new JButton(String.valueOf(j++)), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(new JButton(String.valueOf(j++)), gbc);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
private JScrollPane getEmptyPanel(Dimension d) {
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(0,0,Color.red,
0,h,Color.cyan);
((Graphics2D)g).setPaint(gp);
g.fillRect(0,0,w,h);
}
};
panel.setPreferredSize(new Dimension(300,400));
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
public static void main(String[] args) {
JFrame f = new JFrame();
JScrollPane jp = new MultiPanels().getContent();
jp.setSize(new Dimension(200, 200));
jp.setMaximumSize(new Dimension(200,200));
jp.setPreferredSize(new Dimension(200,200));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(jp);
f.setSize(400,400);
f.setLocation(200,200);
f.setResizable(false);
f.setVisible(true);
}
}
Any time things don't size or arrange correctly, you have to look into Layouts.
Generally, spend more time on:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
To be specific, the default layout of a JPanel and JFrame is BorderLayout which is a very simple layout manager indeed. When you add to a component managed by BorderLayout without saying where, it is automatically added to the center and fills to use all available space:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#border
It is possible to use "none" (Absolute Positioning) as the layout, but this is almost always a bad idea and you want to think about what you really want to do with the rest of the space in the JFrame: perhaps by letting new child components, with their own size demands, take up some of the space that the main panel is now swallowing up.
Why are the JLabel, JTextField and JButton components on the FieldBox panel not displaying?
Please let me know if I can add anything to this question to make it more answerable!
The field box (one of which is created for each of several instance fields):
public class FieldBox extends javax.swing.JPanel {
JLabel label;
JTextField textField;
public FieldBox() {
setBackground(Color.RED);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
label = new JLabel();
gbc.gridx = 0;
gbc.gridy = 0;
add(label,gbc);
textField = new JTextField();
gbc.gridx = 1;
gbc.gridy = 0;
add(textField, gbc);
JButton editBtn = new JButton("edit");
gbc.gridx = 2;
gbc.gridy = 0;
add(editBtn, gbc);
JButton saveBtn = new JButton ("save");
gbc.gridx = 3;
gbc.gridy = 0;
add(saveBtn, gbc);
label.setVisible(true);
label.setBackground(Color.yellow);
setVisible(true);
initComponents();
}
The panel onto which the boxes are added: (the panel appears. I know this panel loads because I can see its background. I can also see the "tit" JLabel, which I'm using for testing. Also, when I load the boxes onto it using a gridLayout (as shown below), I see the background of one of the boxes, but none of its contents are displayed, and also there should be three boxes shown but I only see one. Using a GridBagLayout
public class ShowBook extends javax.swing.JPanel {
public ShowBook(Book b) {
setLayout(new GridLayout(1,6));
setBackground(Color.GREEN);
String[] fieldTitles = {"title", "catalog", "publisher" };
JLabel tit = new JLabel("tit");
add(tit);
for (String s : fieldTitles){
FieldBox fb = new FieldBox();
fb.label.setText(s + ": ");
fb.textField.setText(getField(b, s));
System.out.println("in text field" + fb.textField.getText());
fb.revalidate();
fb.setVisible(true);
add (fb);
}
revalidate();
setVisible(true);
}
The panel onto which the ShowBook panel (above) is added. This panel also contains a listbox, which lists books in the collection, and a button to launch a ShowBook panel with the selected book as parameter.
public class ShowLib extends javax.swing.JPanel {
/**
* Creates new form ShowLib
*/
public ShowLib(Library l) {
setLayout(new BorderLayout()) ;
setBackground(Color.WHITE);
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
final JList bookList = new JList();
bookList.setVisible(true);
bookList.setPreferredSize(new Dimension(150, 600));
bookList.setBackground(Color.yellow);
DefaultListModel dlm = new DefaultListModel();
for (Book b : l.libList){
dlm.addElement(b);
}
bookList.setModel(dlm);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = gbc.BOTH;
listPanel.add(bookList, gbc);
JButton showBtn = new JButton("show");
gbc.gridy = gbc.RELATIVE;
listPanel.add(showBtn, gbc);
showBtn.addActionListener(new ActionListener () {
#Override
public void actionPerformed(ActionEvent e){
Book b = (Book) bookList.getSelectedValue();
ShowBook db = new ShowBook (b);
System.out.println("Clicked");
add (db, BorderLayout.CENTER);
revalidate();
}
});
revalidate();
setVisible(true);
add(listPanel, BorderLayout.WEST);
}
Where the ShowLib panel is added to the main JFrame. I know this works because that panel displays properly
void showLib() {
ShowLib sl = new ShowLib(l);
sl.setVisible(true);
setContentPane(sl);
revalidate();
// this.revalidate();
// this.pack();
System.out.println("showlibClicked");
}
The problem was the line initComponents(); at the bottom of the FieldBox class.
Lesson learned: If you're not using NetBeans visual GUI editor, GET RID OF THIS LINE!
I want to make with swing this interface:
And when I resise it I want all the subpanels and buttons to be resized like this:
Not only main window to be resized. I am using GridBagLayout. And I dont know how to stick the borders of the panel with GridBagLayout to the borders of the Frame in that way, when I am resizing the frame the panel also to be resized.
I normally use nested layouts for this.
Use a JPanel with a BorderLayout as the base.
Store your central components in a JPanel, and add this to the CENTER of the BorderLayout.
Store your bottom components in two separate JPanels.
Create another JPanel with a GridLayout of 1 row and 2 columns.
Add the two JPanels to it in the correct order.
Add this JPanel to the SOUTH of the BorderLayout.
The property to achieve this, i.e. when JFrame is resized the JPanel should also resize itself, will be GridBagConstraints.BOTH. It appears to me that your Left JButton is a bit smaller than the Right JButton. If you really wanted to achieve this with GridBagLayout, here I have crafted a small sample code for your help, have a look and ask any question that may arise :
import java.awt.*;
import javax.swing.*;
public class GridBagExample
{
private JPanel contentPane;
private void displayGUI()
{
JFrame frame = new JFrame("GridBag Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
JPanel centerPanel = new JPanel();
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.CYAN);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.weightx = 1.0;
gbc.weighty = 0.9;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.BOTH; // appears to me this is what you wanted
contentPane.add(centerPanel, gbc);
JButton leftButton = new JButton("Left");
JButton rightButton = new JButton("Right");
gbc.gridwidth = 1;
gbc.gridy = 1;
gbc.weightx = 0.3;
gbc.weighty = 0.1;
contentPane.add(leftButton, gbc);
gbc.gridx = 1;
gbc.weightx = 0.7;
gbc.weighty = 0.1;
contentPane.add(rightButton, gbc);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new GridBagExample().displayGUI();
}
});
}
}