Creating A billing Page for inventory management project. Java - 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.

Related

JavaFX - label below affecting the labels above

I'm trying to add a label beneath 3 labels I currently have on a different row, however it looks like the width of the fourth label that's below the top 3 is actually pushing away 2 labels on the row above, even though I have set this label to be on a different row.
mainGrid.add(scanLabel, 45, 25);
mainGrid.add(imageLabel, 75, 25);
mainGrid.add(patientLabel, 105, 25);
mainGrid.add(statusLabel, 45, 30);
Here's an image of the top 3 labels with the statusLabel commented out - https://puu.sh/FSdZd/a2b4585b69.png - and here is where I add my label back in and they get pushed out - https://puu.sh/FSesI/b0c524fedd.png
Rest of my code (left some out just so it's not so long):
public class ScanInterfaceStage {
Stage stage;
Scene scene;
BorderPane mainContent;
BorderPane buttonZone;
BorderPane topZone;
BorderPane leftZone;
BorderPane rightZone;
BorderPane bottomZone;
GridPane mainGrid;
GridPane voiceGrid;
Button backButton;
Button settingsButton;
Button exitButton;
Button voiceButton;
Button voiceConfirmButton;
Label statusLabel;
Label imageLabel;
Label patientLabel;
Label scanLabel;
Image exitImage;
Image backImage;
Image voiceImage;
Image settingsImage;
ImageView exitImageView;
ImageView backImageView;
ImageView voiceImageView;
ImageView settingsImageView;
Alert voicePopUp;
double offsetX;
double offsetY;
public ScanInterfaceStage (double sizeX, double sizeY, double positionX, double positionY) {
stage = new Stage();
// Instantiate panes
mainContent = new BorderPane();
buttonZone = new BorderPane();
leftZone = new BorderPane();
topZone = new BorderPane();
bottomZone = new BorderPane();
rightZone = new BorderPane();
voiceGrid = new GridPane();
mainGrid = new GridPane();
// Instantiate Labels
statusLabel = new Label("");
imageLabel = new Label("");
patientLabel = new Label("");
scanLabel = new Label("");
// Instantiate buttons
backButton = new Button("");
settingsButton = new Button("");
exitButton = new Button("");
voiceButton = new Button("");
voiceConfirmButton = new Button("Done\n\nClick to view");
// Set button sizes
exitButton.setMinWidth(103);
exitButton.setMaxWidth(103);
exitButton.setPrefWidth(103);
exitButton.setMinHeight(52);
exitButton.setMaxHeight(52);
exitButton.setPrefHeight(52);
voiceButton.setMinWidth(103);
voiceButton.setMaxWidth(103);
voiceButton.setPrefWidth(103);
voiceButton.setMinHeight(52);
voiceButton.setMaxHeight(52);
voiceButton.setPrefHeight(52);
backButton.setMinWidth(103);
backButton.setMaxWidth(103);
backButton.setPrefWidth(103);
backButton.setMinHeight(52);
backButton.setMaxHeight(52);
backButton.setPrefHeight(52);
settingsButton.setMinWidth(103);
settingsButton.setMaxWidth(103);
settingsButton.setPrefWidth(103);
settingsButton.setMinHeight(52);
settingsButton.setMaxHeight(52);
settingsButton.setPrefHeight(52);
// Set Label sizes
scanLabel.setMinWidth(250);
scanLabel.setMaxWidth(250);
scanLabel.setPrefWidth(250);
scanLabel.setMinHeight(400);
scanLabel.setMaxHeight(400);
scanLabel.setPrefHeight(400);
imageLabel.setMinWidth(500);
imageLabel.setMaxWidth(500);
imageLabel.setPrefWidth(500);
imageLabel.setMinHeight(350);
imageLabel.setMaxHeight(350);
imageLabel.setPrefHeight(350);
patientLabel.setMinWidth(250);
patientLabel.setMaxWidth(250);
patientLabel.setPrefWidth(250);
patientLabel.setMinHeight(400);
patientLabel.setMaxHeight(400);
patientLabel.setPrefHeight(400);
statusLabel.setMinWidth(800);
statusLabel.setMaxWidth(800);
statusLabel.setPrefWidth(800);
statusLabel.setMinHeight(150);
statusLabel.setMaxHeight(150);
statusLabel.setPrefHeight(150);
// Generate and apply drop shadow
DropShadow ds = new DropShadow();
ds.setOffsetY(3.0);
ds.setOffsetX(3.0);
ds.setColor(Color.GRAY);
exitButton.setEffect(ds);
settingsButton.setEffect(ds);
backButton.setEffect(ds);
voiceButton.setEffect(ds);
topZone.setEffect(ds);
scanLabel.setEffect(ds);
imageLabel.setEffect(ds);
patientLabel.setEffect(ds);
statusLabel.setEffect(ds);
// Define stage parameters
stage.setX(positionX);
stage.setY(positionY);
// Set the scene to display mainContent at passed size
scene = new Scene(mainContent, sizeX, sizeY);
// Setting up voice alert
voicePopUp = new Alert(AlertType.INFORMATION);
voicePopUp.initStyle(StageStyle.UNDECORATED);
voicePopUp.setGraphic(voiceImageView);
voicePopUp.setHeaderText("We are now listening!");
voicePopUp.setHeight(550);
voicePopUp.setWidth(550);
voicePopUp.setX(500);
voicePopUp.setY(500);
voicePopUp.getDialogPane().setStyle("-fx-background-color: c9f0ff; -fx-font: 25 Open_Sans;");
voicePopUp.setContentText("AMMS is now listening to your voice! Please clearly state what you would like us to help with!");
mainContent.setCenter(mainGrid);
mainContent.setTop(topZone);
mainContent.setLeft(leftZone);
mainContent.setRight(rightZone);
mainContent.setBottom(bottomZone);
mainGrid.setPadding(new Insets(10, 10, 10, 10));
mainGrid.setHgap(3);
mainGrid.setVgap(3);
Text menuText = new Text("AAMS - Scan Type: X-Ray");
menuText.setStyle("-fx-font: 25 Open_Sans-Semi-Bold");
topZone.setCenter(menuText);
mainGrid.add(scanLabel, 45, 25);
mainGrid.add(imageLabel, 75, 25);
mainGrid.add(patientLabel, 105, 25);
mainGrid.add(statusLabel, 45, 80);
A grid pane works by having indexed rows and columns which take up variable height and width, respectively. When you specify the column and row indexes in mainGrid.add(node, columnIndex, rowIndex), these are not pixel values, but indexes of the columns and rows respectively (which are zero-based).
So
mainGrid.add(scanLabel, 45, 25);
mainGrid.add(imageLabel, 75, 25);
mainGrid.add(patientLabel, 105, 25);
mainGrid.add(statusLabel, 45, 80);
results in a grid with 106 columns and 81 rows. Almost all of these are empty, so take up zero space. The exceptions are row 25, which contains three labels, and row 80, which contains 1 label, and columns 75 and 105, which contain one label each, and column 45, which contains two labels.
Since column 45 contains two labels, it will be sized so that it is wide enough to contain the widest of those labels. Columns 46-74, which are all empty and have zero width, all lie to the the right of that, the column 75 (wide enough to contain the imageLabel), followed by columns 76-104 (zero width) and finally column 105 (wide enough to contain the patient label).
The layout this creates is completely identical to
mainGrid.add(scanLabel, 0, 0);
mainGrid.add(imageLabel, 1, 0);
mainGrid.add(patientLabel, 2, 0);
mainGrid.add(statusLabel, 0, 1);
I think you are expecting the status label to span the full width of the grid; you can achieve this by setting its columnSpan. You probably need something like:
mainGrid.add(scanLabel, 0, 0);
mainGrid.add(imageLabel, 1, 0);
mainGrid.add(patientLabel, 2, 0);
mainGrid.add(statusLabel, 0, 1, 3, 1);
The last call uses the overloaded add() method, which specifies a column span and row span, in additional to the column and row indices.

In JFreeChart, how to position the Domain Axis Label next to the Legend?

Essentially, I have a chart that looks like this:
but I want the Domain axis label to show on the left, on the same line as the legend, on the right:
so I can better utilize the small space I got.
My chart source looks like this:
Color lightGray = new Color(200, 200, 200);
Color gray = new Color(150, 150, 150);
JFreeChart chart = createChart(createDataset(), yAxisTitle, xAxisTitle);
chart.setBackgroundPaint(Color.BLACK);
chart.getPlot().setBackgroundPaint(Color.BLACK);
chart.getPlot().setInsets(new RectangleInsets(0,2,2,2));
chart.getXYPlot().setBackgroundPaint(Color.BLACK);
chart.getXYPlot().getDomainAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getDomainAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getDomainAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setLabelPaint(lightGray);
chart.getXYPlot().getRangeAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getRangeAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getRangeAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setLabelPaint(lightGray);
chart.getTitle().setFont(new Font("Arial", Font.PLAIN, 12));
chart.getTitle().setBackgroundPaint(Color.BLACK);
chart.getTitle().setPaint(lightGray);
chart.getLegend().setItemFont(new Font("Arial", Font.PLAIN, 10));
chart.getLegend().setBackgroundPaint(Color.BLACK);
chart.getLegend().setItemPaint(gray);
chart.setTextAntiAlias(true);
chart.setAntiAlias(true);
chart.getLegend().setHorizontalAlignment(HorizontalAlignment.RIGHT);
panel.add(new ChartPanel(chart, false));
How can I accomplish this? Is there a way?
Thank you!
Found it.
To move the axis to the desired position, I used:
chart.getXYPlot().getDomainAxis().setLabelLocation(AxisLabelLocation.LOW_END);
Then to move the legend up in the same line, I had to adjust it's padding to use a negative value, which essentially moves it up, like so:
RectangleInsets padding = chart.getLegend().getPadding();
chart.getLegend().setPadding(new RectangleInsets(padding.getTop() - 20, padding.getRight(), padding.getBottom(), padding.getLeft()));
chart.getLegend().setBounds(chart.getLegend().getBounds());
This results in what I was looking for:
Hope this helps someone else out there!

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);

Components get squished in JPanel and JFrame

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.

How to make Arrays of my JButton/ActionListeners

I'm doing a project for school and we need to create a program with many JButtons, ActionListeners, images, etc. I have created a class with many individual ActionListeners and JButtons, but how can I change this into an array of buttons rather than creating them all individually?
This is my code...
public class Selectie extends JFrame{
private JButton keeper, verdediger, verdediger1, verdediger2;
public void initGUI() {
keeper = new JButton("1. "+""+" Kenneth Vermeer");
Cursor cur = keeper.getCursor();
keeper.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
keeper.setBounds(20, 50, 186, 12);
keeper.setFocusable(false);
keeper.setBorderPainted(false);
keeper.setContentAreaFilled(false);
keeper.setFont(new Font("Arial",Font.PLAIN,17));
keeper.setForeground(Color.WHITE);
keeper.setActionCommand(Vermeer);
verdediger = new JButton("2. "+""+" Gregory van der Wiel");
Cursor cur1 = verdediger.getCursor();
verdediger.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
verdediger.setBounds(20, 70, 215, 17);
verdediger.setFocusable(false);
verdediger.setBorderPainted(false);
verdediger.setContentAreaFilled(false);
verdediger.setFont(new Font("Arial",Font.PLAIN,17));
verdediger.setForeground(Color.WHITE);
verdediger.setActionCommand(Gregory);
verdediger1 = new JButton("3. "+""+" Toby Alderweireld");
Cursor cur2 = verdediger1.getCursor();
verdediger1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
verdediger1.setBounds(20, 95, 188, 17);
verdediger1.setFocusable(false);
verdediger1.setBorderPainted(false);
verdediger1.setContentAreaFilled(false);
verdediger1.setFont(new Font("Arial",Font.PLAIN,17));
verdediger1.setForeground(Color.WHITE);
verdediger1.setActionCommand(Alderweireld);
verdediger2 = new JButton("4. "+""+" Jan Vertonghen");
Cursor cur3 = verdediger2.getCursor();
verdediger2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
verdediger2.setBounds(20, 120, 174, 17);
verdediger2.setFocusable(false);
verdediger2.setBorderPainted(false);
verdediger2.setContentAreaFilled(false);
verdediger2.setFont(new Font("Arial",Font.PLAIN,17));
verdediger2.setForeground(Color.WHITE);
verdediger2.setActionCommand(Vertonghen);
SelectieController s1 = new SelectieController(keeper,verdediger,verdediger1,verdediger2);
keeper.addActionListener(s1);
verdediger.addActionListener(s1);
verdediger1.addActionListener(s1);
verdediger2.addActionListener(s1);
}
}
Without doing all the work, you need to do something like this (you can fill in the blanks - it is your homework after all...)
// create the array
JButton[] buttons = new JButton[10];
// Repeat for each button...
for (int i=0;i<buttons.length;i++){
// create the button
JButton button = new JButton("label");
// set the button properties...
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
button.setBounds(20, 95, 188, 17);
button.setFocusable(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
// etc.
// Assign the listener
button.addActionListener(s1);
// Add the button to the array
buttons[i] = button;
}

Categories