How to put JOutlookBar into a JPanel in Java - java

How can I put a JOutlookBar into a JPanel?
Here is my code:
JFrame frame = new JFrame("JOutlookBar Test");
JOutlookBar outlookBar = new JOutlookBar();
outlookBar.addBar("One", getDummyPanel1("one"));
outlookBar.addBar("Two", getDummyPanel2("Two"));
outlookBar.addBar("Three", getDummyPanel3("Three"));
outlookBar.addBar("Four", getDummyPanel4("Four"));
outlookBar.addBar("Five", getDummyPanel5("Five"));
outlookBar.setVisibleBar(0);
frame.getContentPane().add(outlookBar);
frame.setSize(800, 600);

Adding frame.setVisible(true); allowed me to view your code working correctly.
Below is full working example of the code you provided, copy and paste it into a file named Library.java and it'll be grand!
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Library {
public static void main(String[] args) {
JFrame frame = new JFrame("JOutlookBar Test");
JPanel left = new JPanel();
JPanel center = new JPanel();
JOutlookBar outlookBar = new JOutlookBar();
outlookBar.addBar("One", getDummyPanel1("one"));
outlookBar.addBar("Two", getDummyPanel1("Two"));
outlookBar.addBar("Three", getDummyPanel1("Three"));
outlookBar.addBar("Four", getDummyPanel1("Four"));
outlookBar.addBar("Five", getDummyPanel1("Five"));
outlookBar.setVisibleBar(0);
//Add outlookbar to left panel
left.add(outlookBar);
//Add some content to center panel...
center.add(new JTextArea());
//Add left and center panels with layout styles
frame.setLayout(new BorderLayout());
frame.add(left, BorderLayout.WEST);
frame.add(center, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Show JFrame
frame.setVisible(true);
}
public static JPanel getDummyPanel1(String num) {
JPanel panel = new JPanel();
panel.add(new JButton("num"));
return panel;
}
}

Related

How to constantly update a JPanel

I am trying to make a program where I have two JPanels inside a JFrame, one of which contains a canvas. I am trying to find a way to get that canvas to be constantly updating so that I could create something like a game inside the canvas. I was wondering how I could make it so that the JPanel with the canvas in it is constantly refreshing so that you can see when something is changed in the canvas. Here is my code:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main
{
private JFrame frame;
private JPanel mainPanel;
private JPanel gamePanel;
private JPanel sidePanel;
public void setup()
{
frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocation(100, 50);
mainPanel = new JPanel();
Dimension d = new Dimension(800, 600);
mainPanel.setMaximumSize(d);
mainPanel.setMinimumSize(d);
mainPanel.setPreferredSize(d);
frame.add(mainPanel);
frame.pack();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
}
public void setupPanels()
{
gamePanel = new JPanel();
Dimension d = new Dimension(600, 600);
gamePanel.setMaximumSize(d);
gamePanel.setMinimumSize(d);
gamePanel.setPreferredSize(d);
gamePanel.setBackground(Color.RED);
mainPanel.add(gamePanel);
sidePanel = new JPanel();
d = new Dimension(600, 600);
sidePanel.setMaximumSize(d);
sidePanel.setMinimumSize(d);
sidePanel.setPreferredSize(d);
sidePanel.setBackground(Color.BLUE);
mainPanel.add(sidePanel);
}
public void setupGame()
{
GameArea game = new GameArea();
gamePanel.add(game);
game.start();
}
public static void main(String[] args)
{
Main main = new Main();
main.setup();
main.setupPanels();
main.setupGame();
}

JFrame doesn't conform JPanel's setMinimunSize()

I expect JPanel's setMinimumSize() to confine the resizing of JFrame too, but it doesn't.
The following is my example code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class AutoResize{
public static void main(String[] args) {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
leftPanel.setBackground(Color.RED);
rightPanel.setBackground(Color.BLUE);
leftPanel.setSize(500,400);
rightPanel.setSize(500,400);
Dimension d = new Dimension(450,300);
leftPanel.setMinimumSize(d);
rightPanel.setMinimumSize(d);
JSplitPane split;
split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
split.setDividerLocation(400);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(split, BorderLayout.CENTER);
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
If you resize JFrame into a very small size it produces this:
What I want is that JFrame couldn't be resized into a smaller region than JPanels' minimum size. Is there anyway to implement this?
All I need is adding frame.setMinimumSize(); , I feel dumb.
Thanks #Andrew Thompson #MadProgrammer and #user1803551
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class AutoResize{
private final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private final double w = screenSize.getWidth();
private final double h = screenSize.getHeight();
private final Dimension d = new Dimension((int) w/3,(int) h/3);
public void run() {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
leftPanel.setBackground(Color.RED);
rightPanel.setBackground(Color.BLUE);
//leftPanel.setSize(500,400);
//rightPanel.setSize(500,400);
leftPanel.setMinimumSize(d);
rightPanel.setMinimumSize(d);
JSplitPane split;
split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
split.setDividerLocation(400);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(split, BorderLayout.CENTER);
frame.setSize((int) w,(int) h);
//frame.pack();
frame.setMinimumSize(new Dimension((int) w/3*2,(int) h/3*2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
AutoResize test = new AutoResize();
test.run();
}
}

how to make some specific widths and heights and some auto with jframes - Java

I wanna make a jframe that looks something like this:
As shown on image, I wanna have right side panel to have width of 200px and bottom panel height of 80px, then everything else to be auto, as I want the window to be resizable.
Could someone please help me to achieve this? I've been trying with gridLayout, BorderLayout and others. I've seen something with insets, but can't figure it out.
I believe this should do the trick.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
public static void main(String[] args) {
MainFrame frame = new MainFrame();
JPanel centerWrapper = new JPanel();
JPanel center = new JPanel();
center.setBackground(Color.BLUE);
JPanel right = new JPanel();
right.setBackground(Color.CYAN);
right.setPreferredSize(new Dimension(200, 10));
centerWrapper.setLayout(new BorderLayout());
centerWrapper.add(center, BorderLayout.CENTER);
centerWrapper.add(right, BorderLayout.EAST);
JPanel bottom = new JPanel();
bottom.setPreferredSize(new Dimension(640, 200));
bottom.setBackground(Color.GREEN);
frame.setLayout(new BorderLayout());
frame.add(centerWrapper, BorderLayout.CENTER);
frame.add(bottom, BorderLayout.SOUTH);
// you can now use center, right and bottom
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

swing and GUI components not appearing

I'm a complete noobie with swing. I'm trying to set a few JPanels and TextAreas to show up but after spending 2 days reading the APIs and trying to add panels to frames and textareas to panels and nothing is showing up.. I'm utterly confused. If anyone could explain how is the best way to do this I would be very grateful
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new FlowLayout()); // J FRAME
JPanel panel = new JPanel(); // first panel on the left
panel.setLayout(new BoxLayout(panel, 1));
// frame.getContentPane().setBackground(Color.red);
frame.add(panel);
JLabel surname = new JLabel();
JLabel initial = new JLabel();
JLabel ext = new JLabel();
surname.setOpaque(true);
initial.setOpaque(true);
ext.setOpaque(true);
frame.add(surname);
panel.add(initial);
panel.add(ext);
JTextArea table = new JTextArea();
table.setEditable(false);
panel.add(table);
table.setVisible(true);
You're adding stuff to the JFrame after it's already visible. If you do that, you need to revalidate your JFrame so it knows to redo its layout.
You could also just wait to show your JFrame until after you've added everything.
Edit: Here is an example program that shows what I'm talking about. Try running this, then take out the call to revalidate() to see the difference.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public static void main(String[] args) {
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton show = new JButton("Show");
show.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent showE) {
frame.add(new JLabel("Test"), BorderLayout.SOUTH);
frame.revalidate(); //tell the JFrame to redo its layout!
}
});
frame.add(show);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
You are adding an empty elements like:
JLabel surname = new JLabel();
Your elements is already added but have nothing to be display.
Try :
JLabel surname = new JLabel("UserName");
JLabel initial = new JLabel("Iinitial");
JLabel ext = new JLabel("Ext");
JTextArea table = new JTextArea(10, 5);

Creating a class and Going into the Frame

I am trying to create this Panel into a class but it is not working, trying to make it go into the Frame as well. I am getting the "It is not a class error"
Please explain to me what I am doing wrong. Programming is fun until you are stuck for hours on one problem.
Panel:
import java.awt.Button;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TopPanel extends JPanel {
public TopPanel{
JPanel panel = new JPanel();
JFrame frame = new JFrame("Create a frame");
frame.getContentPane().add(panel);
Button button = new Button("111");
JLabel Crse = new JLabel("Course Info");
Crse.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(Crse);
panel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
Frame:
import javax.swing.*;
import java.awt.*;
public class CourseGUI extends JFrame {
public CourseGUI()
{
super("CourseGUI Frame");
JPanel topPanel = new JPanel();
topPanel.setBackground(java.awt.Color.WHITE);
Dimension d = new Dimension(800,600);
topPanel.setPreferredSize(d);
this.setLayout(new BorderLayout());
this.add(topPanel, BorderLayout.NORTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
TopPanel.setLayout(new BorderLayout());
TopPanel.add(Crse, BorderLayout.NORTH);
this.setVisible(true);
}
public static void main(String[] args)
{
new CourseGUI();
}
}
Thanks in advanced.
I changed the TopPanel:
import javax.swing.*;
import java.awt.*;
public class TopPanel extends JPanel {
public TopPanel(){
JPanel panel = new JPanel();
JLabel Crse = new JLabel("Course Info");
Crse.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(Crse);
panel.add(button);
}
}
TopPanel is your class name, topPanel is your JPanel instance. (Java is case sensitive).
Lines like
TopPanel.setLayout(new BorderLayout());
TopPanel.add(Crse, BorderLayout.NORTH);
Are trying to use the class which is not what you intended...
Your are also missing () on the line public TopPanel { (the one inside the class, not the one defining the class)
Crse is a local variable in the TopPanel creator, so you can't use it inside CourseGUI()
TopPanel is creating a frame to put itself into which is weird...

Categories