Java - Button in a button - java

I'm making a program with 2 JButton, however I have a MyFrame class and the buttons are in a different class named KnoppenPanel. The problem is, when I do this I will get a JButton in a JButton. So I have my 2 buttons and another Button surrounding these. How do I solve this?
MyFrame:
public class MyFrame extends JFrame {
Uithangbord u = new Uithangbord();
KnoppenPanel kp = new KnoppenPanel();
public MyFrame() {
setLayout(new FlowLayout());
add(u);
add(kp);
setSize(280, 180);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class KnoppenPanel extends JButton implements ActionListener {
private JButton b, b2;
private JPanel p1;
Uithangbord bord = new Uithangbord();
public KnoppenPanel() {
p1 = new JPanel();
add(p1);
b = new JButton("Open");
p1.add(b);
b.addActionListener(this);
b2 = new JButton("Gesloten");
p1.add(b2);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
JButton knop = (JButton)(event.getSource());
if (knop == b) {
b.setEnabled(false);
b2.setEnabled(true);
bord.maakOpen();
}
if (knop == b2) {
b2.setEnabled(false);
b.setEnabled(true);
bord.maakGesloten();
}
}
}

You class is extending JButton. Then when you add your JPanel (with 2 JButton) you are adding this in a JButton.
I think what do you want is the KnoppenPanel have only the 2 JButtons then you just need to change:
public class KnoppenPanel extends JButton implements ActionListener {
By
public class KnoppenPanel extends JPanel implements ActionListener {
If you do this change, you can also directly add the JButton in the KnoppenPanel like that:
public KnoppenPanel() {
b = new JButton("Open");
add(b);
b.addActionListener(this);
b2 = new JButton("Gesloten");
add(b2);
b2.addActionListener(this);
}

Related

Add JPanel from other Class to JPanel

I would like to add a JPanel from another class to a JPanel:
class FirstPanel extends JPanel
{
private JButton button;
FirstPanel()
{
setLayout(null);
setVisible(true);
button = new JButton();
button.setBounds(x, y, width, height);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(SecondPanel.class.getResource(filePath)));
button.setBackground(bgColor);
button.setForeground(Color.white);
button.setVisible(true);
Border emptyBorder = BorderFactory.createEmptyBorder();
button.setBorder(emptyBorder);
add(button);
ButtonActionHandler buttonActionHandler = new ButtonActionHandler();
}
public class ButtonActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setVisible(true);
add(new SecondJPanel());
new SecondJPanel().setVisible(true);
}
} }
And this is my Second JPanel:
class SecondPanel extends JPanel
{
private JButton button;
private JLabel titleLabel;
SecondPanel()
{
setLayout(null);
setVisible(true);
button = new JButton();
button.setBounds(100, 200, 100, 200);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(SecondPanel.class.getResource(filePath)));
button.setBackground(bgColor);
button.setForeground(Color.white);
button.setVisible(true);
Border emptyBorder = BorderFactory.createEmptyBorder();
button.setBorder(emptyBorder);
add(button);
}
}
The Launching of the First Panel through a JFrame (from another class) works, however the adding of the second JPanel to the first one doesn't.
Any help is really appreciated
I'm sorry, but you don't seem to be understanding what I am telling you.
Composition is your friend.
I would do it like this:
public class JPanel1 extends JPanel {
private JButton button;
public JPanel1(ActionListener buttonListener) {
this.button = new Button("Push me");
this.button.addActionListener(buttonListener);
// do what's needed to add the button to the display.
}
}
public class JPanel2 extends JPanel {
private JButton button;
public JPanel2(ActionListener buttonListener) {
this.button = new Button("Push me");
this.button.addActionListener(buttonListener);
// do what's needed to add the button to the display.
}
}
public class TwoPanelFrame extends JFrame {
public TwoPanelFrame(JPanel p1, JPanel p2) {
// add the two panels to your frame and display.
}
}

Why launching does it random?

When i run this program sometimes shows me all buttons, but sometimes only 2 or 3 or 4 or 5 or even just 1.. why is that??
I really do not get it. There should always be 6 buttons, but it doesnt show them. Is there any logical reason?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class testnet
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Knjigarna");
frame.setVisible(true);
frame.setSize(800,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton button1 = new JButton("Prikazi vse");
panel.add(button1);
button1.addActionListener (new Action1());
JButton button2 = new JButton("Prikazi knjigo");
panel.add(button2);
button2.addActionListener (new Action2());
JButton button3 = new JButton("Dodaj knjigo");
panel.add(button3);
button3.addActionListener (new Action3());
JButton button4 = new JButton("Brisi knjigo");
panel.add(button4);
button4.addActionListener (new Action4());
JButton button5 = new JButton("Uredi knjigo");
panel.add(button5);
button5.addActionListener (new Action5());
JButton button6 = new JButton("Izhod");
panel.add(button6);
button6.addActionListener (new Action6());
}
static class Action1 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame2 = new JFrame("Pikaz vseh knjig");
frame2.setVisible(true);
frame2.setSize(500,800);
JLabel label = new JLabel("Seznam vseh knjig:");
JPanel panel = new JPanel();
JTextField text1=new JTextField("Naslov: ");
JTextField text2=new JTextField("Avtor: ");
frame2.add(panel);
panel.add(label);
panel.add(text1);
panel.add(text2);
}
}
static class Action2 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame3 = new JFrame("Prikaz knjige");
frame3.setVisible(true);
frame3.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige:");
JPanel panel = new JPanel();
frame3.add(panel);
panel.add(label);
}
}
static class Action3 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame4 = new JFrame("Dodajanje knjige");
frame4.setVisible(true);
frame4.setSize(600,300);
JLabel label = new JLabel("Vpisi podtke o knjigi");
JPanel panel = new JPanel();
frame4.add(panel);
panel.add(label);
}
}
static class Action4 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame5 = new JFrame("Brisanje knjige");
frame5.setVisible(true);
frame5.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis brisati");
JPanel panel = new JPanel();
frame5.add(panel);
panel.add(label);
}
}
static class Action5 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame6 = new JFrame("Urejanje knjige");
frame6.setVisible(true);
frame6.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis urejati");
JPanel panel = new JPanel();
frame6.add(panel);
panel.add(label);
}
}
static class Action6 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
try something with layout. JFrame and or remove managed by inside with a content pane. content pane default layout is BorderLayout. so you need to try border layout stuff.
or you can try this code in you main method
frame.setLayout(new FlowLayout());
this will add component by one by one.
for more about layout you can get in here
This is just a fix and not really an explanation of why the problem is occurring.
Call frame.revalidate() after adding all the buttons.
From the Java Docs,
public Component add(Component comp)
This method
changes layout-related information, and therefore, invalidates the
component hierarchy. If the container has already been displayed, the
hierarchy must be validated thereafter in order to display the added
component.

How to replace current JPanel with another JPanel?

I am trying to transit from a UserAdminPanel to AdminLogin within the same JPanel when I press the Admin button.
UserAdmin Panel
transit to AdminLogin Panel
The problem I have now is that I am opening up a new panel instead of changing the current panel to the new panel.
This is my code for the UserAdminPanel
public class SelectAdminUserPanel extends JPanel
{
public SelectAdminUserPanel()
{
setLayout(new GridLayout(3,1));
JButton b1 = new JButton("User Login");
JButton b2 = new JButton("Admin Login");
JButton b3 = new JButton("Exit");
b1.addActionListener(new SelectUserButtonListener() );
b2.addActionListener(new SelectAdminButtonListener());
b3.addActionListener(new SelectExitButtonListener() );
add(b1);
add(b2);
add(b3);
}
private class SelectAdminButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
AdminModule am = new AdminModule();
am.run();
}
}
private class SelectUserButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
GameModule gm = new GameModule();
gm.run();
}
}
private class SelectExitButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
}
}
}
This is the code for the AdminLogin Panel
public class AdminLoginPanel extends JPanel
{
AdminLoginPanel()
{
JLabel pwlabel = new JLabel("Password");
JPasswordField pwfield = new JPasswordField(20);
JButton loginbutton = new JButton("Login");
add(pwlabel);
add(pwfield);
add(loginbutton);
}
}
I have looked at the following example and this example but it's not very applicable because it talks about CardLayout instead of like rewriting the current JPanel.
I think that you should have a reference to your main frame and just remove the components from it based on the button pressed and add only the required components. From what you say, UserAdminPanel is your main panel. I think it's added to a frame for which you can obtain a reference. When you click a button, you want to remove all the content shown on it and display only what the button clicked should show. I think it should look something like this:
private class SelectAdminButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.getContentPane().removeAll();
AdminModule am = new AdminModule();
frame.add(am.getNewPanel());
frame.pack();
// am.run(); //it's not clear what does for you
}
}
Where the method getNewPanel() would return the underlying JPanel. I'm assuming that AdminModule has a reference to the AdminLoginPanel.

How do I change the JLabel text by clicking on my JButton?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class myAction extends JFrame implements ActionListener {
myAction() {
super("Lab 4 Question 1");
Container c = getContentPane();
JPanel p = new JPanel();
JPanel p1 = new JPanel();
JButton b = new JButton("Button 1");
JButton b1 = new JButton("Button 2");
JButton b2 = new JButton("Button 3");
Font newFont = new Font("SERIF", Font.BOLD, 16);
b1.setFont(newFont);
b1.addActionListener(this);
JLabel l = new JLabel("Hello.");
p.add(b);
p.add(b1);
p.add(b2);
p1.add(l);
c.add(p);
c.add(p1);
c.setLayout(new FlowLayout());
setSize(300, 300);
show();
}
public static void actionPerformed (ActionEvent e) {
if(e.getSource().equals(b))
{
this.l.setText("Testing");
}
}
public static void main(String[] args) {
myAction output = new myAction();
}
}
How do I make my JButton b1 change the value of my JLabel l ? I am fairly new to programming so I'm sorry for any mistakes that you guys notice! I just need it to change whenever I click the button, I thought I had it right but my symbols can't be found and I'm pretty sure I'm not supposed to pass them into the method :S
Well, you didn't even added ActionListener for your button and make actionPerformed method as non-static (just remove static). This solves a problem:
b.addActionListener(this);
Also, I recommend to use anonymous inner classes instead of implementing ActionListener directly to your class. Like this:
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//Do stuff
}
});
Also, make your JButton's vars. as instance variables (move them outside of constructor).
b is not visible to your actionPerformed method because it is defined in the constructor. You need to move the variable b outside of myAction().
class myAction extends JFrame implements ActionListener {
JButton b;
myAction() {
b = new JButton("Click");

close window on button click

Hello,
I am using Java Swing and I want to close a window on a button click. I don't know using an actionlistener as the best way to do this but currently I am having compilation errors with it so its must be incorrect.
Here my code:
public class assignment2
{
public static void main(String[] args){
MyFrame f = new MyFrame(); //open the inital gui interface
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); //set it visibile
}
}
//this is the initial gui screen, presenting user with options for which action they would like to take
//al actions for the gui are commenced here
class MyFrame extends JFrame{
public MyFrame(){
buttonPanel1 p = new buttonPanel1(); // add the buttons for this frame
add(p);
setSize(800,600);
setTitle("Travel Console");
setLocationRelativeTo(null);
}
}
class buttonPanel1 extends JPanel{
public buttonPanel1(){
//create buttons
JButton addItem = new JButton("Add an Item");
JButton deleteItem = new JButton("Delete an item");
JButton listItem = new JButton("List items");
JButton editItem = new JButton("Edit an item");
JButton bookFlight = new JButton("Book a flight");
JButton save = new JButton("Save data");
JButton load = new JButton("Load data");
JButton exit = new JButton("Exit");
//set layout manager for button panel
setLayout(new GridLayout(8,1,1,5));
//create buttons
add(addItem);
add(deleteItem);
add(listItem);
add(editItem);
add(bookFlight);
add(load);
add(save);
add(exit);
addItemListener addList = new addItemListener();
addItem.addActionListener(addList);
exitListener exitList = new exitListener();
exit.addActionListener(exitList);
}
}
//listener classes for the inital gui page. each button has its own actionlistener which launches the selected option
class addItemListener implements ActionListener{
public void actionPerformed(ActionEvent event){ //launch add item
addItemFrame addItem = new addItemFrame();
addItem.setDefaultCloseOperation(addItemFrame.DISPOSE_ON_CLOSE);
addItem.setVisible(true);
}
}
class addItemFrame extends JFrame{
public addItemFrame(){
addItemButtonPanel b = new addItemButtonPanel(); // add the buttons for this frame
add(b);
setSize(800,500);
setTitle("Add an Item");
setLocationRelativeTo(null);
}
}
//part of addItemFrame class
class addItemButtonPanel extends JPanel{
public addItemButtonPanel(){
JLabel selectItem = new JLabel("Select which item you would like to add:");
JButton newCustomer = new JButton("Customer");
JButton newflight = new JButton("Flight");
JButton newMovie = new JButton("Movie");
JButton goBack = new JButton("Return to main menu");
setLayout(new GridLayout(5,1,1,5));
add(selectItem);
add(newCustomer);
add(newflight);
add(newMovie);
add(goBack);
goBackListener gbList = new goBackListener();
goBack.addActionListener(gbList);
}
}
//listener classes for the addItemFrame
class goBackListener implements ActionListener{
public void actionPerformed(ActionEvent event){
addItemFrame.dispose();
}
}
The problem I am having is with the last class listed goBackListener, which effectively just closes the current window so the main menu screen is displayed again. I need a static reference to addItemFrame created in the addItemListener class. But changing it to static is an invalid modifier?
How can I fix it?
try this
//listener classes for the addItemFrame
class goBackListener implements ActionListener{
private addItemFrame frame;
public goBackListener(addItemFrame frame){
this.frame= frame;
}
public void actionPerformed(ActionEvent event){
frame.dispose();
}
}
and send an instance of addItemFrame to it's constructor

Categories