How To Design Dashboard Form In Java Using NetBeans
In this Java Tutorial we will see how How To Design A Dashboard Form In Java NetBeans .
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
/**
*
* @author 1BestCsharp
*/
public class Dashboard_Form_Design {
private JFrame frame;
private JPanel titleBar;
private JLabel titleLabel;
private JLabel closeLabel;
private JLabel minimizeLabel;
private JPanel dashboardPanel;
// variables for form dragging
private boolean isDragging = false;
private Point mouseOffset;
public Dashboard_Form_Design()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,500);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.setBackground(new Color(255,159,26));
/*Container contentPane = frame.getContentPane();
contentPane.setBackground(new Color(255,159,26));
*/
// Set the custom rounded border to the form
frame.getRootPane().setBorder(BorderFactory.createCompoundBorder(
new RoundedBorder(5, new Color(45,45,45)),
new EmptyBorder(0,0,0,0)
));
// form title bar (top of the form)
titleBar = new JPanel();
titleBar.setLayout(null);
titleBar.setBackground(new Color(75,75,75));
titleBar.setPreferredSize(new Dimension(frame.getWidth(), 30));
frame.add(titleBar, BorderLayout.NORTH);
// form title bar (in the titlebar)
titleLabel = new JLabel("Dashboard");
titleLabel.setForeground(Color.white);
titleLabel.setFont(new Font("Arial", Font.BOLD, 16));
titleLabel.setBounds(10,0,200,30);
titleBar.add(titleLabel);
// close the form label
closeLabel = new JLabel("X");
closeLabel.setForeground(Color.white);
closeLabel.setFont(new Font("Arial", Font.BOLD, 16));
closeLabel.setHorizontalAlignment(SwingConstants.CENTER);
closeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
closeLabel.setBounds(frame.getWidth() - 50, 0, 30, 30);
titleBar.add(closeLabel);
closeLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
@Override
public void mouseEntered(MouseEvent e)
{
closeLabel.setForeground(Color.yellow);
}
@Override
public void mouseExited(MouseEvent e)
{
closeLabel.setForeground(Color.white);
}
});
// minimize the form label
minimizeLabel = new JLabel("-");
minimizeLabel.setForeground(Color.white);
minimizeLabel.setFont(new Font("Arial", Font.BOLD, 16));
minimizeLabel.setHorizontalAlignment(SwingConstants.CENTER);
minimizeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
minimizeLabel.setBounds(frame.getWidth() - 80, 0, 30, 30);
titleBar.add(minimizeLabel);
minimizeLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
frame.setState(JFrame.ICONIFIED);
}
@Override
public void mouseEntered(MouseEvent e)
{
minimizeLabel.setForeground(Color.yellow);
}
@Override
public void mouseExited(MouseEvent e)
{
minimizeLabel.setForeground(Color.white);
}
});
dashboardPanel = new JPanel();
dashboardPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
dashboardPanel.setBackground(new Color(30,30,30));
frame.add(dashboardPanel, BorderLayout.CENTER);
// add data panels
addDataPanel("Sales", "$500k");
addDataPanel("Expenses", "$350k");
addDataPanel("Profit", "$150k");
addDataPanel("Customers", "1,000");
// display the chart panel
JPanel chartPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//draw the chart
drawChart(g, getHeight());
}
};
chartPanel.setLayout(new BorderLayout());
chartPanel.setPreferredSize(new Dimension(740, 300));
chartPanel.setBackground(Color.DARK_GRAY);
chartPanel.setBorder(new LineBorder(new Color(255, 204, 0), 2));
// create a title for the chart panel
JLabel chartTitleLabel = new JLabel("Orders");
chartTitleLabel.setFont(new Font("Arial", Font.BOLD, 20));
chartTitleLabel.setHorizontalAlignment(SwingConstants.CENTER);
chartTitleLabel.setOpaque(true);
chartTitleLabel.setBackground(new Color(255,87,34));
chartTitleLabel.setForeground(Color.white);
chartPanel.add(chartTitleLabel, BorderLayout.NORTH);
dashboardPanel.add(chartPanel);
// form dragging
// Mouse listener for window dragging
titleBar.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e)
{
isDragging = true;
mouseOffset = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e)
{
isDragging = false;
}
});
// Mouse motion listener for window dragging
titleBar.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e)
{
if(isDragging)
{
// When the mouse is dragged, this event is triggered
// Get the current location of the mouse on the screen
Point newLocation = e.getLocationOnScreen();
// Calculate the new window location by adjusting for the initial mouse offset
newLocation.translate(-mouseOffset.x, -mouseOffset.y);
// Set the new location of the main window to achieve dragging effect
frame.setLocation(newLocation);
}
}
});
frame.setVisible(true);
}
// create a method to add data to the panel
private void addDataPanel(String title, String value)
{
JPanel dataPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// draw the data panel
drawDataPanel(g, title, value, getWidth(), getHeight());
}
};
dataPanel.setLayout(new GridLayout(2, 1));
dataPanel.setPreferredSize(new Dimension(170, 100));
dataPanel.setBackground(new Color(45,45,45));
dataPanel.setBorder(new LineBorder(new Color(255, 204, 0), 2));
dashboardPanel.add(dataPanel);
}
// Create a Custom method to draw a data panel
private void drawDataPanel(Graphics g, String title, String value, int width, int height)
{
Graphics2D g2d = (Graphics2D) g;
// Customize the data panel appearance here
g2d.setColor(new Color(45,45,45));
g2d.fillRoundRect(0, 0, width, height, 20, 20);
// Stylish background color for Data Panel Title
g2d.setColor(new Color(30,30,30));
g2d.fillRect(0, 0, width, 40);
g2d.setColor(Color.white);
g2d.setFont(new Font("Arial", Font.BOLD, 20));
g2d.drawString(title, 20, 30);
// value
g2d.setColor(Color.white);
g2d.setFont(new Font("Arial", Font.PLAIN, 16));
g2d.drawString(value, 20, 75);
}
// Create a Custom method to draw a chart
private void drawChart(Graphics g, int height)
{
Graphics2D g2d = (Graphics2D) g;
// Customize the chart appearance here
int barWidth = 60;
int barSpacing = 55;
int startX = 50;
int startY = height - 80;
// data values
int[] data = {100,200,150,300,250,350};
// Calculate maximum data value for scaling
int maxDataValue = 0;
for(int value : data)
{
if(value > maxDataValue)
{
maxDataValue = value;
}
}
// Set colors for bars and labels
Color barColor = new Color(0,150,136);
Color labelColor = Color.white;
// Draw the bars and labels
for(int i = 0; i < data.length; i++)
{
int barHeight = (int) ((double)data[i]/maxDataValue * (startY-60));
int x = startX + (barWidth + barSpacing) * i;
int y = startY - barHeight;
g2d.setColor(barColor);
g2d.fillRect(x, y, barWidth, barHeight);
// Draw data labels
g2d.setColor(labelColor);
g2d.setFont(new Font("Arial",Font.BOLD, 14));
g2d.drawString(String.valueOf(data[i]), x+10, y-10);
// Draw product labels (e.g., "Product 1", "Product 2", etc.)
g2d.setFont(new Font("Arial",Font.PLAIN, 12));
g2d.drawString("Product " + (i+1), x+5, startY+20);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Dashboard_Form_Design();
}
}
// Create a Custom Border class for rounded corners
class RoundedBorder implements Border
{
private int radius;
private Color color;
public RoundedBorder(int radius, Color color)
{
this.radius = radius;
this.color = color;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(color);
g2d.drawRoundRect(x, y, width-1, height-1, radius, radius);
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(radius, radius, radius, radius);
}
@Override
public boolean isBorderOpaque() {
return true;
}
}
The Final Result:
More Java Projects:
Download Projects Source Code