Refresh JFrame? Java Swing - java

I don't know how to resolve this case:
I have a JFrame with JPanel on it. I added two JButtons to this JPanel.
Class MainFrame
import java.awt.Color;
import javax.swing.JFrame;
public class MainFrame extends JFrame{
public MainFrame(){
this.setSize(100,100);
MainPanel panel = new MainPanel();
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
and MainPanel with two buttons
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements ActionListener{
JButton button, example;
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New");
button.addActionListener(this);
JButton example = new JButton("example");
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}
}
and start class Main
public class Main {
public static void main (String[] args){
MainFrame frame = new MainFrame();
}
}
What should I do to change background color second button?

You have your button variables defined twice, once as an instance variable and once as a local variable.
Get rid of the local variable:
//JButton example = new JButton("example");
example = new JButton("example");
Now your ActionListener code can reference the instance variable.

In your example:
JButton button, example; // <-- Here, you create your two (protected) variables
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New"); // <-- And here, you create a local variable
button.addActionListener(this);
JButton example = new JButton("example"); // <-- Here, another local variable
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}

Related

The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (JPanel)

import java.awt.BorderLayout;
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;
public class Main implements ActionListener{
JButton button;
public static void main(String[] args) {
JButton button = new JButton();
JLabel label = new JLabel();
label.setText("");
label.setHorizontalAlignment(JLabel.CENTER);
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
redPanel.setBounds(0,250,250,250);
redPanel.setLayout(new BorderLayout());
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
bluePanel.setBounds(250,250,250,250);
bluePanel.setLayout(new BorderLayout());
JPanel greenPanel = new JPanel();
greenPanel.setBackground(Color.green);
greenPanel.setBounds(0,0,500,250);
greenPanel.setLayout(new BorderLayout());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(750, 750);
frame.setVisible(true);
redPanel.add(label);
redPanel.add(button);
frame.add(redPanel);
frame.add(bluePanel);
frame.add(greenPanel);
button.setBounds(0,250,250,250);
button.addActionListener(redPanel);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
System.out.print("ahahah");
}
}
}
I wanted to add a fully functioning button inside the main class
i had the error:The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (JPanel)
(I tried putting JPanel as an argument)
button.addActionListener(redPanel);
You cannot register a java.awt.Component (which JPanel is) as an ActionListener of JButton. Instead you should use one of the appropriate listeners listed here, to register one onto the JPanel: https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html
You should do:
button.addActionListener(new Main());
but it would be better if you create a new class for the ActionListener and implement the interface there, and then in the main method use the constructor of your own listener.
And always read the documentation ;)

Jpanel textbox no output

I'm working in java using Jpanel and my work is compiling fine however is showing no output. hopefully, someone could tell me why this is. I'm using jscrollpane and I'm calling it at the end idk if it's something to do with the listener or what.
FileDemoPanel.java
package Tutoiral03Task01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton openBtn, saveBtn;
JTextArea workTa;
openBtn = new JButton ("Open");
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn = new JButton ("Save");
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
add(logScrollPane);
}
}
FileDemo.java
package Tutoiral03Task01;
import javax.swing.*;
public class FileDemo {
public static void main (String[] args){
JFrame frame = new JFrame("Working with files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FileDemoPanel());
frame.pack();
frame.setVisible(true);
}
}
The problem is that you create all the buttons and others in a actionperformed method.
This is wrong, because that is used as a ButtonListener, so if you dont press a button nothing will happened. We use to write the GUI frame in the constructor of the class.Then we create an object type of the GUI class. So i think i fixed it and i did some extra changes to make the program more simple. The step i didnt do is to add a ButtonListener, so Buttons do nothing.
i wish it will helps you.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JFrame {
private JPanel panel = new JPanel();
private JButton openBtn = new JButton("Open");
private JButton saveBtn = new JButton ("Save");
private JTextArea workTa;
public FileDemoPanel(){
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
panel.add(openBtn);
panel.add(saveBtn);
panel.add(logTA);
panel.add(logScrollPane);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(true);
this.setSize(350,150);
this.setTitle("Κεντρική Σελίδα");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And the mainclass.As you can see is too small.
public class FileDemo {
public static void main (String[] args){
new FileDemoPanel();
}
}

Swing Card Layout change panel displayed on action by user

I'm currently writing an interface where a have a JFrame class and two JPanel classes. When the script is first executed, Panel A is shown. I have a JButton in Panel A which I would like, when clicked, to display Panel B instead of Panel A.
Is there any way I could do this?
Read tutorial for that.
You can use next() method of CardLayout for showing next card,
or you can use show(...); for showing specific card.
Simple example:
import java.awt.BorderLayout;
import java.awt.CardLayout;
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 Example {
public static void main(String[] args){
JFrame frame = new JFrame();
final JPanel panel = new JPanel(new CardLayout());
JLabel l1 = new JLabel("1");
JLabel l2 = new JLabel("2");
JLabel l3 = new JLabel("3");
panel.add(l1,"l1");
panel.add(l2,"l2");
panel.add(l3,"l3");
JButton btn = new JButton("next");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.next(panel);
}
});
JButton btnSpec = new JButton("l3");
btnSpec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.show(panel, "l3");
}
});
frame.add(panel);
frame.add(btn,BorderLayout.SOUTH);
frame.add(btnSpec,BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

How to add JPanel to JFrame from different classes

How do i add the JPanel to JFrame? It is really confusing me. I want to add the JPanel to the JFrame. I've tried all sorts of things including the extend but I cant get it to work.
events
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class events {
public static void main (String args[]) {
Time timeObject = new Time();
JFrame mainJFrame;
mainJFrame = new JFrame();
mainJFrame.setLayout(BorderLayout());
mainJFrame.setVisible(true);
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainJFrame.setSize(600,400);
mainJFrame.setVisible(true);
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setTitle("Travel Agent System");
mainJFrame.setBackground(Color.BLUE);
timeObject.selectButton();
}
}
Time
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Time{
public static void selectButton()
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton,moneyButtons,hotelButton,exitButton);
}
Have a look over this source. Note the comments.
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class events {
public static void main (String args[]) {
Time timeObject = new Time();
JFrame mainJFrame;
mainJFrame = new JFrame();
// Coding by magic!
//mainJFrame.setLayout(BorderLayout());
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// don't do this, just call pack() later
//mainJFrame.setSize(600,400);
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setTitle("Travel Agent System");
mainJFrame.setBackground(Color.BLUE);
timeObject.selectButton();
mainJFrame.add(timeObject.getGUI());
mainJFrame.pack();
// should be last.
mainJFrame.setVisible(true);
}
}
class Time {
private JPanel buttonPanel;
// don't use static unless necessary - it is not necessary.
//public static void selectButton() {
public void selectButton() {
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton);
buttonPanel.add(moneyButton);
buttonPanel.add(hotelButton);
buttonPanel.add(exitButton);
}
public JComponent getGUI() {
return buttonPanel;
}
}
Pass JFrame object to selectButton() :
timeObject.selectButton(mainJFrame);
Then use that JFrame object to add JPanel to it.
public static void selectButton(JFrame frame)
{
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton,moneyButtons,hotelButton,exitButton);
frame.getContentPane().add(buttonPanel,BorderLayout.CENTER); // i've added to CENTER.
}
This will add JPanel to CENTER of your JFrame.
Here is a good example of implementing a JFrame class and then adding JPanels to the JFrame and to other JPanels.
Link to another StackOverflow question/answer

Removing panel from JTabbedPane via button on panel

I have 2 classes, mainFrame and panel. By clicking the button on mainFrame I call panel from another class and set it in tabbed pane which is in JFrame (mainFrame class). Now, I have another button (btnRemove) on my panel in panel class. So when I click that button I want to remove my panel from tabbed pane in mainFrame class. How do I write my listener properly?
mainFrame class:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MainFrame extends JFrame {
JTabbedPane tPane = new JTabbedPane();
JButton btn = new JButton("Add panel");
public MainFrame(){
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
add(tPane, BorderLayout.CENTER);
add(btn,BorderLayout.NORTH);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel p = new panel();
tPane.add("Panel",p);
}
});
}
public static void main(String[] args){
new MainFrame();
}
}
panel class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Panel extends JPanel{
JButton btnRemove = new JButton("Remove panel");
public Panel(){
setLayout(new FlowLayout());
add(btnRemove);
btnRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
}
}
panel.this.getParent().remove(panel.this);
If you want the code to keep working even if you nest the button inside a sub-panel, you should use the follwoing:
SwingUtilities.getAncestorOfClass(JTabbedPane.class, panel.this).remove(panel.this);
Side note: please respect Java naming conventions: classes start with upper-case letters.

Categories

Resources