0

I created a button with JTextPane inside. When I added JTextPane to the JButton I cant click on the button. When I remove JTextPane I can click on the button. Somebody know how to fix this problem?

public class BtnOrderFefcoStyle extends JButton {
    
        private final static String NAME_FONT = "ARIAL";
        private final static boolean IS_BOLD = true;
        private final static int SIZE_FONT = 18;
        private final static Color NAVY_COLOR = new Color(0 , 51 , 102)
        private final static int BUTTON_WIDTH  = 122;
        private final static int BUTTON_HEIGHT = 64;
        private final static int BORDER_THICKNESS = 2;
        private int posX , posY;


        public BtnOrderFefcoStyle(int pos_x , int pos_y) 
        {
            super() ;   
            this.posX = pos_x;
            this.posY= pos_y;
            setOpaque(false);
            setBorderPainted(false);
            setContentAreaFilled(false);
            setFocusable(false);
            setEnabled(true);
            setBorder(null);
            setBounds(pos_x, pos_y, BUTTON_WIDTH, BUTTON_HEIGHT);
            setForeground(Color.WHITE);

            
            JTextPane nameBtn = new JTextPane();
            nameBtn.setEditable(false);
            nameBtn.setText("ADD TO LIST");
            nameBtn.setAutoscrolls(true);
            nameBtn.setEnabled(false);
            nameBtn.setOpaque(false);
            
            SimpleAttributeSet attribs = new SimpleAttributeSet();
            StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_CENTER);
            StyleConstants.setFontFamily(attribs, NAME_FONT);
            StyleConstants.setBold(attribs, IS_BOLD);
            StyleConstants.setFontSize(attribs, SIZE_FONT);
            
            nameBtn.setParagraphAttributes(attribs, true);
            nameBtn.setCaretPosition(1);
            nameBtn.setParagraphAttributes(attribs, true);
            nameBtn.setDisabledTextColor(NAVY_COLOR);
            //nameBtn.setBackground(new Color(153 , 153, 153));
            nameBtn.setSize(BUTTON_WIDTH - 2 * BORDER_THICKNESS  , BUTTON_HEIGHT - 2 * BORDER_THICKNESS);
            //nameBtn.setForeground(NAVY_COLOR);
            add(nameBtn);   
        }

        @Override
        protected void paintComponent(Graphics g) 
        {
            // TODO Auto-generated method stub
            super.paintComponent(g);
            g.setColor(Color.WHITE);
            g.fillRect(0 , 0 , BUTTON_WIDTH, BUTTON_HEIGHT);
            g.setColor(Color.GRAY);
            g.fillRect(BORDER_THICKNESS , BORDER_THICKNESS , BUTTON_WIDTH - 2 * BORDER_THICKNESS , BUTTON_HEIGHT  - 2 * BORDER_THICKNESS );
        }   
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Dawid Z
  • 3
  • 3
  • where is your code? – Reimeus Jun 26 '20 at 10:02
  • Could you maybe show your code and explain why you are trying to add a JTextPane to a JButton? You can show text on a JButton without a JTextPane. – maloomeister Jun 26 '20 at 10:03
  • I would like wrap my text on the button thats why i used a JTextPane. Otherwise I can't see the text when I craeted text by JButton's constructor because i use paintComponent propably – Dawid Z Jun 26 '20 at 10:27
  • But if thats your only wish, to make the text wrap, then why make it this complicated? This is easily done with built in functions of the JButton. Check [Word Wrap in JButtons](https://stackoverflow.com/questions/5766175/word-wrap-in-jbuttons). You can also change the font of the displayed text with default JButton behavior. – maloomeister Jun 26 '20 at 10:35
  • I know that I can use ex. HTML lang. But I want to insert a raw text without any special signs. In the future Im going to import the text from a file. – Dawid Z Jun 26 '20 at 10:44
  • With HTML you can concat the HTML tags + your raw text + closing HTML tags. You're making this too complicated – Frakcool Jun 26 '20 at 13:16

2 Answers2

1

What's happening in your current example is:

Your JTextPane is being placed on top of your JButton and as you removed the border from your JButton it's taking all the available space, so you're clicking over the JTextPane all the time.

If we add a border to the JTextPane this is what we get before removing the JButton's border and after

enter image description here enter image description here

To fix this you can either:

  • Add a MouseListener to the JTextPane, for example:

    nameBtn.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
             super.mouseClicked(e);
             System.out.println("Clicked on nameBtn");
         }
    });
    
  • create a method to use HTML as suggested in the comments above:

      private String getButtonString(String stringFromFile) {
          StringBuilder sb = new StringBuilder();
          sb.append("<html><body><p>");
          sb.append(stringFromFile);
          sb.append("</p></body></html>");
          return sb.toString();
    }
    

This is how each of them look like when resized:

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

A MouseEvent is passed to the component that is clicked. If you add a JTextPane to the JButton, then you are clicking on the text pane, not the button.

You can tryd to remove the MouseListeners from the JTextPanel

for (MouseListener ml: textPane.getListeners(MouseListener.class))
    textPane.removeMouseListener( ml );

Not sure if you also need to remove the MouseMotionListener.

camickr
  • 321,443
  • 19
  • 166
  • 288