CardLayout in Applet not showing - java

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.

Related

addactionListener(this) How do I place this line of code in correctly?

So I am designing a chatroom and before I can read up on sockets I need to finish up this GUI. Basically I am mostly using TextDemo as my guide. I like how it displays so I figured it'd be an easy spot to start with my code. In my code it breaks whenever I try to put in:
input.addActionListener(this);
When I comment out that line it goes back to displaying/running perfectly. Due to my errors, it looks like I'm putting it in the wrong location. I've tried moving it around a bit but I don't seem to have the problem solving skills to fix this yet. Can someone help correct me and explain what I am doing wrong here?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JPanel implements ActionListener
{
private final static String newline = "\n";
///// CREATING THE GUI /////
JFrame frame = new JFrame("Chatroom");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JPanel chatpanel = new JPanel();
JPanel inputpanel = new JPanel();
JPanel sendpanel = new JPanel();
JTextArea chat = new JTextArea(19, 49);
JTextArea input = new JTextArea(3, 40);
JScrollPane chatscroll = new JScrollPane(chat,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane inputscroll = new JScrollPane(input,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JButton connectbutton = new JButton("Connect");
JButton disconnectbutton = new JButton("Disconnect");
JButton send = new JButton("Send");
JLabel label = new JLabel();
///// GUI CONSTRUCTOR /////
public GUI()
{
chatroomGUI();
}
public void chatroomGUI()
{
///// GUI DISPLAY /////
frame.setVisible(true);
frame.setSize(800, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setBackground(Color.GRAY);
panel2.setBackground(Color.lightGray);
chatpanel.setBackground(Color.lightGray);
inputpanel.setBackground(Color.lightGray);
sendpanel.setBackground(Color.lightGray);
///// ACTION LISTENER /////
//input.addActionListener(this);
chat.setEditable(false);
chat.setFont(new Font("Dialog", Font.PLAIN, 12));
chat.setLineWrap(true);
chat.setWrapStyleWord(true);
input.setFont(new Font("Fialog", Font.PLAIN, 12));
input.setLineWrap(true);
input.setWrapStyleWord(true);
sendpanel.setLayout(new BorderLayout(0, 0));
sendpanel.setPreferredSize(new Dimension(95, 50));
chatpanel.setLayout(new FlowLayout());
chatpanel.setPreferredSize(new Dimension(565, 320));
///// ADD AREA /////
chatpanel.add(chatscroll);
inputpanel.add(inputscroll);
inputpanel.add(sendpanel, BorderLayout.EAST);
sendpanel.add(send, BorderLayout.CENTER);
panel.add(connectbutton);
panel.add(disconnectbutton);
panel.add(label);
panel2.add(chatpanel);
panel2.add(inputpanel);
frame.add(panel, BorderLayout.WEST);
frame.add(panel2);
}
///// ACTION PERFORMED /////
/*The following will take any text that is typed inside of
the "input" area and display it in the "chat" screen area.*/
public void actionPerformed(ActionEvent evt)
{
String text = input.getText();
chat.append(text + newline);
input.selectAll();
chat.setCaretPosition(chat.getDocument().getLength());
}
}
Note: My main is in another class. That code just simply looks like:
public class Chatroom
{
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GUI();
}
});
}
}
JTextArea does not support the ActionListener API, so it does not have a addActionListener method. You should consult the JavaDocs and tutorials first
Depending on what you are trying to do, you might consider using a DocumentListener or a DocumentFilter or use the key bindings API, for example

Java GUI - Possible to store JPanels inside a single main JPanel?

I am working on a semester project that I have and I was wondering if it was possible to store 3-4 JPanels instead one single "main" JPanel. The reason for me asking this is because I a trying to make a GUI checkbook program and my checkbook has 7 buttons that should open a new window once I click on it. To switch between each window I'm going to have to use the CardLayout, but my understand of the CardLayout is that I can only assign one single JPanel to that card so I can't assign multiple JPanels to a single Card layout so when the user clicks on a different card 3-4 different JPanels appear.
The reason that I am asking this is because I asked for help earlier and received help for creating my first window in this project, it produces the output I want PERFECTLY, but uses more than 1 JPanel in doing so. Since this prevents me from continuing on to the other steps of my 7 GUI Windows, I am stuck.
Here is the code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class checkbook extends JPanel implements ActionListener {
private static final String title = "Use The Buttons Below To Manage Transactions";
private static final String[] bottomButtons = { "Create a New Account",
"Load a Trans from a File", "Add New Transactions",
"Search Transactions", "Sort Transactions",
"View/Delete Transactions", "Backup Transaction", "Exit" };
static JButton Button[] = new JButton[8];
static ActionListener AL = new checkbook();
public checkbook() {
JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 18));
JPanel titlePanel = new JPanel();
titlePanel.add(titleLabel); // put it in a JPanel so it will expand to fill BoxLayout
JTextField textfield = new JTextField();
JPanel accountBalancePanel = new JPanel();
accountBalancePanel.add(new JLabel("Account Name:"));
accountBalancePanel.add(new JTextField(10));
accountBalancePanel.add(Box.createHorizontalStrut(4));
accountBalancePanel.add(new JLabel("Balance:"));
textfield = new JTextField("0.0", 10);
textfield.setHorizontalAlignment(JTextField.RIGHT);
accountBalancePanel.add(textfield);
JPanel northPanel = new JPanel();
northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.PAGE_AXIS));
northPanel.add(titlePanel);
northPanel.add(accountBalancePanel);
JPanel southBtnPanel = new JPanel(new GridLayout(2, 4, 1, 1));
for(int i = 0; i < 8; i++){
Button[i] = new JButton(bottomButtons[i]);
southBtnPanel.add(Button[i]);
Button[i].addActionListener(AL);
}
setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(Box.createRigidArea(new Dimension(100, 100))); // just an empty placeholder
add(southBtnPanel, BorderLayout.SOUTH);
}
private static void createAndShowGui() {
checkbook mainPanel = new checkbook();
JFrame frame = new JFrame("Checkbook");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
createAndShowGui();
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Button[7]) {
System.exit(0);
}
}
}
Credit goes to Hovercraft Full Of Eels for showing me the above example
If there is anything that is unclear about my question, please ask and I will do the best I can to fix it.
Here is what the code produces:
http://i.stack.imgur.com/WY0c3.png

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

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.

Add Jpanel to Jframe NetBeans

Hi all, I was in a development of my college mini project. It was a Library Management system , which i should do in Java swing using Net-beans IDE. First I was doing manual coding. It takes time.
In manual coding I create single JFrame with Menu bar and and items, on all action performed I wrote codes for all Jpanels. It made the file and code huge. and confusing too.
Now I need your help.
I have created a Main JFrame with Menu
A JPanel - ADD Book
another Jpanel - On add book success (demo! , for Actions happening in ADD Book )
I had made action listener
addBook addbk = new addBook();
this.getContentPane().add(addbk);
wrote this code.
I doesn't make sense.
Friends, As a new to java, What i need to study is
1.) How cal and Show an external Jpanel an action performed
2.) How to show another JPanel to same root JFrame if any event has occurred in external JPanel.
In sort, something like Iframe in HTML
Thank you all.
http://compilr.com/abelkbil/openlib/OpenLibMainGUI.java
http://compilr.com/abelkbil/openlib/addBook.java
http://compilr.com/abelkbil/openlib/bookAdded.java
CardLayout, is exactly what you need for this situation. And do learn Java Naming Conventions and stick to them, as you are a beginner, so to be on the right track from the start is always GOOD.
Here is one example, that you can look at :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel panel2;
private MyPanel panel3;
private JComboBox choiceBox;
private String[] choices = {
"Panel 1",
"Panel 2",
"Panel 3"
};
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
choiceBox = new JComboBox(choices);
panel1 = new MyPanel(contentPane
, Color.RED.darker().darker(), this);
panel2 = new MyPanel(contentPane
, Color.GREEN.darker().darker(), this);
panel3 = new MyPanel(contentPane
, Color.DARK_GRAY, this);
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
contentPane.add(panel3, "Panel 3");
frame.getContentPane().add(choiceBox, BorderLayout.PAGE_START);
frame.getContentPane().add(contentPane, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JComboBox getChoiceBox()
{
return choiceBox;
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class MyPanel extends JPanel
{
private JButton jcomp1;
private JPanel contentPane;
private Color backgroundColour;
private JComboBox choiceBox;
public MyPanel(JPanel panel, Color c, CardLayoutExample cle)
{
contentPane = panel;
backgroundColour = c;
choiceBox = cle.getChoiceBox();
setOpaque(true);
setBackground(backgroundColour);
//construct components
jcomp1 = new JButton ("Show New Panel");
jcomp1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String changeToPanel = (String) choiceBox.getSelectedItem();
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, changeToPanel);
}
});
add(jcomp1);
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
}
Else you can have a look at this example also.

BoxLayout program not working

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class File
{
private JFrame frame1;
private JPanel panel1;
private JPanel panel2;
private JLabel labelWeight;
private JLabel labelHeight;
File()
{
frame1 = new JFrame();
panel1 = new JPanel();
panel2 = new JPanel();
labelWeight = new JLabel("Weight :");
labelHeight = new JLabel("Height :");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
panel1.setLayout(new FlowLayout());
panel1.add(labelWeight);
panel2.setLayout(new FlowLayout());
panel2.add(labelHeight);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
frame1.setLayout(new BoxLayout(frame1,BoxLayout.X_AXIS));
panel1.setAlignmentY(0);
panel2.setAlignmentY(0);
frame1.add(panel1);
frame1.add(panel2);
frame1.setSize(400, 200);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
public static void main (String args[])
{
new File();
}
}
It gives BoxLayout Sharing error at runtime
Generally, LayoutManagers are set on a JPanel. I guess JFrame implements this method to forward it to the content pane of the frame. I would suggest you try:
Container contentPane = frame1.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.X_AXIS));
If you still have problems take a look at the Swing tutorial on How to Use Box Layout for working examples.
Swing components should be created in the Event Dispatch Thread. Try this in your main():
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new File();
}
});
But your problem may be the same as this question.

Categories

Resources