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.
Related
I am trying to create a register form for my application(school project), I wanted to set the layout to BoxLayout but the Jtextfields and combo box is having issue as you can see below, does this issue relates to setSize() or is it something I am doing incorrect,I just want the Jtextfields sorts vertically , I appreciate the support
private JPanel SetUpRegister() {
JLabel registerLabel = new JLabel("Registera");
registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
String[] genderlist = new String[] { "Male", "Female", "Other" };
JComboBox<String> registerList = new JComboBox<>(genderlist);
JPanel registerPanel = new JPanel();
registerPanel.setBackground(new Color(255, 140, 0));
registerPanel.add(registerLabel);
registerPanel.add(loginRegisterInput);
registerPanel.add(passwordRegisterInput);
registerPanel.add(fnRegisterInput);
registerPanel.add(lnRegisterInput);
registerPanel.add(ageRegisterInput);
registerPanel.add(registerList);
registerPanel.setLayout(new BoxLayout(registerPanel,BoxLayout.Y_AXIS));
return registerPanel;
}
The input fields are huge
The BoxLayout will attempt to resize components when extra space is available on the panel. It will resize a component up to its maximum size.
For some reason the maximum height of a JTextField is Integer.MAX_VALUE which makes no sense to me, since the height of the text never changes as you enter more text.
In any case you have a couple of choices:
Use a different layout manager, like the GridBagLayout. The GridBagLayout, will respect the preferred size of the text fields.
Create a custom JTestField and override the getMaximumSize() method to return the preferred height of the component
Use a wrapper panel.
For the wrapper panel you could do:
JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(registerPanel, BorderLayout.PAGE_START);
return wrapper;
//return registerPanel;
The BorderLayout will respect the preferred height of any component added to the PAGE_START, so there is no need for the BoxLayout to resize any component.
private JPanel SetUpRegister() {
JLabel registerLabel = new JLabel("Registera");
JLabel registerLabel1 = new JLabel("Login :");
JLabel registerLabel2 = new JLabel("Password :");
JLabel registerLabel3 = new JLabel("First Name :");
JLabel registerLabel4 = new JLabel("Last Name :");
JLabel registerLabel5 = new JLabel("Age :");
JLabel registerLabel6 = new JLabel("Gender :");
JLabel registerLabel7 = new JLabel("Bio :");
registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
JButton createAccButton = new JButton("Create");
createAccButton.addActionListener(new CreateAccountListener());
loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
bioRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
String[] genderlist = new String[] { "Male", "Female", "Other" };
registerList = new JComboBox(genderlist);
JPanel registerPanel = new JPanel();
registerPanel.setLayout(new BoxLayout(registerPanel, BoxLayout.Y_AXIS));
registerPanel.add(registerLabel);
JPanel registerLabPanel = new JPanel();
registerLabPanel.setLayout(new FlowLayout());
registerLabPanel.add(registerLabel);
JPanel usernamePanel = new JPanel();
usernamePanel.setLayout(new FlowLayout());
usernamePanel.add(registerLabel1);
usernamePanel.add(loginRegisterInput);
JPanel passwordPanel = new JPanel();
passwordPanel.setLayout(new FlowLayout());
passwordPanel.add(registerLabel2);
passwordPanel.add(passwordRegisterInput);
JPanel fnPanel = new JPanel();
fnPanel.setLayout(new FlowLayout());
fnPanel.add(registerLabel3);
fnPanel.add(fnRegisterInput);
JPanel lnPanel = new JPanel();
lnPanel.setLayout(new FlowLayout());
lnPanel.add(registerLabel4);
lnPanel.add(lnRegisterInput);
JPanel agePanel = new JPanel();
agePanel.setLayout(new FlowLayout());
agePanel.add(registerLabel5);
agePanel.add(ageRegisterInput);
JPanel genderPanel = new JPanel();
genderPanel.setLayout(new FlowLayout());
genderPanel.add(registerLabel6);
genderPanel.add(registerList);
JPanel bioPanel = new JPanel();
bioPanel.setLayout(new FlowLayout());
bioPanel.add(registerLabel7);
bioPanel.add(bioRegisterInput);
JPanel buttonLoginPanel = new JPanel();
buttonLoginPanel.setLayout(new FlowLayout());
buttonLoginPanel.add(createAccButton);
registerPanel.add(registerLabel);
registerPanel.add(usernamePanel);
registerPanel.add(passwordPanel);
registerPanel.add(fnPanel);
registerPanel.add(lnPanel);
registerPanel.add(agePanel);
registerPanel.add(genderPanel);
registerPanel.add(bioPanel);
registerPanel.add(buttonLoginPanel);
return registerPanel;
}
I fixed the issue by making a Panel for each input and label
i want save input that i get from user and save them in element .
i want to access elements that user write in my UI.
and if i want save the elements in array list which kind of array list i should build.
in my UI i have text field name and text field middle name and combo box city has got 3 city name and and a radio box that it depend sex.
in final show them in console what should i do ?
this all of my code:
package ui;
import java.awt.*;
import javax.swing.*;
public class UI extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500, 600);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
JRadioButton rb1 = new JRadioButton("man");
JRadioButton rb2 = new JRadioButton("weman");
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel,BorderLayout.CENTER);
frame.add(spanel,BorderLayout.SOUTH);
}
}
You need to use listeners to create code that runs when ui component is pressed. I added listener to every component. try it:
public class UI extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
south.addActionListener(e->{System.out.println("Save button is pressed");});
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
combo.addActionListener(e -> {
System.out.println(((JComboBox<String>) e.getSource()).getSelectedItem());
});
JRadioButton rb1 = new JRadioButton("man");
rb1.addActionListener(e -> {
System.out.println("man: " + ((JRadioButton) e.getSource()).isSelected());
});
JRadioButton rb2 = new JRadioButton("weman");
rb2.addActionListener(e -> {
System.out.println("weman: " + ((JRadioButton) e.getSource()).isSelected());
});
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
name.addActionListener(e -> {
System.out.println("name: " + ((JTextField) e.getSource()).getText());
});
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
family.addActionListener(e -> {
System.out.println("family: " + ((JTextField) e.getSource()).getText());
});
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
comment.addActionListener(e -> {
System.out.println("comment: " + ((JTextField) e.getSource()).getText());
});
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel, BorderLayout.CENTER);
frame.add(spanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
You will have to put JRadioButton into RadioGroup to select one of them.
At first, you need to check your GUI, because from skimming it works not good. Later you can get data from the different component using an appropriate listener for each component or you can use any General button to reading data from all components and print it in a console or anywhere else.
To get data from combo, for example, will be here:
String data = combo.getSelectedIndex();
More information you can get here:
How can I get the user input in Java?
look here
I have to make a format like this, like make a 1,2 panel, then put a 3,1 panel on the left side. Then, I put 4 text fields in the middle left panel and one button in the bottom left panel.
I'm not sure if I can just use GridLayout or if I have to use BorderLayout too.
How can I get this organized because when I set it up, I can't get the layout right. The textfields are not in the right position, they end up on the right side.
public class CreatePanel extends JPanel
{
private Vector projectList;
private JButton button1;
private ProjectSpendingPanel spendingPanel;
private JFrame frame1;
JPanel panel1;
JPanel leftPanel;
JPanel subPanel;
GridLayout layout1;
BorderLayout layout2;
GridLayout layout3;
JLabel message;
JLabel labelName;
JLabel labelNumber;
JLabel labelLocation;
JLabel labelFunding;
JTextField textField1;
JTextField textField2;
JTextField textField3;
JTextField textField4;
//Constructor initializes components and organize them using certain layouts
public CreatePanel(Vector projectList, ProjectSpendingPanel spendingPanel)
{
this.projectList = projectList;
this.spendingPanel = spendingPanel;
//organize components here
layout1 = new GridLayout(1,2);
layout2 = new BorderLayout(1,3);
layout3 = new GridLayout(4,2);
this.setLayout(layout1);
panel1 = new JPanel(layout1);
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
add(panel1);
panel1.add(subPanel);
panel1.add(leftPanel);
labelName = new JLabel("Project Name");
leftPanel.add(labelName);
textField1 = new JTextField("", 15);
leftPanel.add(textField1,BorderLayout.SOUTH);
labelName = new JLabel("Project Number");
leftPanel.add(labelName);
textField2 = new JTextField("",15);
leftPanel.add(textField2);
labelLocation = new JLabel("Project Location");
leftPanel.add(labelLocation);
textField3 = new JTextField("",15);
leftPanel.add(textField3);
labelFunding = new JLabel("Initial Funding");
leftPanel.add(labelFunding);
textField4 = new JTextField("",15);
leftPanel.add(textField4);
add(leftPanel);
button1 = new JButton("Create a project");
subPanel.add(button1,BorderLayout.SOUTH);
}
To get the result you probably intended, you need to tweak your code just a little.
Your lines
panel1 = new JPanel(layout1);
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
should be altered to read
panel1 = new JPanel(new GridLayout(2, 1));
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
GridLayout takes rows first, then columns. Your layout1 defines one row and two columns but you use it for the text fields and the button, so I assume the two panels need to be above each other.
Second, you need to change the order from
add(panel1);
panel1.add(subPanel);
panel1.add(leftPanel);
to
add(panel1);
panel1.add(leftPanel);
panel1.add(subPanel);
Finally, I think you need to change
add(leftPanel);
button1 = new JButton("Create a project");
to
add(spendingPanel);
button1 = new JButton("Create a project");
Adding leftPanelagain seems to destroy the layout. If you do not want to see your spendingPanel here, you could use new JPanel() instead.
Here is a screenshot of the running program
I am using FlowLayout and trying to make JLabels and JTextField in order but I am stuck and do not know how to do it.
public class SoftwareProducts extends JPanel implements ActionListener {
JTextField ramtf;
JTextField processortf;
JTextField productIDtf;
JTextField productNametf;
JTextField productYeartf;
JTextField productPublishHousetf;
JButton CompleteOrder;
JLabel Ramlb = new JLabel("RAM:");
JLabel processorlb = new JLabel("Processor:");
JLabel productIDlb = new JLabel("Product ID");
JLabel productNamelb = new JLabel("Product Name:");
JLabel productYearlb = new JLabel("Product Year:");
JLabel PublishHouselb = new JLabel("Publish House:");
JPanel softwarePanel;
CardLayout c2 = new CardLayout();
CompleteOrder completeOrder;
public static void main(String[] args) {
new SoftwareProducts();
}
public SoftwareProducts() {
setSize(500, 300);
setLayout(new FlowLayout());
add(Ramlb);
Ramlb.setFont(new Font("Arial", 5, 28));
ramtf = new JTextField(15);
add(ramtf);
ramtf.addActionListener(this);
add(processorlb);
processortf = new JTextField(15);
processortf.addActionListener(this);
add(processortf);
add(productIDlb);
productIDtf = new JTextField(10);
productIDtf.addActionListener(this);
add(productIDtf);
add(productNamelb);
add(productNamelb);
productNametf = new JTextField(10);
productNametf.addActionListener(this);
add(productNametf);
add(productYearlb);
productYeartf = new JTextField(10);
productYeartf.addActionListener(this);
add(productYeartf);
add(PublishHouselb);
productPublishHousetf = new JTextField(10);
productPublishHousetf.addActionListener(this);
add(productPublishHousetf);
CompleteOrder = new JButton("CompleteOrder");
CompleteOrder.setSize(25, 40);
CompleteOrder.addActionListener(this);
add(CompleteOrder);
softwarePanel = new JPanel(new FlowLayout());
softwarePanel.add(Ramlb);
softwarePanel.setLayout(c2);
completeOrder = new CompleteOrder();
softwarePanel.add(new JPanel(), "empty");
softwarePanel.add(completeOrder, "completeOrder");
this.add(softwarePanel);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == CompleteOrder) {
c2.show(softwarePanel, "completeOrder");
float ram = Float.parseFloat(ramtf.getText());
float processor = Float.parseFloat(processortf.getText());
int productID = Integer.parseInt(productIDtf.getText());
String productName = productNametf.getText();
int productYear = Integer.parseInt(productYeartf.getText());
String productPublishHouse = productPublishHousetf.getText();
main.softwareList.add(new Software(productID, productName, productYear, productPublishHouse));
ramtf.setText("");
System.out.println("Saved");
}
}
}
What code would result in it new lines breaks in each line? Shat should to be done to make the line breaks down in JLabel and LTextField?
These are two completely different problems and neither of them has anything to do with the LayoutManager.
For the JTextField:
Taken from the docs:
JTextField is a lightweight component that allows the editing of a single line of text
There's no way at all to add a newline to a JTextField (atleast no legit way). Use a JTextArea instead; newlines are just like in the console \n.
JLable like most other JComponents aswell can process and display HTML-code. So a newline in a JLabel would look like this:
JLabel twoLined = new JLabel("This is one line</br>And this is another one");
I am a newbee so advise and help is always greatly appreciated.
Cannot seem to get my container contentPane to display the title.
My code:
class CreateStockCodeDetails extends JFrame implements ActionListener
{
OptraderSA parent;
OptraderGlobalParameters GV = new OptraderGlobalParameters();
private boolean DEBUG = true; //Set DEBUG = true for Debugging
JButton SAVE_BUTTON = new JButton("SAVE");
JButton CANCEL_BUTTON = new JButton("CANCEL");
Font MyFont = new Font("Helvetica",Font.BOLD,24);
JLabel PriceBidLabel = new JLabel(" Bid Price",JLabel.LEFT);
JLabel PriceAskLabel = new JLabel(" Ask Price",JLabel.LEFT);
JLabel PriceMidLabel = new JLabel(" Mid Price",JLabel.LEFT);
JLabel DividendLabel = new JLabel(" Dividend",JLabel.LEFT);
JTextField PriceBid = new JTextField(5);
JTextField PriceAsk = new JTextField(5);
JTextField PriceMid = new JTextField(5);
JTextField Dividend = new JTextField(5);
JTextField NewUnderlyingCode = new JTextField(10);
String NewCode;
public void CreateStockDetails(String StockCode)
{
super("Hallo All");
Container contentPane = getContentPane();
setSize(400,500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Centre Screen To Right Of Main
Dimension sd=Toolkit.getDefaultToolkit().getScreenSize();
super.setLocation(sd.width/2-100/2, sd.height/2-300/2);
Thanks
Kind Regards
Stephen
If it is a JFrame, Window, JInternalFrame, ect, just call setTitle("Hallo"); Also, the call to super must be the first call in your constructor.
You need to set the title to the JFrame and not the ContentPane
A container doesn't have a setTitle() method. Please read the API.
In addition to setting the title of the JFrame, you can add a "titled" border around the content pane:
JPanel content = (JPanel)getContentPane();
content.setBorder( ... );
Read the section from the Swing tutorial on How to Use Borders for an example of using a TitledBorder.