I'm trying to add a simple JPanel from a different class with only a JButton and a JTextArea into the GUI.
I'm using IntelliJ IDEA for the swing application and there are no errors. However, the JPanel simply doesn't appear/isn't shown.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
public class GUI {
private JPanel mainPanel;
private JPanel mainInner;
public GUI() {
mainPanel.addContainerListener(new ContainerAdapter() {
#Override
public void componentAdded(ContainerEvent e) {
super.componentAdded(e);
System.out.println("component Added");
}
});
}
public void start() {
JFrame frame = new JFrame("GUI");
frame.setContentPane(new GUI().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
InnerPanel inner = new InnerPanel();
mainInner = inner.getMainInner();
inner.setLayout(new BorderLayout());
mainPanel.setLayout(new BorderLayout());
mainPanel.add(mainInner, BorderLayout.CENTER);
mainPanel.revalidate();
mainPanel.repaint();
frame.pack();
frame.setVisible(true);
}
}
The component listener shows that the component has been added, yet it isn't shown.
You forgot to add the mainPanel to frame:
frame.add(mainPanel);
Related
I have a Java Swing application with a single large class that via CardLayout replaces the various JPanels based on the various buttons. Each JPanel is built in its own class. I would like to divide the various JPanels into different Model-View-Controllers. My application has for example a welcome page with a button that goes to the login page, the login page can go to three different pages: forgotten password, registration and access to the personal page. All pages can go back. How could I do?
For the moment I've done something like this:
Model:
public record Model(String name,String surname) {}
MainView:
import javax.swing.*;
import java.awt.*;
public class MainView {
JFrame frame;
JPanel cardLayoutPanel;
JPanel masterPanel;
CardLayout cl;
MainView(){
frame = new JFrame("Frame");
cardLayoutPanel = new JPanel();
cardLayoutPanel.setLayout(new CardLayout());
cl=(CardLayout) cardLayoutPanel.getLayout();
masterPanel = new JPanel();
masterPanel.setLayout(new BorderLayout());
masterPanel.add(cardLayoutPanel);
frame.getContentPane().add(masterPanel);
frame.setResizable(true);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
MainView viewM= new MainView();
MainController controllerM=new MainController(viewM);
SignUpView viewS = new SignUpView();
SignUpController controllerR=new SignUpController(controllerM,viewS);
controllerM.addPanel(viewS.panel,"SIGNUP");
controllerM.showPanel("SIGNUP");
}
}
MainController:
import javax.swing.*;
public class MainController {
MainView view;
MainController(MainView view){
this.view=view;
}
void addPanel(JPanel panel, String name){
view.cardLayoutPanel.add(panel,name);
}
void showPanel(String panel){
view.cl.show(view.cardLayoutPanel,panel);
}
}
SignUpView:
import javax.swing.*;
public class SignUpView {
JPanel panel;
JTextField name,surname;
JButton submit;
SignUpView(){
panel=new JPanel();
name=new JTextField(20);
surname=new JTextField(20);
submit=new JButton("submit");
panel.add(name);
panel.add(surname);
panel.add(submit);
}
}
SignUpController:
public class SignUpController{
SignUpController(MainController main,SignUpView view){
PersonalAreaView viewPA=new
PersonalAreaView();
PersonalAreaController controllerPA = new PersonalAreaController(main,viewPA);
main.addPanel(viewPA.panel,"PERSONALAREA");
view.submit.addActionListener(e -> {
Model model=new Model(view.name.getText(),view.surname.getText());
controllerPA.setValue(model);
main.showPanel("PERSONALAREA");
});
}
}
PersonalAreaView:
import javax.swing.*;
public class PersonalAreaView{
JPanel panel;
JLabel name,surname;
JButton back;
PersonalAreaView(){
panel=new JPanel();
name=new JLabel();
surname=new JLabel();
back=new JButton("Back");
panel.add(name);
panel.add(surname);
panel.add(back);
}
}
PersonalAreaController:
public class PersonalAreaController{
PersonalAreaView view;
PersonalAreaController(MainController main,PersonalAreaView view){
this.view=view;
view.back.addActionListener(e -> main.showPanel("SIGNUP"));
}
public void setValue(Model model){
view.name.setText(model.name());
view.surname.setText(model.surname());
}
}
The code can be found on GitHub: GitHub code
Is something correct or am I wrong?
Not sure what my issue is. I created a JFrame and I have a panel that will have 4 large buttons (with graphics - though that isn't coded yet) to show on the frame but I am getting an error when it tries to run this and the panel isn't showing up in the frame.
UPDATED: No error message, but no panel or buttons in the frame...
public class EasyExpress {
private static JFrame frame = new JFrame("EASY BUTTONS");
private JButton WriteBTN = new JButton("Write Email");
private JButton EmailBTN = new JButton("View Emails");
private JButton SolBTN = new JButton("Play Solsuite Solitaire");
private JButton ShutBTN = new JButton("Shutdown Computer");
private JPanel btnPanel;
public EasyExpress() {
/* try {
Image img = ImageIO.read(getClass().getResource("write.jpg"));
WriteBTN.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}*/
btnPanel = new JPanel(new GridLayout(1,4,1,1));
btnPanel.setBounds(0, 0, 1200, 400);
WriteBTN.setPreferredSize(new Dimension(300,400));
EmailBTN.setPreferredSize(new Dimension(300,400));
SolBTN.setPreferredSize(new Dimension(300,400));
ShutBTN.setPreferredSize(new Dimension(300,400));
btnPanel.add(EmailBTN);
btnPanel.add(WriteBTN);
btnPanel.add(SolBTN);
btnPanel.add(ShutBTN);
frame.add(btnPanel);
frame.add(frame);
}
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(1200,400);
frame.setVisible(true);
}
Essentially, you're adding a frame to another frame, which you simply can't do
You're also not initialising your buttons, which is causing a NullPointerException.
Start by removing extends JFrame, this is just confusing things and as general rule, you should avoid extending from top level containers. Instead, start with a JPanel, for example...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class EasyExpress extends JPanel {
JButton WriteBTN, EmailBTN, SolBTN, ShutBTN;
JPanel btnPanel;
public EasyExpress() {
btnPanel = new JPanel(new GridLayout(1, 4, 1, 1));
btnPanel.setBounds(0, 0, 1200, 400);
WriteBTN = new JButton("1");
EmailBTN = new JButton("2");
SolBTN = new JButton("3");
ShutBTN = new JButton("4");
WriteBTN.setPreferredSize(new Dimension(300, 400));
EmailBTN.setPreferredSize(new Dimension(300, 400));
SolBTN.setPreferredSize(new Dimension(300, 400));
ShutBTN.setPreferredSize(new Dimension(300, 400));
btnPanel.add(EmailBTN);
btnPanel.add(WriteBTN);
btnPanel.add(SolBTN);
btnPanel.add(ShutBTN);
setLayout(new BorderLayout());
add(btnPanel);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new EasyExpress());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Like #MadProgrammer mentioned in the comments above, you can't have a JFrame inside another JFrame.
However, instead of removing the extends JFrame, I would suggest removing the JFrame inside your EasyExpress object. You already set all the properties for that JFrame in your main, so it will be easier to fix.
Remove JFrame frame = new JFrame("EASY BUTTONS");
Add "EASY BUTTONS" to the EasyExpress object you create in main EasyExpress main = new EasyExpress("EASY BUTTONS");
Remove frame. from in front of frame.add(btnPanel);
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;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class sample {
JTextField field=new JTextField(10);
JPanel panel=new JPanel();
JFrame frame=new JFrame();
String message;
public sample()
{
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
field.setEditable(true);
JButton button=new JButton("press here");
ActionListener listener=new action();
button.addActionListener(listener);
panel.add(field);
panel.add(button);
frame.add(panel);
}
public void getMessage()
{
JTextField f=new JTextField(10);
f.setText(message);
JPanel p=new JPanel();
p.add(f);
frame.add(p);
frame.repaint(); <-- problem here
}
class action implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
message=field.getText();
frame.remove(panel);
getMessage();
}
}
}
//The problem is frame doesn't call repaint method at getMessage method. So, I have to minimize the window to solve this problem. Is their any other way to solve this problem?
The problem is that you added a component to a visible GUI. By default all components have a size of (0, 0) so there is nothing to paint.
What you really need to do is invoke the layout manager so the size/location of the component can be determined.
The basic code should be:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to repaint components
In JDK7 and later you can also do the revalidate()/repaint() on 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...