Components get squished in JPanel and JFrame - java

I'm trying to create a GUI window for the user to enter some information for my program. But it seems like no matter how I change the sizes or the locations, all of my components are squished in far smaller than what I want. Could someone please point out what I am missing?
Here's what I tried:
JFrame inputFrame = new JFrame();
JPanel panel = new JPanel();
inputFrame.setTitle("Create Event");
inputFrame.setSize(500,400);
JTextField eventName = new JTextField("Untitled event");
JTextField eventStart = new JTextField();
JTextField eventEnd = new JTextField();
JButton save = new JButton("Save");
JLabel selectedDate = new JLabel(MyCalendarTester.currentMonth + 1 + "/" + selectedDay + "/" + MyCalendarTester.currentYear);
selectedDay = null;
panel.setSize(450,300);
eventName.setBounds(10, 10, 600, 50);
panel.add(eventName);
selectedDate.setBounds(10, 20, 50, 20);
panel.add(selectedDate);
panel.add(eventStart);
eventStart.setBounds(100, 20, 50, 20);
panel.add(eventEnd);
eventEnd.setBounds(175, 20, 50, 20);
panel.add(save);
save.setBounds(250, 20, 60, 30);
inputFrame.add(panel);
inputFrame.setVisible(true);

The default layout manager for a JPanel is the FlowLayout. The FlowLayout will display components at their preferred size, which is the way the component should be displayed.
You should not attempt to give the component a random size because you don't know what the best size for the component should be based on Font, OS etc.
When you create a JTextField you can use:
JTextField textField = new JTextField(10);
The value 10 will allow the text field to give itself a reasonable preferred size.
The JLabel and JButton size will be determined by the text of the component.

Related

Resizing Components and Font with MigLayout

i'm pretty new to Layout Managers and i have absolutely no idea how to resize the Font-Size automatically with the MigLayout Manager. I have already managed to resize the components with the grow and fill constraint, but I don't seem to get the font-size to change with the size of the components. How do i do this?
Here my few code lines:
public class Projekte {
public Projekte()
{
main();
}
public static void main() {
JFrame projekte = new JFrame();
projekte.setBounds(100, 100,1080,1900);
projekte.setExtendedState(Frame.MAXIMIZED_BOTH);
projekte.setTitle("Testframe");
projekte.getContentPane().setBackground(new Color(255,255,255));
projekte.getContentPane().setLayout(new MigLayout("", "[][][][][][grow,fill][][][][]", "[][][][][][][][][][][grow,fill][][]"));
JLabel lblTest = new JLabel("Test");
projekte.getContentPane().add(lblTest, "cell 4 10,alignx trailing");
JTextField textField = new JTextField();
projekte.getContentPane().add(textField, "cell 5 10");
textField.setColumns(10);
JLabel lblTest_2 = new JLabel("Test_2");
projekte.getContentPane().add(lblTest_2, "cell 6 10,alignx trailing");
JTextField textField_2 = new JTextField();
projekte.getContentPane().add(textField_2, "cell 7 10");
textField_2.setColumns(10);
JLabel lblTest_3 = new JLabel("Test_3");
projekte.getContentPane().add(lblTest_3, "cell 4 11,alignx trailing");
JTextField textField_3 = new JTextField();
projekte.getContentPane().add(textField_3, "cell 5 11");
textField_3.setColumns(10);
}
}
I think it is quite easy, but I don't seem to find the solution, maybe you can help.
Font have attributes, but any of them relate to layout - you can manually scale font with some parameter using deriveFont(float size)
- creates a new Font object by replicating the current Font object and applying a new size to it.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
float scale = screenWidth/1000;
label.getFont().deriveFont(scale);

How can I set the size of JTextField?

I was trying to set size of JButton or JTextField or JPanel but somehow I didn't manage to do that.
I tried
JTextField FirstName = new JTextField("First Name");
FirstName.setSize(new Dimension(40, 20));
and
JTextField FirstName = new JTextField("Fisrt Name");
FirstName.setSize(40, 20);
but none of them worked. How can I achieve the desired effect?

Make TextField normal size using LayoutManagers

In the image above, this is what my GUI looks like, i have a problem fixing up the textfield as it takes up the most percentage of space in my frame, and i want to correct the error I have.
I know layout managers is a pain in the ass, but does anyone know how i can make the text field smaller so that my frame looks nicer.
I am using jpanels to hold most of my components
Here is the necessary part of my code
panel1 = new JPanel();
panel1.setLayout(new GridLayout());
frame.getContentPane().add(panel1, BorderLayout.NORTH);
label2 = new JLabel("Score: " + score);
label2.setFont(new Font("SimSun", Font.BOLD, 22));
label2.setLocation(0,0);
label2.setSize(117,37);
panel1.add(label2);
label3 = new JLabel("Lives: " + lives);
label3.setFont(new Font("SimSun", Font.BOLD, 22));
label3.setLocation(0,70);
label3.setSize(105,49);
panel1.add(label3);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
frame.getContentPane().add(panel2, BorderLayout.CENTER);
label1 = new JLabel(a +" + " +b);
label1.setFont(new Font("SimSun", Font.BOLD, 22));
label1.setHorizontalAlignment(SwingConstants.CENTER);
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
label1.setBorder(BorderFactory.createCompoundBorder(null,paddingBorder));
label1.setLocation(249,105);
label1.setSize(183,60);
panel2.add(label1, BorderLayout.NORTH);
box1 = new JTextField();
box1.setPreferredSize(new Dimension(50, 50));
box1.setLocation(249,176);
box1.setSize(96,25);
panel2.add(box1, BorderLayout.CENTER);
panel3 = new JPanel();
panel3.setLayout(new GridLayout());
frame.getContentPane().add(panel3, BorderLayout.SOUTH);
button1 = new JButton("Answer");
panel3.add(button1);
button1.addActionListener(this);
button1.setLocation(187,236);
button1.setSize(105,50);
button2 = new JButton("Reset");
panel3.add(button2);
button2.addActionListener(this);
button2.setVisible(false);
button2.setLocation(302,236);
button2.setSize(105,50);
The reason for this is you are adding your JTextField to BorderLayout.CENTER, which will stretch it to fill all the space available to this particular layout manager. Although I am not exactly sure how you want your GUI to look, perhaps a simple FlowLayout (the default layout) or a BoxLayout coupled with setAlignmentX(Component.CENTER_ALIGNMENT); on the JTextField will solve your problem.
You can find more information on layouts here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Additionally, note that you shouldn't be using things like setLocation() as the point of the layout manager is to allow it to choose how components should be laid out on the screen, and as such the layout manager may ignore this as well as methods like setSize.

Creating A billing Page for inventory management project. Java

Hi I want to create a billing page as part of my Mini-project as in image project billing. I tried to create it using setBounds. But i didn't get the desired page. Can anyone help me to do this?
package start;
void makeandshowGUI()
{
billno=new JTextField(15);
dd=new JTextField(2);
mm=new JTextField(2);
yy=new JTextField(2);
itemcode=new JTextField(5);
itemname=new JTextField(50);
qty=new JTextField(3);
cname=new JTextField(50);
wlcm=new JLabel("Product Billing # BillDesk");
ptotal=new JLabel("");
billn=new JLabel("Bill No.:");
billdate=new JLabel("Bill Date");
cusname=new JLabel("Customer Name");
pid=new JLabel("Product ID:");
slash1=new JLabel("/");
slash2=new JLabel("/");
pname=new JLabel("Product Name:");
pqty=new JLabel("Qty:");
total=new JLabel("Total:");
billd=new JLabel("Billing Details");
purchased=new JLabel("Purchase Details");
ftotal=new JLabel("Total:");
disc=new JLabel("Discount:");
gtotal=new JLabel("Grand Total:");
save=new JButton("Save Bill & Print");
view=new JButton("View Bill");
list=new JButton("..");
String[] columnNames = {"Sl. No.",
"Product ID",
"Product Name",
"Qty",
"Rate","Price"};
JFrame bill=new JFrame("Product Billing || BillDesk");
bill.setSize(1000,5000);
bill.setBackground(Color.white);
bill.setVisible(true);
bill.setResizable(false);
bill.setMinimumSize(new Dimension(1000, 1000));
DefaultTableModel model = new DefaultTableModel(columnNames,10);
JTable billTable=new JTable(model)
{#Override
public boolean isCellEditable(int arg0, int arg1) {
return false;
}};
JScrollPane pane = new JScrollPane(billTable);
Container c=bill.getContentPane();
bill.setLayout(null);
wlcm.setBounds(500, 0, 200, 20);
billn.setBounds(0, 100, 100, 25);
billno.setBounds(52,100,100,25);
billdate.setBounds(700, 100, 100, 25);
dd.setBounds(751, 100,20, 25);
slash1.setBounds(772,100,5,25);
mm.setBounds(777, 100,20, 25);
slash2.setBounds(810,100,5,25);
yy.setBounds(813, 100,20, 25);
cusname.setBounds(0, 130, 50, 25);
cname.setBounds(55, 130, 50, 25);
pid.setBounds(0, 200, 50, 25);
itemcode.setBounds(55, 200, 30, 25);
pname.setBounds(100, 200, 50,25);
itemname.setBounds(125, 200, 50,25);
list.setBounds(206, 200, 5, 25);
pqty.setBounds(215, 200, 25, 25);
qty.setBounds(145, 200, 25, 25);
total.setBounds(175, 200, 25, 25);
ptotal.setBounds(205, 200, 50, 50);
c.add(wlcm,FlowLayout.LEADING);
c.add(billn);
c.add(billdate);
c.add(cusname);
c.add(pid);
c.add(pname);
c.add(pqty);
c.add(slash1);
c.add(slash2);
c.add(ptotal);
c.add(billno);
c.add(dd);
c.add(mm);
c.add(yy);
c.add(cname);
c.add(cusname);
c.add(itemcode);
c.add(itemname);
c.add(list);
c.add(qty);
c.add(total);
c.add(ptotal);
}
`
Why are you manually trying to arrange everything ? You should consider using a LayoutManager which will handle the task for you.
JComponents with their ability to nest are really powerful and allow you to create really neat UIs without having to worry about manually positioning them. Each one of the nested JComponent, say a JPanel can have a different LayoutManager associated with them.
Also, the 4 parameters in setBounds() are:
x - the new x-coordinate of this component
y - the new y-coordinate of this component
width - the new width of this component
height - the new height of this component
The problem is the width and height. Manually assigning them will give an inconsistent look-and-feel to your application across screens of different resolutions. A LayoutManager will handle all that for you.

how do you increase panel width?

With this code I will have the following window. I created 2 panels and added the mainp one to the frame and the panel to the mainp I did this in order to make window resizing dynamic (so the panel wont resize to the frame) I tried making my default panel size wider so that the text fields and label become wider but panel.setsize doesn't seem to do anything.
// creates the labels
studId = new JLabel("Student ID");
studAvg = new JLabel("Student Average");
studName = new JLabel("Student Name");
// creates the text fields
JTextField studIdText = new JTextField();
JTextField studAvgText = new JTextField();
JTextField studNameText = new JTextField();
JPanel mainp = new JPanel();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 2, 2));
panel.setSize(300, 100);
// adds to the GridLayout
panel.add(studId);
panel.add(studIdText);
panel.add(studName);
panel.add(studNameText);
panel.add(studAvg);
panel.add(studAvgText);
mainp.add(panel);
add(BorderLayout.CENTER,mainp);
// verifies the textfields
studIdText.setInputVerifier(new IntVerifier());
studAvgText.setInputVerifier(new DoubleVerifier());
setTitle("Student Form");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
The method you are looking for is setPreferredSize. Use it instead of panel.setSize(300, 100).
panel.setPreferredSize(new Dimension(300, 100));
I would also recommend to not setting the size of your JFrame to the fixed value (300,200) but do pack() instead. This will set the size of your JFrame to fit the panels inside.
using Advise from #Dan and #MADprogrammer and #trashgod i came up with the following
JTextField studIdText = new JTextField(20);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints r1 = new GridBagConstraints();
r1.fill = GridBagConstraints.HORIZONTAL;
r1.weightx = 0.0;
r1.gridx = 0;
r1.gridy = 0;
panel.add(studId,r1);
r1.weightx = 0.5;
r1.gridx = 1;
r1.gridwidth = GridBagConstraints.REMAINDER;
panel.add(studIdText,r1);
of course you can make GridBagConstraints for every row and just change the gridy
Set the layout for mainp as BorderLayout.
mainp.setLayout(new BorderLayout());
Then, in order to avoid having the textfields resize vertically and look strange, you can add panel to BorderLayout.NORTH, for instance.
mainp.add(panel, BorderLayout.NORTH);

Categories