简体   繁体   中英

Update border title when JmenuItem is clicked

My question is a little complicated please,Am trying to update the borderTitle of a Jpanel when a JMenuItem is clicked,i have 3 classes,A implements ActionListener, B is the JPanel class and C is the JFrame class,here is what i have tried already

public class PanelTitle implements ActionListener{
     String title;
    public PanelTitle(){
    }                     
    @Override
    public void actionPerformed(ActionEvent ae){
         SedimentPanel sp = new SedimentPanel();
         sp.titledBorder.setTitle("SEDIMENT");

         sp.repaint();
         sp.revalidate();


    }
}

i have this in my JFrame class

 velocityMenuItem.addActionListener(new PanelTitle());

here is my JPanel class

public class SedimentPanel extends JPanel{
    public SedimentPanel(){
        super();
        initComponents();
        initPlaceHolders();
        setBorder(titledBorder); 

    }

    TitledBorder titledBorder = BorderFactory.createTitledBorder(null, "border title",TitledBorder.CENTER,TitledBorder.DEFAULT_POSITION);

}

please how do i really get the borderTitle to change when i click a JMenuItem? Here is how i referenced it in the frame class,now i get a Nullpointer Exception

public class FrameClass extends JFrame{
private static SedimentPanel sp;
    public FrameClass(SedimentPanel sp){
        this.sp = sp;}
}
public static void main(String args[]){
 FrameClass fc = new FrameClass(sp);
}

You're making a basic mistake here:

public class PanelTitle implements ActionListener{
     String title;
    public PanelTitle(){
    }                     
    @Override
    public void actionPerformed(ActionEvent ae){
         SedimentPanel sp = new SedimentPanel(); // ********
         sp.titledBorder.setTitle("SEDIMENT");

         sp.repaint();
         sp.revalidate();
    }
}

That new SedimentPanel is a completely new reference and calling a method on it will have no effect on the original displayed object. Don't do this, get the appropriate reference and call the method on it.

public class PanelTitle implements ActionListener{
    String title;
    private SedimentPanel sp;

    public PanelTitle(SedimentPanel sp){  // pass in reference
        this.sp = sp;
    }

    @Override
    public void actionPerformed(ActionEvent ae){
         // SedimentPanel sp = new SedimentPanel(); // ******** NO
         // sp.titledBorder.setTitle("SEDIMENT");
         sp.setTitle("SEDIMENT"); // better to give the class this method
         sp.repaint();
         sp.revalidate();
    }
}

public class SedimentPanel {
    private TitledBorder titledBorder = ....;

    public void setTitle(String title) {
        titledBorder.setText(title);
    }
}   

Then when you create this listener, pass in the appropriate reference to the actual visualized JPanel.

That you're making this mistake suggests that it wouldn't harm you to read or re-read a decent chapter in your text on what an object/reference is and what it represents because this is a foundational mistake that you're making.

A working example:

import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class TitleExample {

    private static void createAndShowGui() {
        SedimentPanel sedimentPanel = new SedimentPanel();
        PanelTitle panelTitle = new PanelTitle(sedimentPanel); // pass in the reference
        JMenuItem menuItem = new JMenuItem("SEDIMENT");
        menuItem.addActionListener(panelTitle);
        JMenu jMenu = new JMenu("Menu");
        jMenu.add(menuItem);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(jMenu);

        JFrame frame = new JFrame("TitleExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(sedimentPanel);
        frame.setJMenuBar(menuBar);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

class SedimentPanel extends JPanel {
    private TitledBorder titledBorder = BorderFactory.createTitledBorder(null, "border title", TitledBorder.CENTER,
            TitledBorder.DEFAULT_POSITION);

    public SedimentPanel() {
        super();
        setBorder(titledBorder);
        setPreferredSize(new Dimension(400, 300));
    }

    public void setTitle(String title) {
        titledBorder.setTitle(title);
        repaint();
    }

}

class PanelTitle implements ActionListener{
    String title;
    private SedimentPanel sp;

    public PanelTitle(SedimentPanel sp){  // pass in reference
        this.sp = sp;
    }

    @Override
    public void actionPerformed(ActionEvent ae){
         sp.setTitle("SEDIMENT"); // better to give the class this method
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM