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");
Related
As stated in the title i need to move the label for the text box to be above the box and not to the side. attached i have a picutres of what i mean. what i have vs what i want i have tried searching for it but i cannot seem to find the answer im looking for/not exactly sure what to look up. I have tried using JFrame but it made a separate window unless i need to make the entire GUI a JFrame for me to get the result i want?
Also the actionPerformed method has things but it is irrelevant to the question but displays correctly still.
import java.awt.event.\*;
import javax.swing.\*;
import java.text.DecimalFormat;
public class Project4 extends JFrame implements ActionListener {
private JTextArea taArea = new JTextArea("", 30, 20);
ButtonGroup group = new ButtonGroup();
JTextField name = new JTextField(20);
boolean ch = false;
boolean pep = false;
boolean sup = false;
boolean veg = false;
DecimalFormat df = new DecimalFormat("##.00");
double cost = 0.0;
public Project4() {
initUI();
}
public final void initUI() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
getContentPane().add(panel1, "North");
getContentPane().add(panel2, "West");
getContentPane().add(panel3, "Center");
getContentPane().add(panel4, "East");
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
getContentPane().add(panel5, "South");
JButton button = new JButton("Place Order");
button.addActionListener(this);
panel5.add(button);
JButton button2 = new JButton("Clear");
button2.addActionListener(this);
panel5.add(button2);
panel3.add(taArea);
JCheckBox checkBox1 = new JCheckBox("Cheese Pizza") ;
checkBox1.addActionListener(this);
panel4.add(checkBox1);
JCheckBox checkBox2 = new JCheckBox("Pepperoni Pizza");
checkBox2.addActionListener(this);
panel4.add(checkBox2);
JCheckBox checkBox3 = new JCheckBox("Supreme Pizza");
checkBox3.addActionListener(this);
panel4.add(checkBox3);
JCheckBox checkBox4 = new JCheckBox("Vegetarian Pizza");
checkBox4.addActionListener(this);
panel4.add(checkBox4);
JRadioButton radioButton1 = new JRadioButton("Pick Up");
group.add(radioButton1);
radioButton1.addActionListener(this);
panel1.add(radioButton1);
JRadioButton radioButton2 = new JRadioButton("Delivery");
group.add(radioButton2);
radioButton2.addActionListener(this);
panel1.add(radioButton2);
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
panel5.add(name_label);
panel5.add(name);
setSize(600, 300);
setTitle("Pizza to Order");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent action) {
}
public static void main(String[] args) {
Project4 ex = new Project4();
ex.setVisible(true);
}
}
You can use a nested JPanel with another layout in order to achieve that. I would go with BorderLayout here. You can also other layouts that allow vertical orientation. Visiting the visual guide to Layout Managers will help you spot them.
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
JPanel verticalNestedPanel = new JPanel(new BorderLayout());
verticalNestedPanel.add(name_label, BorderLayout.PAGE_START);
verticalNestedPanel.add(name, BorderLayout.PAGE_END);
panel5.add(verticalNestedPanel);
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?
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 create a GUI using JPanels that have a GridLayout. The fromPanel works perfectly, but the toPanel will only add the JTextFields. The code for the panels is almost exactly the same so I'm not sure why one works but the other doesn't. I have tried changing either rows or columns to 0, but the JLabels still don't show up in the toPanel.
Here is my code:
public class Driver extends JFrame{
private int WIDTH = 800, HEIGHT = 500, WIDTH2 = 350;
private JPanel toPanel, fromPanel, sizePanel, messagePanel, deliveryPanel,
totalPanel, bottomPanel;
private JLabel firstLabel, lastLabel, streetLabel, cityLabel, stateLabel, zipLabel;
private JTextField toFirstText, toLastText, toStreetText, toCityText, toStateText, toZipText,
fromFirstText, fromLastText, fromStreetText, fromCityText, fromStateText, fromZipText;
public Driver(){
setTitle("JoAnn's Floral");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
//labels
lastLabel = new JLabel("Last Name", JLabel.RIGHT);
firstLabel = new JLabel("First Name", JLabel.RIGHT);
streetLabel = new JLabel("Street", JLabel.RIGHT);
cityLabel = new JLabel("City", JLabel.RIGHT);
stateLabel = new JLabel("State", JLabel.RIGHT);
zipLabel = new JLabel("ZIP", JLabel.RIGHT);
buildToPanel();
add(toPanel);
buildFromPanel();
add(fromPanel);
}
public void buildToPanel(){
toPanel = new JPanel(new GridLayout(6, 2, 5, 5));
toPanel.setBorder(BorderFactory.createTitledBorder("To"));
toPanel.setPreferredSize(new Dimension(WIDTH2, HEIGHT/3));
//text fields
toLastText = new JTextField(10);
toFirstText = new JTextField(10);
toStreetText = new JTextField(10);
toCityText = new JTextField(10);
toStateText = new JTextField(10);
toZipText = new JTextField(10);
//add to layout
toPanel.add(firstLabel);
toPanel.add(toFirstText);
toPanel.add(lastLabel);
toPanel.add(toLastText);
toPanel.add(streetLabel);
toPanel.add(toStreetText);
toPanel.add(cityLabel);
toPanel.add(toCityText);
toPanel.add(stateLabel);
toPanel.add(toStateText);
toPanel.add(zipLabel);
toPanel.add(toZipText);
}
public void buildFromPanel(){
fromPanel = new JPanel(new GridLayout(6, 2, 5, 5));
fromPanel.setBorder(BorderFactory.createTitledBorder("From"));
fromPanel.setPreferredSize(new Dimension(WIDTH2, HEIGHT/3));
//text fields
fromFirstText = new JTextField(10);
fromLastText = new JTextField(10);
fromStreetText = new JTextField(10);
fromCityText = new JTextField(10);
fromStateText = new JTextField(10);
fromZipText = new JTextField(10);
//add to layout
fromPanel.add(firstLabel);
fromPanel.add(fromFirstText);
fromPanel.add(lastLabel);
fromPanel.add(fromLastText);
fromPanel.add(streetLabel);
fromPanel.add(fromStreetText);
fromPanel.add(cityLabel);
fromPanel.add(fromCityText);
fromPanel.add(stateLabel);
fromPanel.add(fromStateText);
fromPanel.add(zipLabel);
fromPanel.add(fromZipText);
}
public static void main(String[] args) {
Driver drive = new Driver();
drive.setVisible(true);
}
}
A JComponent can only appear in one container at a time. Since there is only one instance of each label, the code will only show one on-screen.
Tips
See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
Also consider GroupLayout for each detail panel as seen in this answer.
Hey guys, see this snipped of code:
JFrame loginFrame = new JFrame("Login");
loginFrame.setSize(500,400);
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new GridLayout(0,2));
JLabel header = new JLabel("Login");
JLabel header2 = new JLabel("blahvlah");
JLabel loginLabel = new JLabel("Login");
JTextField loginField = new JTextField(24);
JLabel passLabel = new JLabel("Password");
JTextField passField = new JTextField(24);
loginPanel.add(header);
loginPanel.add(header2);
loginPanel.add(loginLabel);
loginPanel.add(loginField);
loginPanel.add(passLabel);
loginPanel.add(passField);
loginFrame.add(loginPanel);
loginFrame.setVisible(true);
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
the JTextFields are present on the app, but i cannot enter anything into them? There is no cursor either, what is the problem? Also my exit on close does not seem to work?
thanks
The code you posted does not compile (on the last line you typed loginframe instead of loginFrame). Having fixed that, the following program:
public class JFrameTest {
public static void main(String[] args) {
JFrame loginFrame = new JFrame("Login");
loginFrame.setSize(500,400);
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new GridLayout(0,2));
JLabel header = new JLabel("Login");
JLabel header2 = new JLabel("blahvlah");
JLabel loginLabel = new JLabel("Login");
JTextField loginField = new JTextField(24);
JLabel passLabel = new JLabel("Password");
JTextField passField = new JTextField(24);
loginPanel.add(header);
loginPanel.add(header2);
loginPanel.add(loginLabel);
loginPanel.add(loginField);
loginPanel.add(passLabel);
loginPanel.add(passField);
loginFrame.add(loginPanel);
loginFrame.setVisible(true);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
runs just fine. I can enter text in both JTextFields and can also close the JFrame: