Basically I need to duplicate a JPanel, for example if we do it with Integer variables this should work:
Integer intaux,int1;
int1 = 3;
intaux = int1;
But this doesn't work with panels:
jPanelaux = jPanel1;
Is there any setter method I don't know?
As Kira San already said, you will need an instance for every panel you want to display.
For example:
public class MyPanel extends JPanel {
//creates a JPanel with the text "hello"
public MyPanel() {
super();
this.add(new JLabel("Hello"));
}
}
public class someClass {
public void someMethod() {
MyPanel myPanel = new MyPanel();
//here we add the same instance of MyPanel twice to panel1, which ..
JPanel panel1 = new JPanel();
//...adds myPanel
panel1.add(myPanel);
//...removes myPanel from the container it was added to first and adds it to this container (which is panel1 in both cases)
panel1.add(myPanel);
//here we add two separate instances of MyPanel to panel2, which should both be shown
JPanel panel2 = new JPanel();
panel2.add(new MyPanel());
panel2.add(new MyPanel());
}
}
Make your own JPanel child class that contains all you want.
Something like:
public class MyPanel extends JPanel {
JButton okButton;
JButton cancelButton;
JTextField nameTextField;
public MyPanel() {
okButton = new JButton();
JLabel nameLabel = new JLabel("Name:");
setLayOut(...);
add(okButton);
...
}
}
Either you use a GUI editor, or copy all from your current code.
Then you can use two new MyPanel()s to have identical complex components.
If you only want a duplicate image of the original panel then creating another JPanel that uses the original JPanel to paint could work.
JPanel dup = new JPanel(){
#Override
public void paintComponent(Graphics g){
jPanel1.paintComponent(g);
}
#Override
public Dimension getPreferredSize(){
return jPanel1.getPreferredSize();
}
#Override
public Dimension getMaximumSize(){
return jPanel1.getMaximumSize();
}
#Override
public Dimension getMinimumSize(){
return jPanel1.getMinimumSize();
}
};
This would only create a view of the original panel and none of the components would be functional, eg JButton or JTextField would not receive input.There would also need to be some work done to cause repainting.
Related
I have a problem with this nested class of mine. I can't add my JComponents to my panels for some reason in the constructor. It gives an error of
The method of add(JComponent, String) is undefined for the HomePanel.inputFrame
The HomePanel is the outer class while inputFrame is my inner class. My methods return JComponents and I am able to add it to my outer class but my inner class just doesn't allow it so I decided to just use add.(enterButton(), BorderLayout.SOUTH) which doesn't give any errors but when I try to use this inner class from mainWindow class, it says
The method add(JComponent) in the type container is not applicable for the arguments (HomePanel.inputFrame)
But the problem is I can use the outer class from mainWindow class which makes it confusing as to what went wrong. Is it due to it being an inner class or my order is wrong?
mainWindow class
public class mainWindow extends JFrame{
CardLayout cl = new CardLayout();
private JFrame mainPage = new JFrame("Food Expiry");
private JPanel mainPanel = new JPanel(cl);
private static final String homePage = "HomePage";
private static final String expiryDateEntry = "Expiry Date Entry";
private JPanel card1 = new JPanel();
public JPanel card2 = new JPanel();
HomePanel getGui = new HomePanel();
HomePanel.inputFrame nextPane = getGui.new inputFrame();
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
public mainWindow(){
mainPage.setSize(500, 300);
mainPage.setLocationRelativeTo(null);
mainPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPage.add(mainPanel);
mainPage.setVisible(true);
}
protected void mainPanel(){
card1.add(getGui);
}
protected void getInput(){
card2.add(nextPane);
}
}
HomePanel
public class HomePanel extends JPanel{
private JButton inputButton, storeButton;
private JLabel label, itemLabel;
private JPanel textAreaPanel, buttonPanel, labelPanel;
private JTextField itemField, itemCategory, expiryDate;
public HomePanel(){
setLayout(new BorderLayout());
this.add(getLabelPanel("Home"), BorderLayout.NORTH);
this.add(getButtonPanel(), BorderLayout.CENTER);
}
protected JComponent getButtonPanel(){
//add some buttons to a buttonPanel
return buttonPanel;
}
protected JComponent getLabelPanel(String topText){
//labelPanel
return labelPanel;
}
class inputFrame {
private JLabel categoryLabel, expiryLabel;
private JButton enter, back;
protected JComponent getInputFrame(){
textAreaPanel = new JPanel();
textAreaPanel.setLayout(new BoxLayout(textAreaPanel,
BoxLayout.Y_AXIS));
itemLabel = new JLabel("Item Name: ");
itemField = new JTextField(15);
textAreaPanel.add(itemLabel);
textAreaPanel.add(itemField);
categoryLabel = new JLabel("Category Name: ");
itemCategory = new JTextField(33);
textAreaPanel.add(categoryLabel);
textAreaPanel.add(itemCategory);
expiryLabel = new JLabel("Expiry Date: ");
expiryDate = new JTextField(33);
textAreaPanel.add(expiryLabel);
textAreaPanel.add(expiryDate);
return textAreaPanel;
}
protected JComponent enterButton(){
buttonPanel = new JPanel();
enter = new JButton("Enter");
back = new JButton("Back");
enter.addActionListener(new getInput());
back.addActionListener(new previousFrame());
buttonPanel.add(back);
buttonPanel.add(enter);
return buttonPanel;
}
public inputFrame(){
setLayout(new BorderLayout());
add(getLabelPanel("Expiry Date Entry"),BorderLayout.NORTH);
add(getInputFrame(), BorderLayout.CENTER);
add(enterButton(), BorderLayout.SOUTH);
}
}
I couldn't find any duplicates on this question and if anyone is able to find any, please do link it in the comments or answer it. Thank you.
So, based on...
public class HomePanel extends JPanel{
//...
class inputFrame {
//...
}
}
inputFrame extends (by default) from Object, making it compatible with Container#add, which expects some class which extends from Component.
Based on the current implementation, inputFrame is actually modifying the state of the instance of HomePanel from which it was instantiated, rather then adding components to itself
Something like...
public class HomePanel extends JPanel{
//...
class inputFrame extends JPanel {
//...
}
}
should alleviate the most pressing issues
I am creating a simple program in which I want to display an Image and a button.So I've created a JFrame.
This JFrame contains tow JPanel and one of the panels contains JButton and the other panel contains my image. But image is not displaying in the panel. When I add image to the JFrame, it is displaying normally. Please help with the following code!
main() method:
public class NewClass2 {
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
JFrame frm = new JFrm();
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(500,500);
}
});
}
}
JFrm class which initialize JFrame:
class JFrm extends JFrame{
JButton button;
JPanel panel0,panel1;
JFrm(){
panel0 = new JPanel();
panel1 = new JPanel();
button = new JButton("Start");
button.setPreferredSize(new Dimension(100, 30));
panel0.add(button);
panel1.add(new ImageComponent()); //adding image component to panel1 is not working
//add(new ImageComponent()); //adding image component to the frame is working ..
add(panel1,BorderLayout.PAGE_START);
add(panel0,BorderLayout.PAGE_END);
}
}
ImageComponent class which adds the image:
class ImageComponent extends JComponent{
Image img;
ImageComponent(){
img = new ImageIcon("C:\\Users\\Kaushal28\\Desktop\\Aqua-Ball-icon.png").getImage();
}
#Override
public void paint(Graphics g){
g.drawImage(img, 100,100 , null);
}
}
How can I add image to JPanel?
You did not set a size for your custom JComponent so it'll be squished by the LayoutManager to zero size. Use setPreferredSize() to set whatever size seems suitable.
ImageComponent(){
ImageIcon temp = new ImageIcon("test.png");
img = temp.getImage();
setPreferredSize(new Dimension(temp.getIconWidth(),temp.getIconHeight()));
}
#Override
public void paint(Graphics g){
g.drawImage(img, 0,0 , null);
}
Consider wrapping the image in a JLabel before adding it to the JPanel.
See Display a jpg image on a JPanel
class JFrm extends JFrame{
JButton button;
JPanel panel0, panel1;
JFrm(){
panel0 = new JPanel();
panel1 = new JPanel();
button = new JButton("Start");
button.setPreferredSize(new Dimension(100, 30));
panel0.add(button);
ImageIcon image = new ImageIcon("C:\\Users\\Kaushal28\\Desktop\\Aqua-Ball-icon.png");
panel1.add(new JLabel(image));
add(panel1, BorderLayout.PAGE_START);
add(panel0, BorderLayout.PAGE_END);
}
}
Basically I need to duplicate a JPanel, for example if we do it with Integer variables this should work:
Integer intaux,int1;
int1 = 3;
intaux = int1;
But this doesn't work with panels:
jPanelaux = jPanel1;
Is there any setter method I don't know?
As Kira San already said, you will need an instance for every panel you want to display.
For example:
public class MyPanel extends JPanel {
//creates a JPanel with the text "hello"
public MyPanel() {
super();
this.add(new JLabel("Hello"));
}
}
public class someClass {
public void someMethod() {
MyPanel myPanel = new MyPanel();
//here we add the same instance of MyPanel twice to panel1, which ..
JPanel panel1 = new JPanel();
//...adds myPanel
panel1.add(myPanel);
//...removes myPanel from the container it was added to first and adds it to this container (which is panel1 in both cases)
panel1.add(myPanel);
//here we add two separate instances of MyPanel to panel2, which should both be shown
JPanel panel2 = new JPanel();
panel2.add(new MyPanel());
panel2.add(new MyPanel());
}
}
Make your own JPanel child class that contains all you want.
Something like:
public class MyPanel extends JPanel {
JButton okButton;
JButton cancelButton;
JTextField nameTextField;
public MyPanel() {
okButton = new JButton();
JLabel nameLabel = new JLabel("Name:");
setLayOut(...);
add(okButton);
...
}
}
Either you use a GUI editor, or copy all from your current code.
Then you can use two new MyPanel()s to have identical complex components.
If you only want a duplicate image of the original panel then creating another JPanel that uses the original JPanel to paint could work.
JPanel dup = new JPanel(){
#Override
public void paintComponent(Graphics g){
jPanel1.paintComponent(g);
}
#Override
public Dimension getPreferredSize(){
return jPanel1.getPreferredSize();
}
#Override
public Dimension getMaximumSize(){
return jPanel1.getMaximumSize();
}
#Override
public Dimension getMinimumSize(){
return jPanel1.getMinimumSize();
}
};
This would only create a view of the original panel and none of the components would be functional, eg JButton or JTextField would not receive input.There would also need to be some work done to cause repainting.
I want to create a JPanel subclass thats holds some JLabels. I started to write my code but I immediatly find a big problem. Component added to the JPanel subclass are not visible (or they are not added to JPanel I don' t kano). This is the code of the JPanel Subclass:
public class ClientDetails extends JPanel
{
private JLabel nameAndSurname = new JLabel ("Name & Surname");
private JLabel company = new JLabel ("Company");
private JPanel topPanel = new JPanel ();
public ClientDetails ()
{
this.setBackground(Color.white);
this.setLayout(new BorderLayout());
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
topPanel.add(nameAndSurname);
topPanel.add(company);
this.add(topPanel,BorderLayout.PAGE_START);
}
}
You need to
put the JPanel in a top level container (like a JFrame)
call pack() on it so the LayoutManager finds room for your stuff
.
public class Test extends JPanel {
private JLabel nameAndSurname = new JLabel ("Name & Surname");
private JLabel company = new JLabel ("Company");
private JPanel topPanel = new JPanel ();
JFrame frame;
public Test()
{
this.setBackground(Color.white);
this.setLayout(new BorderLayout());
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
topPanel.add(nameAndSurname);
topPanel.add(company);
this.add(topPanel,BorderLayout.PAGE_START);
frame = new JFrame("test");
frame.add(this);
frame.pack();
frame.setVisible(true);
}
}
Ok say I have a JPanel "controls" and jpanel "graphPanel" within another jpanel
public class outer extends JPanel implements ActionListener{
private JPanel controls,graphPanel;
private JButton doAction
public outer(){
JPanel controls = new JPanel();
JButton doAction = new JButton("Do stuff");
doAction.addActionListener(this);
controls.add(doAction);
JPanel graphPanel = new JPanel();
this.add(controls);
this.add(graphPanel);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==doAction){
//How do I fire paintComponent of controls JPanel on this click
}
How do i make "graphPanel" repaint after my button is clicked
The doAction and graphPanel are declared twice - once at the class level, then again in the method:
private JPanel controls,graphPanel; // declared here first
private JButton doAction; // declared here first
public outer(){
JPanel controls = new JPanel(); // Whoops, declared here again
JButton doAction = new JButton("Do stuff"); // Whoops, declared here again
doAction.addActionListener(this);
controls.add(doAction);
JPanel graphPanel = new JPanel(); // Whoops, declared here again
...
In the method remove the declaration, and make them simple assignments, like this:
controls = new JPanel(); // no leading 'JPanel'
Do that and the additional repaint code won't throw a NPE