How do I add JPanels, created by methods, to cardLayout.add(); - java

below is my user class for guiFrames. I want a class that creates several JFrames with methods and pass those methods into cardLayout. The reason for this is each JFrame will be have different buttons displayed depending on what the user has selected.
So, I thought I would create methods for the individual panels and depending on the parameter passed have different buttons displayed. I need the panels to be displayed in a cardLayout. But I am unable to pass the pass the methods into the cardLayout.add(); because it says the method type is invalid. So I tried to make the method return a Component but its not working out. Help please.
import javax.swing.*;
import java.awt.*;
public class guiFrames extends JFrame{
public guiFrames(){
}
public Component inputFrame(){
JFrame inputFrame = new JFrame("Input");
JPanel inputPnl = new JPanel();
inputPnl.setLayout(new GridLayout(3,2));
JLabel loginLbl = new JLabel("Login");
inputPnl.add(loginLbl);
JTextField loginTxt = new JTextField();
inputPnl.add(loginTxt);
JLabel pwLbl = new JLabel("Password");
inputPnl.add(pwLbl);
JTextField pwTxt = new JTextField();
inputPnl.add(pwTxt);
JPanel buttonPnl = new JPanel();
buttonPnl.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));
JButton submit = new JButton("Submit");
buttonPnl.add(submit);
JButton output = new JButton("Output");
buttonPnl.add(output);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(inputPnl, BorderLayout.CENTER);
container.add(buttonPnl, BorderLayout.SOUTH);
inputFrame.add(container);
inputFrame.pack();
inputFrame.setVisible(true);
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
public void cardView(){
JFrame cardFrame = new JFrame();
JPanel cardGUI = new JPanel();
CardLayout cards = new CardLayout();
cardGUI.setLayout(cards);
cardGUI.add(inputFrame(), "first");
cardFrame.add(cardGUI, BorderLayout.CENTER);
cardFrame.pack();
cardFrame.setVisible(true);
cardFrame.setDefaultCloseOperation(cardFrame.EXIT_ON_CLOSE);
}
}

At the end of inputFrame() you aren't returning anything. You need to return the inputFrame, like this:
return inputFrame;
Hope that helps.

Related

Using JPanel to make a calculator: how to use layout?

I'm working on a question that simply states to make an GUI that looks like This calculator, it doesn't have to function, just look like it. So I think I have the JPanel and JButton components right but I can't organize the fields to make it come out right. I'm pretty new so any crash course on how to organize a GUI would be appreciated.
Here's what I have so far:
// Using a JPanel to help lay out components.
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
public class Calculator extends JFrame
{
private final JPanel buttonJPanel2, buttonJPanel3, buttonJPanel4,
buttonJPanel5; // panel to hold buttons
private final JButton[] buttons3, buttons4, buttons5;
private final JButton[] buttons2;
private final JTextField buttonJPanel1;
// no-argument constructor
public Calculator()
{
super("Calculator");
buttonJPanel1 = new JTextField();
add(buttonJPanel1, BorderLayout.NORTH); // add panel1 to JFrame
buttons2 = new JButton[4];
buttons2[0] = new JButton("7");
buttons2[1] = new JButton("8");
buttons2[2] = new JButton("9");
buttons2[3] = new JButton("/");
buttonJPanel2 = new JPanel();
buttonJPanel2.setLayout(new GridLayout(1, buttons2.length));
add(buttonJPanel2, BorderLayout.AFTER_LAST_LINE); // add panel2 to JFrame
buttons3 = new JButton[4];
buttons3[0] = new JButton("4");
buttons3[1] = new JButton("5");
buttons3[2] = new JButton("6");
buttons3[3] = new JButton("*");
buttonJPanel3 = new JPanel();
buttonJPanel3.setLayout(new GridLayout(1, buttons3.length));
add(buttonJPanel3, BorderLayout.AFTER_LAST_LINE); // add panel3 to JFrame
buttons4 = new JButton[4];
buttons4[0] = new JButton("1");
buttons4[1] = new JButton("2");
buttons4[2] = new JButton("3");
buttons4[3] = new JButton("-");
buttonJPanel4 = new JPanel();
buttonJPanel4.setLayout(new GridLayout(1, buttons4.length));
add(buttonJPanel4, BorderLayout.AFTER_LAST_LINE); // add panel4 to JFrame
buttons5 = new JButton[4];
buttons2[0] = new JButton("0");
buttons5[1] = new JButton(".");
buttons5[2] = new JButton("=");
buttons5[3] = new JButton("+");
buttonJPanel5 = new JPanel();
buttonJPanel5.setLayout(new GridLayout(1, buttons5.length));
add(buttonJPanel5, BorderLayout.AFTER_LAST_LINE); // add panel5 to
//JFrame
}
public static void main(String[] args)
{
PanelFrame panelFrame = new PanelFrame();
panelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelFrame.setSize(700, 500);
panelFrame.setVisible(true);
}
} // end class PanelFrame
In short: every component has to be declared,
JButton button1;
initialized
button1 = new JButton("Click me!");
and added to the component above it in the hierarchy (in this case the panel)
panel1.add(button1);
If you do not add the components to the panel and the panel to the frame they will not be part of the GUI, thus not visible.
A JPanel can be set to adjust its layout in different ways using a LayoutManager as you have done with GridLayout (which seems fitting for a calculator). I suggest you read about how to use the grid layout here.
Hope I could help :)
You need to add the buttons to the `JPanel'. For example:
for(JButton b : buttons2) { buttonJPanel2.add(b); }
BorderLayout accepts one component at each location, so if you set BorderLayout.AFTER_LAST_LINE twice, the last add overwrites previous one.

Changing visibility of JPanels in JFrame not working as intended

I'm having an issue with switching between different JPanels in a JFrame. I am trying to add two different panels to a JFrame that the user can go back and forth between, with mainPanel being displayed when the program is run. mainPanel was showing correctly until I added buyPanel to the frame. Now I'm having issues displaying mainPanel again. When I currently run my program, it comes up as blank. The issue seems to be with the last three lines, however I can't figure out what is wrong.
I've included the relevant code below.
public class TestClass extends JFrame {
private JTextArea welcomeText;
private JComboBox commandsMenu;
private JPanel mainPanel;
private JPanel buyPanel;
private JTextArea buyType;
public TestClass() {
super("TestProgram");
setDefaultLookAndFeelDecorated(true);
setSize(550, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Setup elements for main panel (includes commands menu)
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
welcomeText = new JTextArea(10, 10);
welcomeText.setEditable(false);
welcomeText.setLineWrap(true);
welcomeText.setWrapStyleWord(true);
welcomeText.setText(menu());
commandsMenu = new JComboBox();
commandsMenu.setEditable(true);
commandsMenu.setPrototypeDisplayValue("Commands");
commandsMenu.setSize(50, 50);
commandsMenu.addItem("Buy");
commandsMenu.addItem("Quit");
mainPanel.add(commandsMenu);
mainPanel.add(welcomeText);
add(mainPanel);
// Setup elements for buy panel
buyPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
buyType = new JTextArea();
buyType.setText("Type");
buyType.setEditable(false);
buyPanel.add(commandsMenu);
buyPanel.add(buyType);
add(buyPanel);
buyPanel.setVisible(false);
mainPanel.setVisible(true);

Adding onto JPanel

I was wondering if it was possible to add dropdown menus to a main JPanel from a different class instead of calling it from that class itself. Mainly because a friend and I are working on a personal project trying to create different programs in different tabs.
Here's our main GUI:
public class GUI extends JFrame {
public GUI() {
setTitle("Andy and Jack's favorite programs");
JTabbedPane jtp = new JTabbedPane();
getContentPane().add(jtp);
JPanel jp1 = new JPanel();
JLabel label1 = new JLabel();
JPanel jp2 = new JPanel();
JLabel label2 = new JLabel();
jp1.add(label1);
jtp.addTab("Andy - Encryption Program");
jp2.add(label2);
jtp.addTab("Andy - Hello World Program");
}
public static void main(String[] args) {
GUI tp = new GUI();
tp.setVisible(true);
tp.setMinimumSize(new Dimension(400, 400));
}
Here's one of our tabs:
public class encryptionPrograms extends GUI {
String[] options = new String[] { "XOR", "RSA" };
ComboBox optionsList = new JComboBox(options);
jp1.add(optionsList, BorderLayout.CENTER);
}
I'm not sure if I'm doing it correctly or not. Just got into Java and we've have been playing around with the GUI buttons and such.
There is a lot of "wrong" here and without you saying what your intention is with adding a comboBox to your jPanel it's hard to tell you the right way to do it but yes it can be done.
But first of: Always declare your variables before you initialize them so that you can access them for other methods in the class:
public class GUI extends JFrame{
private JPanel jp1,jp2;
private JLabel label1,label2;
private JTabbedPane jtp;
public GUI() {
setTitle("Andy and Jack's favorite programs");
jtp = new JTabbedPane();
jp1 = new JPanel();
label1 = new JLabel();
jp2 = new JPanel();
label2 = new JLabel();
jp1.add(label1);
jtp.addTab("Andy - Encryption Program", jp1);
jp2.add(label2);
jtp.addTab("Andy - Hello World Program",jp2);
getContentPane().add(jtp);
}
If you need to access a variable from another class you can write a get method for it.
For example:
public JPanel getMainJPanel(){
return jp1;
}
Now you can call the getMainJPanel() from another class in order to, for instance, add components to it. Just remember to .revalidate() and .repaint() the main frame after adding more components.

CardLayout in Applet not showing

I'm trying to write an applet that switches between cards using CardLayout, but the app is not showing anything at all and I can't figure out what's wrong. A little help would be much appreciated :)
import javax.swing.*;
import java.awt.*;
public class TEST extends JApplet{
#Override
public void init(){
}
#Override
public void start(){
JPanel cards = new JPanel(new CardLayout());
JPanel main = new JPanel();
main.setLayout(new GridLayout(3, 1, 2, 2));
JTextField jtfEmail = new JTextField("E-mail", 10);
main.add(jtfEmail);
JTextField jtfPassword = new JPasswordField("Password", 10);
main.add(jtfPassword);
JPanel buttons = new JPanel();
JButton jbtLogin = new JButton("Login");
buttons.add(jbtLogin);
JButton jbtRegister = new JButton("Register");
buttons.add(jbtRegister);
main.add(buttons);
cards.add(main, "Main");
CardLayout cardLayout = (CardLayout) cards.getLayout();
cardLayout.show(cards, "Main");
}
}
Problem: You're not adding anything to the applet's contentPane anywhere.
Solution: Do that -- add something to the applet's contentPane so that you can see it.
Also, you will want to look for an applet tutorial on Google and have a look at it.

Trying to create a GUI using java but not able to implement them the way i wanted

import javax.swing.*;
import java.awt.*;
public class SQlUI {
public static void main(String[] args){
SQlUI user=new SQlUI();
user.go();
}
public void go(){
//Creating a Frame
JFrame frame=new JFrame();
//Creating three Panels
JPanel panel0=new JPanel();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
//Creating three Buttons
JButton button0=new JButton("INSERT");
JButton button1=new JButton("UPDATE");
JButton button2=new JButton("DELETE");
//Adding panel0 to the frame which contains three butoon objects
frame.getContentPane().add(BorderLayout.SOUTH,panel0);
panel0.add(button0);
panel0.add(button1);
panel0.add(button2);
//Creating four textbox
JTextField textbox0 = new JTextField(120);
JTextField textbox1 = new JTextField(120);
JTextField textbox2 = new JTextField(120);
JTextField textbox3 = new JTextField(120);
//Adding panel1 to the frame which contains four textbox objects
frame.getContentPane().add(BorderLayout.EAST,panel1);
//Using BoxLayout managaer for panel1 objects
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(textbox0);
panel1.add(textbox1);
panel1.add(textbox2);
panel1.add(textbox3);
//Adding panel2 to the frame which contains four label objects
frame.getContentPane().add(BorderLayout.WEST,panel2);
//Using BoxLayout managaer for panel1 objects
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
//Creating four labels
JLabel label0=new JLabel("Name");
label0.setSize(50,50);
label0.setVisible(true);
JLabel label1=new JLabel("ID");
label1.setSize(50,50);
label1.setVisible(true);
JLabel label2=new JLabel("AGE");
label2.setSize(50,50);
label2.setVisible(true);
JLabel label3=new JLabel("ADDRESS");
label3.setSize(50,50);
label3.setVisible(true);
//Adding labels to panel2
panel2.add(label0);
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
//Setting frame size and visiblity
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Details about the GUI- I am trying to create a GUI which have four textbox where user can enter his details and there are three buttons available on GUI by which user can send the data or store the data. The GUI also contains labels which tells the user which textbox is for which type of data.
Now the allignment of the objects are not coming the way i wanted it. All the labels are coming at one place and the size of box is very big. Tried to find a solution on stalkoverflow but got nothing.
i want something like this----
Name- textbox0
ID- textbox1
AGE- textbox2
ADDRESS- textbox3
Insert Update Delete
but getting something like----
textbox0
textbox1
Name
ID
AGE
Address textbox2
textbox3
Insert Update Delete
This is not the best looking thing you can do, but it's simple and a good start for you.
public class SQlUI {
public static void main(String[] args) {
SQlUI user = new SQlUI();
user.go();
}
public void go() {
JFrame frame = new JFrame();
JPanel buttonsPanel = new JPanel();
JButton insert = new JButton("INSERT");
JButton update = new JButton("UPDATE");
JButton delete = new JButton("DELETE");
buttonsPanel.add(insert);
buttonsPanel.add(update);
buttonsPanel.add(delete);
frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel("Name");
JTextField nameField = new JTextField(120);
namePanel.add(nameLabel);
namePanel.add(nameField);
JPanel idPanel = new JPanel();
JLabel idLabel = new JLabel("ID");
JTextField idField = new JTextField(120);
idPanel.add(idLabel);
idPanel.add(idField);
centerPanel.add(namePanel);
centerPanel.add(idPanel);
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Notice that I call frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH); with the panel as the first argument and the position second, you did it the other way around.
Not sure why you need text fields of length 120.
Call frame.pack() instead of setting the size yourself.
See what indentation does to the code.
You can complete the rest of the panels by yourself.
I suggest reading up on different layouts here. It'll give you more of an idea how you can lay your items out. You can then experiment to find out which one you actually want.
That said I find it far easier to design a GUI graphically, and IntelliJ has a pretty good GUI builder that I've often used. You'll then be able to play around with layout managers and JPanels to get the effect you actually want.

Categories

Resources