This question already has answers here:
Providing white space in a Swing GUI
(6 answers)
Closed 7 years ago.
With the code below I am making a JPanel with JTextFields and JLabels and adding that panel to another JPanel. How can I adjust the spacing between the JTextFields on infoPanel?
I have tried GridBagLayout and GridLayout with different and undesired results. The way it is right now at least gets them aligned vertically, but I can't seem to add space above and below them. Any gurus on this subject able to help out?
public DrawPanelMain() {
JPanel btnPanel = new JPanel(); //Creates a new Panel for the buttons
JPanel infoPanel = new JPanel();
JPanel fields = new JPanel();
//Text boxes for infoPanel
JTextField textField1 = new JTextField(20);
JTextField textField2 = new JTextField(20);
JTextField textField3 = new JTextField(20);
JTextField textField4 = new JTextField(20);
JTextField textField5 = new JTextField(20);
JTextField textField6 = new JTextField(20);
//JLabels for infoPanel
JLabel jLabel1 = new JLabel("Serial Number: ");
JLabel jLabel2 = new JLabel("Information: ");
JLabel jLabel3 = new JLabel("Information: ");
JLabel jLabel4 = new JLabel("Information: ");
JLabel jLabel5 = new JLabel("Information: ");
JLabel jLabel6 = new JLabel("Information: ");
//These are the buttons that will be added to the btnPanel
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
btnPanel.add(new JButton(new PushConfigAction("Push Config")));
btnPanel.add(new JButton(new ActivateAllAction("Activate All")));
btnPanel.add(new JButton(new DeactivateAllAction("Deactivate All")));
//Fields that will be added to infoPanel
fields.add(jLabel1);
fields.add(textField1);
fields.add(jLabel2);
fields.add(textField2);
fields.add(jLabel3);
fields.add(textField3);
fields.add(jLabel4);
fields.add(textField4);
fields.add(jLabel5);
fields.add(textField5);
fields.add(jLabel6);
fields.add(textField6);
//Sets border padding for the infoPanel
fields.setBorder(new EmptyBorder(20, 20, 0, 20));
//Draws border for the infoPanel
infoPanel.setBorder(BorderFactory.createRaisedBevelBorder());
//Sets layout for the fields panel
fields.setLayout(new GridLayout(6, 1));
//Add fields to infoPanel
infoPanel.add(fields);
//Add panels to tabbedPane
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
add(infoPanel, BorderLayout.EAST);
}
create a compound border
field.setBorder(BorderFactory.createCompoundBorder(
field.getBorder(),
BorderFactory.createEmptyBorder(int top, int left, int bottom, int right)));
Or put some insets
field.setMargin(new java.awt.Insets(int top, int left, int bottom, int right));
Related
import java.awt.*;
import javax.swing.*;
public class guiAs {
public static void main (String args [] )
{
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension (300,300));
frame.setTitle("Calculator");
JPanel panel = new JPanel();
// JLabel label = new JLabel();
//creating num1
JPanel panel2 = new JPanel();
JLabel lable1 = new JLabel("Number 1 ");
JTextField tf1 = new JTextField();
//creating num2
JLabel lable2 = new JLabel("Number 2 ");
JTextField tf2 = new JTextField();
//creating result
JPanel panel3 = new JPanel();
JLabel lable3 = new JLabel("Result: ");
JTextField tf3 = new JTextField(10);
//creating button
JButton Add= new JButton("Add");
JButton Subtract = new JButton("Subtract");
JButton Multiply = new JButton("Multiply");
JButton Division = new JButton("Division");
//creating num1
panel2.add(lable1);
panel2.add(tf1);
//creating num2
panel2.add(lable2);
panel2.add(tf2);
//creating result
panel3.add(lable3);
panel3.add(tf3);
//creating buttons
panel.add(Add);
panel.add(Subtract);
panel.add(Multiply);
panel.add(Division);
// frame.getContentPane().add(BorderLayout.WEST, panel3);
//creating Box Layout for num1 and num2
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
frame.add(panel2);
//creating Box Layout for buttons
BoxLayout layout1 = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout1);
frame.setLayout(new FlowLayout());
frame.add(panel);
// Add.setAlignmentX(Component.RIGHT_ALIGNMENT);
// panel.add(Add);
// frame.getContentPane().add(BorderLayout.SOUTH, panel);
//creating Border Layout for num1 and num2
frame.getContentPane().add(BorderLayout.WEST, panel2);
//creating Border Layout for Buttons
frame.getContentPane().add(BorderLayout.EAST, panel);
//creating Box Layout for Result
frame.getContentPane().add(BorderLayout.SOUTH, panel3);
frame.setVisible(true);
}
}
this is my code, and I have no idea to make the text field next to the numbers. i try several times but does not comes together. whenever i add something the panels moves
its work only with the result but for the number doesn't work
here i used Box Layout for buttons and number 1 and number 2:
You were very close to solving it.
Replace this line
JPanel panel2 = new JPanel();
With this line
JPanel panel2 = new JPanel(new GridLayout(2, 2));
Then, get rid of these 3 lines
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
Now your code looks like this
I've created a registration page in Java using different Layouts and I'm having trouble aligning the buttons on the same line and creating space between each JLabel.You can see it on the link (https://postimg.org/image/jaur8rxz7/).
This is the code that I have written
package liblog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Aregistration extends JFrame {
private JTextField text;
private JTextField text2;
private JPasswordField pass;
private JButton log1;
private JButton sign1;
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel();
JLabel label3 = new JLabel();
JLabel label2 = new JLabel();
Aregistration(){
super("Admin Registration");
setLayout(new FlowLayout(FlowLayout.CENTER,15,15));
setBounds(500,500,500,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(panel);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
label1=new JLabel("First Name");
label1.setToolTipText("Enter Your First Name");
text = new JTextField("Name",20);
label2=new JLabel("Registration No");
label2.setToolTipText("Enter Your Registation no");
text2 = new JTextField("Registation No",20);
label3 = new JLabel("Password");
label3.setToolTipText("Enter Your Password");
pass = new JPasswordField("Password",20);
log1 = new JButton("Log In");
sign1 = new JButton("Register");
panel.add(Box.createRigidArea(new Dimension(0, 0)));
panel.add(Box.createVerticalStrut(HEIGHT));
panel.add(label1);
panel.add(text);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(pass);
panel.add(log1);
panel.add(sign1);
setVisible(true);
}
public static void main(String[]args){new Aregistration();
}
}
I'm having trouble aligning the buttons on the same line
Create a second panel and add the buttons to the panel and then add this panel to your main panel:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
buttonPanel.add( log1 );
buttonPanel.add( Box.createHorizontalStrut(5) );
buttonPanel.add( sign1 );
panel.add( buttonPanel );
and creating space between each JLabel
You can also use a vertical strut.
Read the section from the Swing tutorial on How to Use BoxLayout for more information and examples.
I am new to java. I am trying to implement scroll feature to my Jpanel but the schoolpane is not visible. I tried to many methods but nothing works.
public class test extends ContentPanel {
JPanel secondary;
JPanel customerType;
JPanel primary;
JPanel labelPanel;
JLabel lCustNo;
JLabel lCustName;
JLabel lTelNo;
JLabel lAddress;
JLabel lNationality;
JLabel lResident;
JLabel lVisitor;
JLabel lCustomerType;
JLabel lIdCard;
JLabel lBankName;
JLabel lPassportNo;
JLabel lVisitStart;
JLabel lVisitEnd;
JTextField tCustNo;
JTextField tCustName;
JTextField tTelNo;
JTextField tAddress;
JTextField tNationality;
JTextField tIdCard;
JTextField tBankName;
JTextField tPassportNo;
JTextField tVisitStart;
JTextField tVisitEnd;
JPanel radioButton;
JRadioButton rResident;
JRadioButton rVisitor;
ButtonGroup custType;
JScrollPane scrollBar;
JSeparator s1;
public test (String title, JPanel parent) {
super(title, parent);
secondary = new JPanel(new GridLayout(2,0));
secondary.setBounds(10, 10, 500, 800);
labelPanel = new JPanel(new GridLayout(5,2,300,20));
radioButton = new JPanel(new GridLayout(1, 4));
customerType = new JPanel(new GridLayout(3,4, 30, 20));
primary = new JPanel(new BorderLayout());
lCustNo = new JLabel("Customer No: ");
lCustName = new JLabel("Name: ");
lTelNo = new JLabel("Telephone Number: ");
lAddress = new JLabel("Address: ");
lNationality = new JLabel("Nationality: ");
lResident = new JLabel("Resident");
lVisitor = new JLabel("Visitor");
lCustomerType = new JLabel("Customer Type");
lIdCard = new JLabel("Id Card");
lBankName = new JLabel("Bank Name");
lPassportNo = new JLabel("Passport Number");
lVisitStart = new JLabel("Visit Start");
lVisitEnd = new JLabel("Visit End");
tCustNo = new JTextField(10);
tCustName = new JTextField(10);
tTelNo = new JTextField(10);
tAddress = new JTextField(10);
tNationality = new JTextField(10);
tIdCard = new JTextField(10);
tBankName = new JTextField(10);
tPassportNo = new JTextField(10);
tVisitStart = new JTextField(10);
tVisitEnd = new JTextField(10);
rResident = new JRadioButton();
rVisitor = new JRadioButton();
custType = new ButtonGroup();
customerType.add(lPassportNo);
customerType.add(tPassportNo);
customerType.add(lVisitStart);
customerType.add(tVisitStart);
customerType.add(lIdCard);
customerType.add(tIdCard);
customerType.add(lBankName);
customerType.add(tBankName);
customerType.add(lVisitEnd);
customerType.add(tVisitEnd);
custType.add(rResident);
custType.add(rVisitor);
radioButton.add(lVisitor);
radioButton.add(rVisitor);
radioButton.add(lResident);
radioButton.add(rResident);
labelPanel.add(lCustNo);
labelPanel.add(tCustNo);
labelPanel.add(lCustName);
labelPanel.add(tCustName);
labelPanel.add(lTelNo);
labelPanel.add(tTelNo);
labelPanel.add(lAddress);
labelPanel.add(tAddress);
labelPanel.add(lNationality);
labelPanel.add(tNationality);
secondary.add(customerType);
secondary.add(labelPanel);;
primary.add(secondary,BorderLayout.CENTER);
scrollBar= new JScrollPane(primary);
scrollBar.setBounds(10, 10, 400, 555);
this.add(scrollBar);
}
}
My code is above. This class extends contentPanel and contentPanel extends JPanel. Someone please help me.
Thanks.
Your scrollBar is not visible because it is not necessary. If you change JScrollPane settings, for example:
scrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
you will see you have it, but not use it. It is because your scrollBar doesn't have a content which expands dynamically (like JTextArea for example). Your scrollBar has JPanel inside, which has size to fit all its components, so it will not be enough to add some text inside text components.
To make scroll bars visible, you can change a size of JSrollPane. However setBound() will not work if you JPanel use default FlowLayout. To use absolute positioning in JPanel you need to use null layout, what is generally not recommended and is seen as bad programming practice.
Try to delete setBound() lines and use for example scrollBar.setPreferredSize(new Dimension(400,555)); instead. You should see a difference.
I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}
I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. everytime i add a panel inside it creates an annoying big space after another added panel. I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. Here is my code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500,312));
global.setSize(500,312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
//Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
global.add(new JSeparator());
//Name Field
JPanel c = new JPanel();
c.setSize(100, 1);
c.setPreferredSize(new Dimension(100,1));
c.setLayout(new FlowLayout());
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20,25));
c.add(label);
c.add(text);
global.add(c);
//Hostname Field
JPanel c1 = new JPanel();
c1.setSize(100, 1);
c1.setPreferredSize(new Dimension(100,1));
c1.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20,25));
c1.add(label2);
c1.add(text1);
global.add(c1);
BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components).
Here is the rewritten code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500, 312));
global.setSize(500, 312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
// Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
JSeparator sep = new JSeparator();
sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
global.add(sep);
// Name Field
JPanel c = new JPanel();
c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20, 25));
c.add(label);
c.add(text);
global.add(c);
// Hostname Field
JPanel c1 = new JPanel();
c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20, 25));
c1.add(label2);
c1.add(text1);
global.add(c1);
global.add(Box.createVerticalGlue());