Why does my JPanel not show in JFrame after button is clicked.
There's my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.AbstractListModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
public class Main {
private JFrame frame;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{"1", "Marcin Zelek", "537573656"},
{"2", "Krzysztof Tomala", "324159103"},
{"3", "Zbigniew S", "324159104"},
},
new String[] {
"#", "Name", "Phone number"
}
));
table.getColumnModel().getColumn(1).setPreferredWidth(214);
table.getColumnModel().getColumn(2).setPreferredWidth(246);
table.setBounds(12, 103, 426, 185);
frame.getContentPane().add(table);
JButton btnDodajNowy = new JButton("Dodaj nowy");
btnDodajNowy.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
JPanel panel = new JPanel(null);// Creating the JPanel
panel.setBounds(0, 243, 286, 150);
panel.setVisible(true);
JButton button = new JButton("New button");
button.setBounds(12, 12, 117, 25);
panel.add(button);
frame.getContentPane().add(panel);
}
});
btnDodajNowy.setBounds(12, 30, 117, 25);
frame.getContentPane().add(btnDodajNowy);
JButton btnUsuZaznaczone = new JButton("UsuĊ zaznaczone");
btnUsuZaznaczone.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
int[] selection = table.getSelectedRows();
for (int i = 0; i < selection.length; i++)
{
model.removeRow(selection[i]-i);
}
}
});
btnUsuZaznaczone.setBounds(141, 30, 204, 25);
frame.getContentPane().add(btnUsuZaznaczone);
}
}
Thanks.
You should use actionListener instead:
btnDodajNowy.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();// Creating the JPanel
panel.setBounds(0, 243, 286, 150);
JButton button = new JButton("New button");
button.setBounds(12, 12, 117, 25);
panel.add(button);
frame.getContentPane().add(panel);
frame.repaint();
}
});
and also:
You don't need to pass null as a parameter to the JPanel constructor
You might want to add a frame.repaint()
You don't need to set the JPanel to visible via setVisible().
If you looking to present the panel in same jframe where you added the button you probably should use CardLayout here is the link http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
It is being added, however, it is hiding below the JTable. The quickest fix is this:
public void mouseReleased(MouseEvent arg0) {
//...
panel.setBounds(0, 300, 286, 150); // 300 is on the y axis
//...
// then render the frame again:
frame.revalidate();
frame.repaint();
}
However, I suggest to rewrite it a little:
this.generalPanel = new JPanel();
frame.getContentPane().add(generalPanel);
...
generalPanel.add(table);
This way you'll get a good Layout for the topmost container.
Related
Every time I try to do this, it gives me an error that "Change Listener cannot be converted to Action Listener" and even if I implement ActionListener to the class... it still gives me another error
Is there a way to create a JButton only on the pane "Encryption" that when pressed prints "Hello"
This is my code:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SwingDemo extends JFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("Encryption/Decryption Software");
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1, panel2;
panel1 = new JPanel();
panel2 = new JPanel();
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
tabbedPane.addTab("Encryption", panel1);
tabbedPane.addTab("Decryption ", panel2);
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200,170, 500,250);
frame.setVisible(true);
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(tabbedPane.getSelectedIndex() == 0){
panel1.removeAll();
panel1.setLayout(null);
JLabel initial_text = new JLabel("Enter text to be encrypted:");
JLabel final_text = new JLabel("Final text:");
JLabel key = new JLabel("Key:");
JTextField text_field = new JTextField(100);
JTextField key_field = new JTextField(100);
panel1.add(initial_text);
panel1.add(final_text);
panel1.add(key);
panel1.add(text_field);
panel1.add(key_field);
initial_text.setBounds(10, 20, 300, 50);
final_text.setBounds(10, 150, 600, 50);
key.setBounds(10, 58, 300, 50);
text_field.setBounds(178, 30, 230, 30);
key_field.setBounds(38, 72, 36, 25);
}
}
});
}
}
to detect a click you need to add an MouseListener no a ChangeListener
Like this
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class SwingDemo extends JFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("Encryption/Decryption Software");
JFrame frame = new JFrame("Encryption/Decryption Software");
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1, panel2;
panel1 = new JPanel();
panel2 = new JPanel();
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
tabbedPane.addTab("Encryption", panel1);
tabbedPane.addTab("Decryption ", panel2);
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 170, 500, 250);
frame.setVisible(true);
tabbedPane.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Hello");
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
});
}
I am trying to code a program with multiple screens, however, I do not want to use tabbed panes. I have looked at using multiple JPanels with the card layout and the methods are simply not working. What I need to be able to do is load the new JPanel when a button is clicked. Here is my code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class IA extends JFrame {
private JPanel contentPane;
private JPanel home;
private JPanel clients;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
IA frame = new IA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public IA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
JPanel home = new JPanel();
contentPane.add(home, "name_714429679706141");
home.setLayout(null);
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
home.setVisible(false);
clients.setVisible(true);
}
});
btnClients.setBounds(160, 108, 89, 23);
home.add(btnClients);
JPanel clients = new JPanel();
contentPane.add(clients, "name_714431450350356");
clients.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clients.setVisible(false);
home.setVisible(true);
}
});
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);
}
}
The problem is that you have duplicate variables home and clients .
The folllowing is your modified code to fix that, with comments on the changed lines (five lines total) :
import java.awt.CardLayout;
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;
import javax.swing.border.EmptyBorder;
public class IA extends JFrame {
private final JPanel contentPane;
// private final JPanel home; // REMOVED
// private JPanel clients; // REMOVED
/**
* Launch the application.
*/
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
IA frame = new IA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public IA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
final JPanel home = new JPanel();
contentPane.add(home, "name_714429679706141");
home.setLayout(null);
final JPanel clients = new JPanel(); // MOVED UP
contentPane.add(clients, "name_714431450350356"); // MOVED UP
clients.setLayout(null); // MOVED UP
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
home.setVisible(false);
clients.setVisible(true);
}
});
btnClients.setBounds(160, 108, 89, 23);
home.add(btnClients);
JButton btnHome = new JButton("Home");
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
clients.setVisible(false);
home.setVisible(true);
}
});
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);
}
}
I would take a look at this post, however I have a feeling you'll need to use a actionlistener to get this done...
Java Swing. Opening a new JPanel from a JButton and making the buttons pretty
I would of left this as a comment but apparently you need 50 rep for that...
This link might be more helpful.. How to open a new window by clicking a button
When the following code is invoked the clients variable equals to null.
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
home.setVisible(false);
clients.setVisible(true);
}
});
Write this:
JPanel clients = new JPanel();
contentPane.add(clients, "name_714431450350356");
clients.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);
before you add the Action Listener
how can i use a Jbutton in a class to trigger a panelSlider effect which is in a button actionPerformed in another class
Find the code here
public class programDisplay extends javax.swing.JPanel { /** * Creates new form programDisplay */
public programDisplay() {
initComponents();
}
here is an example for sliding effect of panel. you can take it as a reference and implement your requirement.
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.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SlidingPanel {
JPanel panel;
public void makeUI() {
panel = new JPanel();
panel.setBackground(Color.RED);
panel.setBounds(0, 0, 400, 400);
JButton button = new JButton("Click");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((JButton) e.getSource()).setEnabled(false);
new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setLocation(panel.getX() - 1, 0);
if (panel.getX() + panel.getWidth() == 0) {
((Timer) e.getSource()).stop();
System.out.println("Timer stopped");
}
}
}).start();
}
});
panel.add(button);
JFrame frame = new JFrame("Sliding Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SlidingPanel().makeUI();
}
});
}
}
UPDATE :
in HomePane.java
rightPane rPane = new rightPane();
JPanel lPane = new leftPane(rPane);
in leftPane.java
private rightPane rPane;
public leftPane(final rightPane rPane)
{
pane = new JPanel();
this.rPane = rPane;
staffLogin = new JButton("STAFF LOGIN");
adminLogin = new JButton("ADMIN LOGIN");
guestLogin = new JButton("GUEST LOGIN");
staffLogin.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
rPane.trigerEvent();
}
});
this.setLayout(null);
this.add(staffLogin);
this.add(adminLogin);
this.add(guestLogin);
staffLogin.setFont(new Font("Serif", Font.BOLD, 20));
adminLogin.setFont(new Font("Serif", Font.BOLD, 20));
guestLogin.setFont(new Font("Serif", Font.BOLD, 20));
staffLogin.setBounds(20, 10, 200, 50);
adminLogin.setBounds(20, 75, 200, 50);
guestLogin.setBounds(20, 140, 200, 50);
this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setBorder(BorderFactory.createLineBorder(Color.red));
}
I have a JTabbedPane with two panels inside: panel1 and panel2. I want to update my JComboBox which is located in panel2 when performing an action in panel1.
In panel1 I have this code:
JPanel panel1 = new JPanel();
tabbedPane.addTab("panel1", null, panel1, null);
panel1.setLayout(null);
//some code
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// ?
}
});
btnSubmit.setBounds(12, 155, 150, 25);
panel1.add(btnSubmit);
In panel2 is my JComboBox:
JPanel panel2 = new JPanel();
tabbedPane.addTab("panel2", null, panel2, null);
panel2.setLayout(null);
//some code
final JComboBox comboBox_2 = new JComboBox();
comboBox_2.setBounds(12, 240, 200, 24);
panel2.add(comboBox_2);
How can I do that?
Here's a quick and dirty implementation.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Window extends JFrame {
JTabbedPane jtp = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2= new JPanel();
JComboBox<String> comboBox_2 = new JComboBox<String>();
ActionListener buttonListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
comboBox_2.addItem("abc");
comboBox_2.addItem("def");
}
};
public Window() {
initP1();
initP2();
initJTP();
}
public void initJTP(){
jtp.add(panel1);
jtp.add(panel2);
this.add(jtp);
}
public void initP1(){
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(12, 155, 150, 25);
btnSubmit.addActionListener(buttonListener);
panel1.add(btnSubmit);
}
public void initP2(){
comboBox_2.setBounds(12, 240, 200, 24);
panel2.add(comboBox_2);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
Window frame = new Window();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I have inherited code and for reasons to long to explain I am required to use a null layout. I have been attempting to take what they have an navigate between JPanels. I haven't been able to figure out how. This is what I have now which compiles broken down into a SSCCE below. What I am attempting to do is add the JPanels to an ArrayList which contains a reference to previous JPanels. That way I can call a "home" JPanel from the current JPanel the user is in. As of now it goes to the previous JPanel but the contents are empty. Any help would be great, THANKS!
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Mainscreen extends JFrame {
public JPanel Home;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
ArrayList <JPanel> jpLayout = new ArrayList();
final Dataentrylog DEL = new Dataentrylog(this, jpLayout);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home=new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
Home.setVisible(true);
setContentPane(Home);
Home.setLayout(null);
JButton delLog = new JButton("Next JPanel");
delLog.setFont(new Font("Tahoma", Font.PLAIN, 14));
delLog.setForeground(new Color(0, 0, 0));
delLog.setBackground(UIManager.getColor("Menu.selectionBackground"));
Home.add(delLog);
jpLayout.add(Home);
delLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
getContentPane().setLayout(null);
}
});
delLog.setBounds(44, 214, 213, 61);
}
}
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent, final ArrayList <JPanel> jpLayout) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
final JButton btnSignIn = new JButton("Go Back");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.setContentPane(jpLayout.get(0));
setLayout(null);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
I am required to use a null layout.
If you are asked by some mentor to perform navigation among panels using null layout, here is what you need to do:
First, leave the mentor.
Second, use CardLayout.
Check out the tutorial: How to use CardLayout
Working Example:
here is a written CardLayoutDemo which navigates among 10 panels including Home panel using button click action:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CardLayoutDemo1 extends JFrame {
private JPanel jPanel1;
private JButton navHomeButt;
private JButton navNextButt;
private JButton navPreviousButt;
private JPanel panelContainer;
public CardLayoutDemo1() {
initComponents();
panelContainer.add(createSamplePanel("Home Panel "), ""+0);
for(int i=1; i < 10; i++)
{
panelContainer.add(createSamplePanel("Panel "+i), ""+i);
}
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
navPreviousButt = new JButton();
navNextButt = new JButton();
navHomeButt = new JButton();
panelContainer = new JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
navPreviousButt.setText("Previous");
navPreviousButt.setPreferredSize(new Dimension(90, 23));
jPanel1.add(navPreviousButt);
navNextButt.setText("next");
navNextButt.setPreferredSize(new Dimension(90, 23));
jPanel1.add(navNextButt);
navHomeButt.setText("Back to Home");
jPanel1.add(navHomeButt);
panelContainer.setPreferredSize(new Dimension(400, 300));
panelContainer.setLayout(new CardLayout());
// setting the card layout
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
getContentPane().add(panelContainer, BorderLayout.CENTER);
navNextButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
cardLayout.next(panelContainer);
// using cardLayout next() to go to next panel
}
});
navHomeButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
cardLayout.first(panelContainer);
// suing first to get to the home panel
}
});
navPreviousButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
cardLayout.previous(panelContainer);
// using previous to get to previous(left)panel
}
});
pack();
}
public JPanel createSamplePanel(String panelTitle)
{
JPanel samplePanel = new JPanel();
samplePanel.add(new JLabel(panelTitle));
return samplePanel;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CardLayoutDemo1().setVisible(true);
}
});
}
}