I'm kind of new to java and I experienced this little problem on every computer I run my program.
My code is
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest2 {
public static void main(String[] args){
String s = "Catch Torchic";
MainMenu m = new MainMenu(s);
}
}
class MainMenu extends JFrame {
JButton autoa, manualb, createc, exitd;
public MainMenu(String s){
super(s);
setBackground(new Color(247,247,111));
setSize(640,600);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
CustomPanel panel = new CustomPanel();
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setMinimumSize(new Dimension(600,365));
panel.setPreferredSize(new Dimension(600,365));
panel.setMaximumSize(new Dimension(600,365));
panel.setBackground(new Color(247,247,111));
contentPane.add(panel);
JPanel btnPanel = new JPanel();
btnPanel.setLayout( new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
btnPanel.setBackground(new Color(247,247,111));
autoa = new JButton("AutoPlay");
autoa.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(autoa);
manualb = new JButton("Manual Play");
manualb.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(manualb);
createc = new JButton("Create Maze");
createc.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(createc);
exitd = new JButton("Exit");
exitd.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(exitd);
btnPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
contentPane.add(btnPanel);
setContentPane(contentPane);
ButtonHandler handler = new ButtonHandler(this);
autoa.addActionListener(handler);
manualb.addActionListener(handler);
createc.addActionListener(handler);
exitd.addActionListener(handler);
}
}
class ButtonHandler implements ActionListener{
MainMenu mm;
public ButtonHandler(MainMenu mm){
this.mm = mm;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == mm.autoa)
System.out.println("Clicked on Auto");
else if(e.getSource() == mm.manualb)
System.out.println("Clicked on Manual");
else if(e.getSource() == mm.createc)
System.out.println("Clicked on Create");
else
System.exit(0);
}
}
class CustomPanel extends JPanel{
public void paintComponent (Graphics painter){
Image pic = Toolkit.getDefaultToolkit().getImage("logo.png");
if(pic != null) painter.drawImage(pic, 105, 30, this);
}
}
This code results into this: http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash4/318155_424290814251286_100000111130260_1871937_131988334_n.jpg
where there is always the extra button on the upper left corner, My question is, how do I remove this? the button appears at random intervals, sometimes it does not.
Try this variant. Many comments, the most important in SHOUTING. Note that by the time I had got it to being working code (or rather, code that worked here for loading the image), I never once saw the problem you describe.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.URL;
import javax.imageio.ImageIO;
public class FrameTest2 {
public static void main(String[] args) throws Exception {
final String s = "Catch Torchic";
URL url = new URL("http://pscode.org/media/stromlo2.jpg");
final Image image = ImageIO.read(url);
//Create the frame on the event dispatching thread
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
new MainMenu(s, image);
}
});
}
}
class MainMenu extends JFrame {
JButton autoa, manualb, createc, exitd;
public MainMenu(String s, Image image) {
super(s);
// do this after pack (if at all)
//setSize(640,600);
// do this after pack/setSize
//setVisible(true);
// don't do this in broken code
//setResizable(false);
// If you're going to spend space on GUI position..
// setLocationRelativeTo(null);
// ..do it like this.
setLocationByPlatform(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
CustomPanel panel = new CustomPanel(image);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
//panel.setMinimumSize(new Dimension(600,365));
//panel.setPreferredSize(new Dimension(600,365));
//panel.setMaximumSize(new Dimension(600,365));
contentPane.add(panel);
JPanel btnPanel = new JPanel();
btnPanel.setBorder(new EmptyBorder(5,5,100,5));
btnPanel.setLayout( new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
// not needed for an SSCCE
//btnPanel.setBackground(new Color(247,247,111));
autoa = new JButton("AutoPlay");
autoa.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(autoa);
manualb = new JButton("Manual Play");
manualb.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(manualb);
createc = new JButton("Create Maze");
createc.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(createc);
exitd = new JButton("Exit");
exitd.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPanel.add(exitd);
// you already said that
//btnPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
contentPane.add(btnPanel, BorderLayout.SOUTH);
setContentPane(contentPane);
ButtonHandler handler = new ButtonHandler(this);
autoa.addActionListener(handler);
manualb.addActionListener(handler);
createc.addActionListener(handler);
exitd.addActionListener(handler);
// CAUSE THE COMPONENTS TO BE PROPERLY LAID OUT.
pack();
setSize(640,600);
setVisible(true);
}
}
class ButtonHandler implements ActionListener{
MainMenu mm;
public ButtonHandler(MainMenu mm){
this.mm = mm;
}
public void actionPerformed(ActionEvent e){
// for clarity, even single line statements
// in these should be enclosed in {}
if(e.getSource() == mm.autoa)
System.out.println("Clicked on Auto");
else if(e.getSource() == mm.manualb)
System.out.println("Clicked on Manual");
else if(e.getSource() == mm.createc)
System.out.println("Clicked on Create");
else
System.exit(0);
}
}
class CustomPanel extends JPanel{
Image pic;
CustomPanel(Image pic) {
this.pic = pic;
}
public void paintComponent (Graphics painter){
// DO NOT TRY TO READ IMAGES IN PAINT!
//Image pic = Toolkit.getDefaultToolkit().getImage("logo.png");
if(pic != null) painter.drawImage(pic, 105, 30, this);
}
}
I think it is the missing contentPane.:
//setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));//
Also there is a superfluous setting of the content pane.
//setContentPane(contentPane);
After "nope:"
Difficult.
Move setVisible(true) to the end, to invoke only one layouting.
Try it without two panels (only one) inside the content pane:
Container contentPane0 = getContentPane();
Container contentPane = new JPanel();
contentPane0.add(contentPane);
Related
I'm having a problem trying to change JPanels by using buttons. I have a JFrame with 2 panels, 1 of them is for the buttons, which i want them to always be showed. The other one is the one that i will be switching everytime i press one ot the buttons of the other panel. The problem is that everytime i press them nothing really ever displays, i keep my buttons but the other panel that i call does not appear.
Code for one of the buttons is as follows
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ReparacaoPanel r = new ReparacaoPanel(this, this.jPanel1);
this.getContentPane().remove(this.jPanel1);
this.getContentPane().add(r);
//this.setContentPane(r);
this.visiblePanel.setVisible(false);
this.visiblePanel = r;
this.pack();
this.setVisible(true);
r.setLocation(200, 200);
this.getContentPane().revalidate();
this.repaint();
}
If i try to use "this.setContentPane(r);" (it sets the frame to only show the panel) the panel shows. But when i try to call it as i'm trying to do in the code above nothing is showed apart from the panel that has the buttons.
I have no idea what i'm doing wrong, it does not seem to be a problem with the JPanel that i'm trying to call as it shows if used alone.
Anyone can help me out?
Consider this working example for switching manually between panels. Which produces this output.
.........
Some tiny NumberPanel
Every new instance shows another number in the center.
import javax.swing.JPanel;
public class NumberPanel extends JPanel {
private static int counter = 0;
public NumberPanel() {
setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = new JLabel("" + counter++);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(lblNewLabel);
}
}
Setting up a frame
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.getContentPane().remove(numberPanel);
numberPanel = new NumberPanel();
frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
frame.pack();
}
});
panel.add(btnNewButton);
numberPanel = new NumberPanel();
frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
frame.pack();
}
Testprogram
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestPanelSwitch {
private JFrame frame;
private NumberPanel numberPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestPanelSwitch window = new TestPanelSwitch();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestPanelSwitch() {
initialize();
}
private void initialize() {
// see above
}
}
Back to the Question
I think you only need to pack your frame, like in the anonymous ActionListener.
frame.getContentPane().remove(numberPanel);
numberPanel = new NumberPanel();
frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
frame.pack();
EDIT
As leonidas mentioned it is also possible to revalidate the frame. This requires only to replace the upper call to pack by theese.
frame.invalidate();
frame.validate();
this is the first time I've had a look at JFrames and JPannels and I've come a little stuck.
What I am trying to do is this, I wish to have an starting screen then based on the users button choice it swaps to another screen. To start I have only 2 screens, however once I've moved on there will be multiple screens. I've looked at CardLayout and while that is good it's not the way I wish to go I want to be able to do this first. Here is what I have.
Main.java
import java.awt.BorderLayout;
public class Main extends JFrame {
private JPanel contentPane;
protected boolean someCondition = false;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
if( someCondition == false ){
showTest();
someCondition = test.needToReg();
}else{
showTest2();
}
}
private void showTest(){
contentPane.removeAll();
contentPane.add(new test());
setContentPane(contentPane);
revalidate();
repaint();
}
private void showTest2(){
contentPane.removeAll();
contentPane.add(new test2());
setContentPane(contentPane);
revalidate();
repaint();
}
}
test.java
import javax.swing.JPanel;
public class test extends JPanel {
private JTextField textField;
protected static boolean toReg = false;
/**
* Create the panel.
*/
public test() {
setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked");
System.out.println("Before " + toReg);
toReg = true;
System.out.println("After " + toReg);
}
});
btnNewButton.setBounds(188, 166, 89, 23);
add(btnNewButton);
textField = new JTextField();
textField.setBounds(150, 135, 86, 20);
add(textField);
textField.setColumns(10);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
rdbtnNewRadioButton.setBounds(6, 166, 109, 23);
add(rdbtnNewRadioButton);
}
public static boolean needToReg(){
return toReg;
}
}
test2.java
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
public class test2 extends JPanel {
/**
* Create the panel.
*/
public test2() {
setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(56, 59, 89, 23);
add(btnNewButton);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(122, 165, 46, 14);
add(lblNewLabel);
}
}
Running the program with the outputs I included I get this.
Mouse Clicked
Before false
After true
Mouse Clicked
Before true
After true
Mouse Clicked
Before true
After true
Mouse Clicked
Before true
After true
Mouse Clicked
Before true
After true
I hope it's clear what I am trying to do and I hope you can lend a hand with this. Thanks
Try this out
On clicking the screenSwapper button in the main frame a new Panel is added to the main frame that can have multiple components I have added one button only
On second click this panel is removed and second panel is added to the main frame and previous one is removed.
The swapping is carried as you click the button continuously
You may use two singletons if you want to preserve once created panel in case of MyPanel1 and MyPanel2
You may add more components on each panel and test.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
public boolean switcher;
public JPanel currentPanel;
public JPanel panel1;
public JPanel panel2;
public Test() {
this.switcher = false;
this.currentPanel = null;
this.setSize(200, 200);
panel1 = new JPanel();
JButton screenSwapper = new JButton("Screen Swapper");
panel1.add(screenSwapper);
panel2 = new JPanel();
this.getContentPane().setLayout(new GridLayout(2, 2));
this.getContentPane().add(panel1);
this.getContentPane().add(panel2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screenSwapper.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if (switcher == false) {
currentPanel = new MyPanel1();
switcher = true;
if (panel2.getComponentCount() != 0) {
panel2.removeAll();
}
} else {
switcher = false;
currentPanel = new MyPanel2();
if (panel2.getComponentCount() != 0) {
panel2.removeAll();
}
}
panel2.add(currentPanel);
panel2.repaint();
panel2.revalidate();
}
});
}
public static void main(String[] args) {
Test t = new Test();
}
}
This is the first panel
import java.awt.BorderLayout;
import java.awt.Button;
import javax.swing.JPanel;
public class MyPanel1 extends JPanel{
public MyPanel1() {
// TODO Auto-generated constructor stub
this.setLayout(new BorderLayout());
this.add(new Button("Button1"));
}
}
This is the second Panel
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MyPanel2 extends JPanel {
public MyPanel2() {
this.setLayout(new BorderLayout());
this.add(new JButton("button2"));
}
}
I have a problem with setting position for one of the elements (the image). I cannot align it to right-bottom of the screen. I tried to use different layouts but I can't make it to work exactly like I want it.
Below is the url to view how it looks now. The image is in the right-bottom but it's a new BorderLayout and it creates extra space on the bottom so I would prefer it to fit with the rest. Red square shows where image should be placed.
Below is the code of my program:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame implements ActionListener{
public static final int PORT = 8060;
private JButton reset, exit;
private JEditorPane messages;
private ImageIcon logo = new ImageIcon("C:src\\images\\logo.png");
private JLabel logoSpot;
Container box = getContentPane();
public static void main(String args[]){
new Server().Networking();
}
public void makeMenu(){
JPanel menu = new JPanel();
reset = new JButton("Reset messages");
exit = new JButton("Exit");
menu.add(reset);
menu.add(exit);
reset.addActionListener(this);
exit.addActionListener(this);
box.add(menu, BorderLayout.EAST);
}
public void setLogo(){
JPanel logoSpace = new JPanel(new BorderLayout());
logoSpot= new JLabel();
logoSpot.setIcon(logo);
logoSpace.add(logoSpot, BorderLayout.EAST);
box.add(logoSpace, BorderLayout.SOUTH);
}
public void makeScreen(){
JPanel screen = new JPanel();
messages = new JEditorPane();
messages.setPreferredSize(new Dimension(800,590));
screen.add(messages);
box.add(screen, BorderLayout.WEST);
}
public Server(){
makeMenu();
makeScreen();
setLogo();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(1024, 680);
setTitle("Emergency system");
}
#Override
public void actionPerformed(ActionEvent e){
if(e.getSource() == exit){
super.dispose();
}
if(e.getSource() == reset){
messages.setText(" ");
}
}
public void Networking(){
String received=" ";
try{
ServerSocket ss = new ServerSocket(PORT);
while(true){
Socket sock = ss.accept();
BufferedReader in =
new BufferedReader(new InputStreamReader(
sock.getInputStream()));
received = in.readLine();
messages.setText(messages.getText() + received +
" \n\n");
OutputStreamWriter out =
new OutputStreamWriter(sock.getOutputStream());
BufferedWriter bw = new BufferedWriter(out);
bw.write(received);
bw.flush();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
This should do the trick. It just needed some more panels to constrain the 2nd image (160x160px) to the PAGE_END and LINE_END of some BorderLayout instances.
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame implements ActionListener{
public static final int PORT = 8060;
private JButton reset, exit;
private JEditorPane messages;
private ImageIcon logo = new ImageIcon(
new BufferedImage(140,140,BufferedImage.TYPE_INT_RGB));
private JLabel logoSpot;
Container box = getContentPane();
public static void main(String args[]){
new Server();
}
public void makeMenu(){
JPanel menu = new JPanel(new BorderLayout(5,5));
menu.setBackground(Color.RED);
reset = new JButton("Reset messages");
exit = new JButton("Exit");
JPanel buttons = new JPanel();
buttons.setBackground(Color.GREEN);
menu.add(buttons, BorderLayout.PAGE_START);
buttons.add(reset);
buttons.add(exit);
reset.addActionListener(this);
exit.addActionListener(this);
JLabel l = new JLabel(new ImageIcon(new BufferedImage(
160,160,BufferedImage.TYPE_INT_RGB)));
JPanel forceRight = new JPanel(new BorderLayout());
forceRight.add(l, BorderLayout.LINE_END);
forceRight.setBackground(Color.BLUE);
menu.add(forceRight, BorderLayout.PAGE_END);
box.add(menu, BorderLayout.EAST);
}
public void setLogo(){
JPanel logoSpace = new JPanel(new BorderLayout());
logoSpot= new JLabel();
logoSpot.setIcon(logo);
logoSpace.add(logoSpot, BorderLayout.EAST);
box.add(logoSpace, BorderLayout.SOUTH);
}
public void makeScreen(){
JPanel screen = new JPanel();
screen.setBackground(Color.YELLOW);
messages = new JEditorPane();
messages.setPreferredSize(new Dimension(800,590));
screen.add(messages);
box.add(screen, BorderLayout.WEST);
}
public Server(){
makeMenu();
makeScreen();
setLogo();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(1024, 680);
setTitle("Emergency system");
}
#Override
public void actionPerformed(ActionEvent e){
if(e.getSource() == exit){
super.dispose();
}
if(e.getSource() == reset){
messages.setText(" ");
}
}
}
You should organize the structure of your code better. The constructor is typically found at the start of the class.
Instead of having 3 methods to create the GUI components you could probably have one. You don't need to use the getContentPane() method. When you use the add(...) method of a JFrame the components are added to the content pane. I would also use a JTextArea to display message. a JEdtitorPane should only be used for HTML. You also should add the text area to a JScrollPane so scrollbars will appear as more messages are added.
So the basic code would be something like:
JTextArea textArea = new JTextArea(20, 50);
JScrollPane scrollPane = new JScrollPane( textArea );
add(scrollPane, BorderLayout.CENTER);
JPanel east = new JPanel( new BorderLayout() );
east.add(menuPanel, BorderLayout.NORTH);
east.add(logo, BorderLayout.SOUTH);
add(east, BorderLayout.EAST);
I'm writing a program in Java where I'm using JTabbedPane. Each tab is associated with a different panel with labels, textfields and a button. I have used GridBagLayout in the panels.
I have added an actionlistener to the button, but when I click it nothing happens.
EDIT: I also have other buttons outside the JTabbedPane which works perfectly fine.
I can see that nothing is happening because I do this:
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == button ) {
System.out.println("blablabla");
}
and nothing is printed out.
Is there any common problems with using buttons and GridBagLayout/JTabbedPane?
EDIT with SSCCE
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Hjelp extends JFrame {
private FlowLayout layout;
private JButton button1;
private JButton button2;
private JPanel menu, frontpage;
private JPanel present, previous, something;
public Hjelp() {
layout = new FlowLayout(FlowLayout.CENTER, 10, 20);
setLayout(layout);
setSize(900, 900);
setLocationRelativeTo(null);
setVisible(true);
setPanels();
something = something();
add(something, BorderLayout.CENTER);
something.setVisible(false);
button1 = new JButton("CLICK ME");
add(button1);
buttonListener();
}
private void buttonListener() {
Buttonlistener listener = new Buttonlistener();
button1.addActionListener(listener);
button2.addActionListener(listener);
}
private void setPanels() {
menu = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
frontpage = new JPanel();
previous = frontpage;
present = frontpage;
add(menu);
}
public void visiblePanel() {
previous.setVisible(false);
present.setVisible(true);
}
private JPanel something() {
visiblePanel();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
JComponent panel1 = tab();
tabbedPane.addTab("Click me", panel1);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
panel.add(tabbedPane);
return panel;
}
private JComponent tab() {
JPanel panel = new JPanel(false);
panel.setPreferredSize(new Dimension(870, 300));
panel.setLayout(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();
cs.fill = GridBagConstraints.HORIZONTAL;
button2 = new JButton("Click me");
cs.gridx = 1;
cs.gridy = 6;
cs.gridwidth = 1;
panel.add(button2, cs);
return panel;
}
private class Buttonlistener implements ActionListener {
#Override
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == button1 ) {
present = something;
button1.setVisible(false);
something();
previous = something;
}
else if (e.getSource() == button2) {
System.out.println("Blablabla");
}
}
}
public static void main(String [] args) {
final Hjelp vindu = new Hjelp();
vindu.addWindowListener(
new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
}
}
SOLVED
Solution
You don't need the getSource check at all—your listener is (hopefully) attached to just one button, so if it was invoked, that already means the button was clicked. Remove the check and unconditionally print your string. If you still don't see anything, then you have a problem.
You may not have attached a handler to the actual button, and therefore the event will never get called.
Part 1:
ButtonHandler handler = new ButtonHandler();
button.addActionListener( handler );
Part 2:
public class ButtonHandler implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event) {
}
}
ALSO: Java GUI can be finicky, rather than using "e.getSource() == button" you could try "button..isFocusOwner()"
I'm trying to create a small GUI, it has 2 JButtons, and 2 JPanels with some drawing animation on each of them. By default it must show first JPanel, and by clicking on second JButton I want to see my second JPanel. So : I create JFrame, Panel1 and Panel2, where I have drawn my animations, create Button1 and Button2 and adding to them ActionListeners. I have also MainPanel which has in a fields variable i. By changing this "i" my constructor adds to MainPanel either Panel1 (default) or Panel2 (by clicking on JButton2 I change i). Than I add this MainPanel to my frame. So my question : in the class MainPanel I have refreshMe method, what should I write there to make my GUI working properly? Thanks. Here is my code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GuiTest {
public static void main(String[] args) {
JFrame f = new JFrame();
MainPanel myPanel = new MainPanel();
f.add(myPanel);
Button1 button1 = new Button1();
Button2 button2 = new Button2();
myPanel.add(button1);
myPanel.add(button2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
class MainPanel extends JPanel {
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
public int i = 1; //this is being changed later by clicking JButton
// I use this setter later in actionPerformed in order to change i
public void setI(int i) {
this.i = i;
}
MainPanel() {
if (i == 1) {
this.add(p1);
}
if (i == 2) {
this.add(p2);
}
}
public void refreshMe() {
// Need some help here:
// I don't know what should I write, how to make a repaint of myPanel?
System.out.println("just test, if the method refreshMe working by clicking some button");
}
}
class Panel1 extends JPanel {
public Panel1() {
this.setBackground(Color.BLUE);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Panel2 extends JPanel {
public Panel2() {
this.setBackground(Color.GREEN);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Button1 extends JButton {
MainPanel someObj1 = new MainPanel();
Button1() {
setText("Show Annimation A");
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
someObj1.setI(1);
System.out.println("The variable i is now: " + someObj1.i);
someObj1.refreshMe();
}
});
}
}
class Button2 extends JButton {
MainPanel someObj2 = new MainPanel();
Button2() {
setText("Show Annimation B");
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
someObj2.setI(2);
System.out.println("The variable i is now: " + someObj2.i);
someObj2.refreshMe();
}
});
}
}
In order to reflect changes after adding/removing or resizing a component that is on a visible container call revalidate() and repaint() on the containers instance after adding/removing or resizing the component.
Though this will not work in your code the main reason being inside JButton classes you recreate a new instance of MainPanel when in fact the 2 JButtons should share the single instance which is being used (you could pass MainPanel instance to the JButtons constructors, but you shouldn't really be extending a JButton unless adding custom functionality):
class Button2 extends JButton {
MainPanel someObj2 = new MainPanel();//you create an instance of MainPanel which isnt even showing and than do changes on that, this way you will never see any of the changes
Button2() {
}
}
A few other suggestions on your code:
Dont extend JButton class unnecessarily, simply create an instance of JButton like you did with JFrame and call methods on JButton instance.
Dont forget to create/manipulate Swing components on Event Dispatch Thread, via SwingUtilities.invokeLater(..) block, read here for more.
Here is your code fixed (above suggestions ect implemented):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
final MainPanel myPanel = new MainPanel();
f.add(myPanel);
JButton button1 = new JButton("Show Animation A");
JButton button2 = new JButton("Show Animation B");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
myPanel.setI(1);
System.out.println("The variable i is now: " + myPanel.i);
myPanel.refreshMe();
}
});
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
myPanel.setI(2);
System.out.println("The variable i is now: " + myPanel.i);
myPanel.refreshMe();
}
});
myPanel.add(button1);
myPanel.add(button2);
myPanel.checkPanel();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
}
class MainPanel extends JPanel {
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
public int i = 1; //this is being changed later by clicking JButton
// I use this setter later in actionPerformed in order to change i
public void setI(int i) {
this.i = i;
}
public void refreshMe() {
checkPanel();
revalidate();
repaint();
// Need some help here:
// I don't know what should I write, how to make a repaint of myPanel?
System.out.println("just test, if the method refreshMe working by clicking some button");
}
public void checkPanel() {
if (i == 1) {
this.add(p1);
this.remove(p2);//or it will remain there as this is default flowlayout
} else if (i == 2) {
this.add(p2);
this.remove(p1);
}
}
}
class Panel1 extends JPanel {
public Panel1() {
this.setBackground(Color.BLUE);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Panel2 extends JPanel {
public Panel2() {
this.setBackground(Color.GREEN);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
However Id suggest something simpler, fortunately you have 2 choices:
1) Use CardLayout which will allow you to flip between multiple components on a single JFrame/container.
Here is an example I made:
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;
import javax.swing.SwingUtilities;
public class Test {
private final static String PANEL1 = "panel 1";
private final static String PANEL2 = "panel 2";
public Test() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test();
}
});
}
private void initComponents() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Panel 1"));
JPanel panel2 = new JPanel();
panel2.add(new JLabel("Panel 2"));
//Create the panel that contains the "cards".
final JPanel cards = new JPanel(new CardLayout());
cards.add(panel1, PANEL1);
cards.add(panel2, PANEL2);
//create button to allow chnage to next card
JButton buttonNext = new JButton(">");
buttonNext.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
CardLayout cl = (CardLayout) (cards.getLayout());//get cards
cl.next(cards);
}
});
//create button to allow chnage to previous card
JButton buttonPrev = new JButton("<");
buttonPrev.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
CardLayout cl = (CardLayout) (cards.getLayout());//get cards
cl.previous(cards);
}
});
//create panel to hold buttons which will allow switching between cards
JPanel buttonPanel = new JPanel();
buttonPanel.add(buttonPrev);
buttonPanel.add(buttonNext);
frame.add(cards);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
2) Use removeAll() technique i.e call frame.getContentPane().removeAll() which will remove all components currently on JFrame and than add the new content and call revalidate() and repaint() (also might want to add pack() in there) on JFrame instance to reflect changes. Though Id recommend CardLayout.
I think you can just use CardLayout to implement your function. Please refer to here