I can't get my JFrame from main class to display JPanel from another class. Everything compiles without errors
This is my main class code which extends JFrame:
public OnlineCarSalesSystem(){
setTitle("Online Car Sales System");
setVisible(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new Login());
}
public static void main(String[] args) {
new OnlineCarSalesSystem();
}
In the above code i have added add(new Login()); but it is not displaying that panel on my JFrame. And in the below code i extended my class with the JPanel. And this is the JPanel class code:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JPanel{
JLabel loginLabel = new JLabel("Login ID");
JLabel passwordLabel = new JLabel("Password");
JTextField loginTextField = new JTextField();
JPasswordField passwordTextField = new JPasswordField();
JButton submitButton = new JButton("Submit");
JButton registration = new JButton("new Registration");
JLabel noaccountLabel = new JLabel("No Account yet!!!");
public void Login(){
setBounds(0,0,500,500);
setBackground(Color.red);
setVisible(true);
loginLabel.setBackground(Color.cyan);
passwordLabel.setBackground(Color.cyan);
loginTextField.setBounds(680, 103,90,20);
add(loginTextField);
loginLabel.setBounds(600, 100,90,30);
add(loginLabel);
passwordTextField.setBounds(680, 153,90,20);
passwordTextField.setEchoChar('*');
add(passwordTextField);
passwordLabel.setBounds(600, 150,90,30);
add(passwordLabel);
add(submitButton);
submitButton.setBounds(640,200,90,30);
submitButton.addActionListener(new ActionListener() { //////Submit Button
public void actionPerformed(ActionEvent e) {
}
});
add(registration);
registration.setBounds(638,270,96,30);
add(noaccountLabel);
noaccountLabel.setBackground(Color.cyan);
noaccountLabel.setBounds(640,250,90,30);
registration.addActionListener(new ActionListener() { //////registration Button
public void actionPerformed(ActionEvent e) {
}
});
}
}
The problem is that the Login()-function isn't executed at any point in code. You might want to change
public void Login() { ... }
to
public Login() { ... }
so the code gets executed on object initialization
Related
as you see I have made a very simple program where I input text on a textfield, then press the button to print it on the console:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Main {
private static class Window extends JFrame {
private static class TextField extends JTextField {
private TextField() {
setVisible(true);
setBounds(5, 0, 190,20);
}
}
public class Button extends JButton implements ActionListener {
private Button() {
setText("print");
addActionListener(this);
setVisible(true);
setBounds(5, 35, 190,20);
}
public void actionPerformed(ActionEvent e) {
TextField textField = new TextField();
String input = textField.getText();
System.out.println(input);
}
}
However, while I click the button, it adds blank lines to the console, without the text I have written in it.
Your actionPerformed method creates a new TextField instance and prints the text from that new instance.
A very simple example that works as intended:
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TextAndButtonExample {
public static void main( String[] args ) {
EventQueue.invokeLater(() -> new TextAndButtonExample().start());
}
void start() {
JTextField textField = new JTextField();
JButton button = new JButton(new AbstractAction("Print") {
public void actionPerformed( ActionEvent e ) {
System.out.println(textField.getText());
}
});
JPanel controlPane = new JPanel(new GridLayout(2, 1));
controlPane.add(textField);
controlPane.add(button);
JPanel contentPane = new JPanel();
contentPane.add(controlPane);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Note that I did not extend any Swing components and that I did use a layout manager instead of explicitly setting bounds for components.
i am trying to get it so when a button is pressed a combo box is displayed in java. here is what i have tried.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class main {
public static void main(String[] args) {
final JFrame testFrame = new JFrame();
testFrame.setSize(300,450);
testFrame.setLocation(150,250);
testFrame.setTitle("My frame");
testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
testFrame.setResizable(true);
testFrame.setVisible(true);
testFrame.setLayout(new FlowLayout());
final JButton testButton = new JButton("show");
testFrame.add(testButton);
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == testButton) {
String[] myArray = {"test","test2"};
JComboBox testingCom = new JComboBox(myArray);
testFrame.add(testingCom);
}
}
}
}
}
Any help would be much appreciated. thank you.
Based in your code, you never addActionListener to your button.
In your ActionListener, you are creating a new instance of a combobox each time actionPerformed is called, and i don't if it's that what you want, you may be interested in if it's visible or not.
So you can change your code like this:
final JButton testButton = new JButton("show");
final JComboBox combo = new JComboBox(new String[]{"test1","test2"});
testButton.addActionListener(new ActionListener(){
// this is anonymous class
#Override
public void actionPerformed(ActionEvent evt){
//then you know that is attached to this button
combo.setVisible(!combo.isVisible());
}
});
combo.setVisible(Boolean.FALSE);
testFrame.add(testButton);
testFrame.add(combo);
Simply set the combo visible or invisible in the ActionListener and then revalidate and repaint its container. e.g.,
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
final JComboBox<String> myCombo = new JComboBox<String>(new String[]{"Foo", "Bar"});
final JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(250, 100));
mainPanel.add(new JButton(new AbstractAction("Toggle Combo") {
#Override
public void actionPerformed(ActionEvent arg0) {
myCombo.setVisible(!myCombo.isVisible());
mainPanel.revalidate();
mainPanel.repaint();
}
}));
mainPanel.add(myCombo);
JOptionPane.showMessageDialog(null, mainPanel);
}
}
Perhaps a cleaner way to do it is to use a CardLayout. If you use this, your components won't be shifting on removal and reviewing of the combo. For example:
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Main2 {
public static void main(String[] args) {
final JComboBox<String> myCombo = new JComboBox<String>(new String[]{"Foo", "Bar"});
final CardLayout cardLayout = new CardLayout();
final JPanel cardPanel = new JPanel(cardLayout);
cardPanel.add(myCombo, "combo");
cardPanel.add(new JLabel(), "empty");
final JPanel mainPanel = new JPanel();
mainPanel.add(new JButton(new AbstractAction("Toggle Combo") {
#Override
public void actionPerformed(ActionEvent evt) {
cardLayout.next(cardPanel);
}
}));
mainPanel.add(cardPanel);
JOptionPane.showMessageDialog(null, mainPanel);
}
}
So I'm just checking and when I click my button it won't show my JPanel, any idea why?
Thanks.
I want the third class to show, really do appreciate the help - Thanks allot.
First class - JFrame class.
import javax.swing.JFrame;
public class Frame {
public static void main(String[] args ) {
JFrame frame = new JFrame("JFrame Demo");
Panel panel1 = new Panel();
frame.add(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setVisible(true);
}
}
Second class - Panel 1
import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Panel extends JPanel{
public Panel() {
setLayout(null);
final Panel2 panel2 = new Panel2();
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
panel2.setVisible(true);
}
});
btnNewButton.setBounds(62, 197, 224, 122);
add(btnNewButton);
}
}
Third class - Panel 2 (I want this to show)
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.CardLayout;
import javax.swing.JTextField;
public class Panel2 extends JPanel {
private JTextField textField;
public Panel2() {
setLayout(null);
setVisible(true);
textField = new JTextField();
textField.setBounds(84, 84, 290, 77);
add(textField);
textField.setColumns(10);
}
}
You never add panel2 to anything. A JPanel isn't like a JFrame where setVisible makes it magically appear. You need to add it to a container. Just add it to your Panel.
Also avoid using null layouts. Learn to use Layout Managers
Also see Initial Threads. You want to run your swing apps from the Event Dispatch Thread like this
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Frame();
}
});
}
This looks like a case where you may have been trying to do something along the lines of what a CardLayout achieves. See this example for a basic use. Also see How to Use Card Layout
In the second class, after the second line in the constructor, have you tried?
add(panel2);
See if this works.
Modify Panel.java to look like below. Tell me if this is good for your needs:
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Panel extends JPanel{
Panel2 panel2 = null;
JButton btnNewButton = null;
public Panel() {
setLayout(null);
panel2 = new Panel2();
panel2.setBounds(5,5,300,500);
add(panel2);
showPanel2(false);
btnNewButton = new JButton("New button");
btnNewButton.setBounds(62, 197, 224, 122);
add(btnNewButton);
showButton(true);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showButton(false);
showPanel2(true);
}
});
}
public void showPanel2(boolean bshow)
{
panel2.setVisible(bshow);
}
public void showButton(boolean bshow)
{
btnNewButton.setVisible(bshow);
}
}
I have 2 panel (2 class, extends from JPanel), 1 frame (1 class, extends from JFrame)
My first panel - WelcomePanel:
package caro;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class WelcomePanel extends JPanel {
public WelcomePanel() {
ImageIcon logoImage = new ImageIcon("/home/khanhpq/logo.png");
JButton playButton = new JButton("Play");
JButton exitButton = new JButton("Exit");
JLabel imageLabel = new JLabel(logoImage);
add(imageLabel);
add(playButton);
add(exitButton);
playButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
}
});
exitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int option = JOptionPane.showConfirmDialog(null, "Are you sure ?", "Warning", JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
}
}
My second panel - BoardPanel:
package caro;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class BoardPanel extends JPanel {
public BoardPanel() {
JPanel boardPanel = new JPanel();
Board board = new Board();
CellButton cellButton[] = new CellButton[144];
GridLayout gridLayout = new GridLayout(12, 12);
boardPanel.setLayout(gridLayout);
for (int i = 0; i < 144; i++) {
cellButton[i] = new CellButton();
boardPanel.add(cellButton[i]);
}
}
}
My main frame - MainFrame
package caro;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
public MainFrame() {
add(new WelcomePanel());
setSize(360, 380);
setVisible(true);
}
public static void main(String[] args) {
MainFrame startFrame = new MainFrame();
}
}
My question: Help me write code, addActionListener on buttons of panels (material example). When i press play button (of WelcomePanel), WelcomePanel is hidden and BoardPanel is show. And, when i exit BoardPanel (close button is pressed, or click x button), WelcomePanel is showed.
My friend recommend use Message and Handle, but I don't know. Please help me. Thanks.
Its better to declare dependencies (Component like buttons, panels, etc...) as fields. In this way they are visible for a third class that is the Controller of them. In next examaple I make MainFrame controlling itself, just an example. Read about Presentation patterns for better pratices.
WelcomePanel.java
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class WelcomePanel extends JPanel {
/* Declare your dependecies as fields,
* so you can hold a reference.
*/
ImageIcon logoImage;
JButton playButton;
JButton exitButton;
JLabel imageLabel;
public WelcomePanel() {
logoImage = new ImageIcon("/home/khanhpq/logo.png");
playButton = new JButton("Play");
exitButton = new JButton("Exit");
imageLabel = new JLabel(logoImage);
add(imageLabel);
add(playButton);
add(exitButton);
}
}
BoardPanel.java
import javax.swing.JButton;
import javax.swing.JPanel;
public class BoardPanel extends JPanel {
/* Declare your dependecies as fields,
* so you can hold a reference.
*/
JButton closeButton;
public BoardPanel() {
closeButton = new JButton();
add(closeButton);
}
}
MainFrame.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainFrame extends JFrame implements ActionListener {
/* Declare your dependecies as fields,
* so you can hold a reference.
*/
WelcomePanel welcomePanel;
BoardPanel boardPanel;
public MainFrame() {
welcomePanel = new WelcomePanel();
boardPanel = new BoardPanel();
add(welcomePanel);
add(boardPanel);
boardPanel.setVisible(false);
boardPanel.closeButton.addActionListener(this);
welcomePanel.playButton.addActionListener(this);
setSize(360, 380);
}
/**
* This class is the controller.
*/
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(boardPanel.closeButton)) {
welcomePanel.setVisible(false);
boardPanel.setVisible(true);
} else if (e.getSource().equals(welcomePanel.playButton)) {
welcomePanel.setVisible(true);
boardPanel.setVisible(false);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MainFrame startFrame = new MainFrame();
startFrame.setVisible(true);
}
});
}
}
Folks, I intend to implement a simple button ActionListener but it appears to not work.
The java codes are attached as following.....
The program intends to respond the click action from user and change the context of the JLabel
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Frame extends JFrame implements ActionListener {
public JButton btn = new JButton("Click");
public JLabel display = new JLabel("null");
public JPanel mainPanel = new JPanel();
public Frame() {
mainPanel.add(btn);
mainPanel.add(display);
add(mainPanel);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn) {
display.setText("it works");
System.out.println("it works");
}
}
public static void main(String[] args) {
Frame testFrame = new Frame();
testFrame.pack();
testFrame.setVisible(true);
}
}
Your actionlistener (the JFrame itself) is not added with addActionListener.
You should add ActionListner to the button
public Frame(){
mainPanel.add(btn);
mainPanel.add(display);
btn.addActionListener(this);
add(mainPanel);
}