1

I have custom JButton class with gradient background and rounded corners. Should I override setText() or what should I do to display text upon it? setText("text") has no effect.

UPD: I've tried setContentAreaFilled(false), like MadProgrammer suggested, and it solved this problem, but another appeared. I have such buttons on different tabs, and when they are repainted, they appear for some milliseconds like they all were at the same tab. Updated code is here:

public class DarkGradientButton extends JButton {
private Color startColor = new Color(178, 178, 178);
private Color endColor = new Color(131, 131, 131);

private Color disstartColor = new Color(252, 252, 252);
private Color disendColor = new Color(221, 221, 221);

private Color enstartColor = new Color(178, 178, 178);
private Color enendColor = new Color(131, 131, 131);

public DarkGradientButton(String text, ImageIcon ii) {
    super(text, ii);
    this.setContentAreaFilled(false);
}

public DarkGradientButton() {
    super();
    this.setContentAreaFilled(false);
}

@Override
protected void paintComponent( Graphics g )
{
    g.setColor(new Color(246,250,245));        
    int h = getHeight();
    int w = getWidth();
    GradientPaint gradientPaint = new GradientPaint( 0 , 0 , startColor , 0 , h , endColor );
    int[] x = {0, 1, w-1, w, w,   w-1, w-2, 2,   1, 0};
    int[] y = {1, 0, 0,   1, h-3, h-2, h-1, h-1, h-2, h-3};
    Graphics2D graphics2D = (Graphics2D)g;
    graphics2D.setPaint( gradientPaint );
    graphics2D.fillPolygon(x, y, x.length);    
    graphics2D.setColor(this.getForeground());
    super.paintComponent( g );
}


public void makeDisable() {
    startColor = disstartColor;
    endColor = disendColor;        
    Graphics g = this.getGraphics();
    paintComponent(g);
    this.repaint();
}

public void makeEnable() {
    startColor = enstartColor;
    endColor = enendColor;        
    Graphics g = this.getGraphics();
    paintComponent(g);
    this.repaint();
}
}


Buttons on tabs (that is what Netbeans generated for me):

callStartButton = new gui.DarkGradientButton(language[25], new ImageIcon("pic\\call-start.png"));

callStartButton.setBorder(null);
    callStartButton.setForeground(new java.awt.Color(255, 255, 255));
    callStartButton.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
    callStartButton.setPreferredSize(new java.awt.Dimension(95, 23));
    callStartButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            callStartButtonActionPerformed(evt);
        }
    });


org.jdesktop.layout.GroupLayout callPanelLayout = new org.jdesktop.layout.GroupLayout(callPanel);
    callPanel.setLayout(callPanelLayout);
    callPanelLayout.setHorizontalGroup(
        callPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(callPanelLayout.createSequentialGroup()
            .add(callPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(callPanelLayout.createSequentialGroup()
                    .add(19, 19, 19)
                    .add(callStartButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(callStopButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(callPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .add(numberLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(callPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(numberExampleLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(callPanelLayout.createSequentialGroup()
                            .add(codeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(numberField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .add(callPanelLayout.createSequentialGroup()
                    .add(25, 25, 25)
                    .add(callProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 190, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(14, Short.MAX_VALUE))
    );


callPanelLayout.setVerticalGroup(
        callPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(callPanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(callPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(numberField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(codeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(numberLabel))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(numberExampleLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(18, 18, 18)
            .add(callPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(callStartButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(callStopButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 8, Short.MAX_VALUE)
            .add(callProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(13, 13, 13))
    );

org.jdesktop.layout.GroupLayout testPanelLayout = new org.jdesktop.layout.GroupLayout(testPanel);
    testPanel.setLayout(testPanelLayout);
    testPanelLayout.setHorizontalGroup(
        testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(testPanelLayout.createSequentialGroup()
            .add(8, 8, 8)
            .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(testPanelLayout.createSequentialGroup()
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(ascii, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(0, 0, 0)
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(hex, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jScrollPane9)))
                .add(lightGradientPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(testPanelLayout.createSequentialGroup()
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(testPanelLayout.createSequentialGroup()
                            .add(testManagementPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(0, 0, 0)
                            .add(testResultPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(testPanelLayout.createSequentialGroup()
                            .add(testModemHeaderPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(0, 0, 0)
                            .add(testResultHeaderPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(0, 0, 0)
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(callHeaderPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(callPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
            .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(modemModelPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(modemHeaderPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(modemModelHeaderPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(modemParamsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(simPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 240, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(testModemHeaderPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 240, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(17, 17, Short.MAX_VALUE))
        .add(testPanelLayout.createSequentialGroup()
            .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, testPanelLayout.createSequentialGroup()
                    .add(10, 10, 10)
                    .add(userGuideButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(irzLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(testPanelLayout.createSequentialGroup()
                    .add(309, 309, 309)
                    .add(saveASCIILogButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );
    testPanelLayout.setVerticalGroup(
        testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(testPanelLayout.createSequentialGroup()
            .add(15, 15, 15)
            .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                .add(testPanelLayout.createSequentialGroup()
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(testModemHeaderPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(testResultHeaderPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(callHeaderPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(testResultPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(testManagementPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(callPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(22, 22, 22)
                    .add(lightGradientPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, 0)
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 271, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jScrollPane9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 271, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(hex, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, ascii, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(testPanelLayout.createSequentialGroup()
                    .add(modemModelHeaderPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, 0)
                    .add(modemModelPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, 0)
                    .add(modemHeaderPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, 0)
                    .add(modemParamsPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(testModemHeaderPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, 0)
                    .add(simPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(testPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(testPanelLayout.createSequentialGroup()
                    .add(saveASCIILogButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(userGuideButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, testPanelLayout.createSequentialGroup()
                    .add(0, 34, Short.MAX_VALUE)
                    .add(irzLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())))
    );

    jTabbedPane1.addTab("tab1", testPanel);

jLayeredPane1.add(jTabbedPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);
    jTabbedPane1.getAccessibleContext().setAccessibleName("");

    getContentPane().add(jLayeredPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 5, -1, 660));
Natalia
  • 783
  • 7
  • 18
  • It's not the setText that does work, it is you that is painting over the the text with your override which hides the text. – Guillaume Polet Jul 17 '12 at 10:53
  • Simply do one thingy, send the `Title` that you want as a text of a `JButton` to your `GradientButton` Class's `Constructor`, and write `super(title)` as the first line of the `Constructor`, that will do :-) Here is one related [example](http://stackoverflow.com/a/11490905/1057230) – nIcE cOw Jul 17 '12 at 17:17
  • It didn't work, I can't see text that way – Natalia Jul 18 '12 at 06:17
  • super.paintComponent(), has to be the first line of the respective method, not the last :( Do not make calls to paintComponent() explicitly, that is not meant for developers to call, let Swing worry about that part, simply call repaint() instead :-) – nIcE cOw Jul 18 '12 at 18:58
  • I hope you know the answer to the question, Why you writing, What you writing, in your code ? – nIcE cOw Jul 18 '12 at 19:05
  • Thank you, now everything seems fine :) I'm new to Swing, and haven't yet know it to a nicety. As for the super.paintComponent() - in my case it does should be the last line - I found this in some answers here. When I call it from the first line, I overlay button's title by gradient then. – Natalia Jul 19 '12 at 06:13

5 Answers5

4

The main reason setText isn't working is because your painting over it

g.fillRect(0, 0, w, h);

You'll find that he ui is rendering the text in super.paintComponent(g) call, then you're painting over it

UPDATE

I used the following code to show some tabs and couldn't find anything wrong...

package test;

import java.awt.BorderLayout; import java.awt.GridBagLayout; import java.util.List; import javax.swing.*; import javax.swing.plaf.ColorUIResource;

public class TestButton {

public static void main(String[] args) {
    Object grad = UIManager.get("Button.gradient");
    List gradient;
    if (grad instanceof List) {
        gradient = (List) grad;
        System.out.println(gradient.get(0));
        System.out.println(gradient.get(1));
        System.out.println(gradient.get(2));
        System.out.println(gradient.get(3));
        System.out.println(gradient.get(4));
        //gradient.set(2, new ColorUIResource(221, 232, 243));//origal Color
        //gradient.set(2, new ColorUIResource(255, 255, 255));//origal Color
        //gradient.set(2, new ColorUIResource(184, 207, 229));//origal Color
        gradient.set(2, new ColorUIResource(190, 230, 240));
        gradient.set(3, new ColorUIResource(240, 240, 240));
        gradient.set(4, new ColorUIResource(180, 200, 220));
        //UIManager.put("Button.background", Color.pink);
    }
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new TestButton().makeUI();
        }
    });
}

public void makeUI() {
    JButton button = new JButton("Click");
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());

    JTabbedPane tab = new JTabbedPane();
    tab.add("Help", createPane(1));
    tab.add("Help", createPane(2));
    tab.add("Help", createPane(3));
    tab.add("Help", createPane(4));
    tab.add("Help", createPane(5));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tab);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

protected JPanel createPane(int index) {

    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(new JButton("Hello " + index));

    return panel;

}

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Deleted that part and it didn't work either. Maybe that's gradient overlaying text? How can I place it "under" the text? – Natalia Jul 17 '12 at 10:36
  • Any fill action will paint over the text, try setting the fillContentArea property to false & place your custom paint before the super call. If that doesn't work, there might be a small hack I use to paint candy stripping on tables & trees that might work – MadProgrammer Jul 17 '12 at 10:52
  • ps in swing, any graphics context past to component is guaranteed to be Graphics2D object – MadProgrammer Jul 17 '12 at 10:55
  • 1
    In my tests, setting contentAreaFilled and borderPainted to false allowed me to render under the text. I don't know if this is the desired effect you're after, but it might help – MadProgrammer Jul 17 '12 at 11:03
  • I found a problem. I have such buttons placed on different tabs. When repaint() is called for them, they appear for some milliseconds as if they were all at the current tab. – Natalia Jul 17 '12 at 13:25
  • Updated again. That code was generated for me by Netbeans, I'm not good at it – Natalia Jul 18 '12 at 06:48
  • I've expressed myself wrong. When I switch betweeb tabs everything goes right. But in my app when user start some process, I need to disable every buttons except one. When I do so, using makeDisable() method from above, buttons from all tabs appear on the screen for a little time. – Natalia Jul 18 '12 at 07:54
  • I can't get your example code to work (the button is fine, but there's not enough of the form code for to regenerate working code)...Paste a smaller, complete example if you can. I did a quick test and had not problems (simple two buttons on separate tabs) Can you verify that the panels are actually JPanel's and not AWT Panel's – MadProgrammer Jul 18 '12 at 23:53
2
  • you can to change vlaues in the UIManager, in the case that every JButtons will have got the same color theme

  • then there no issue with rest of methods implemnted in the API

  • you can to override BasicButtonUI, example for MetalButtonUI

code for UIManager

import java.util.List;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;

public class GradieltButton {

    public static void main(String[] args) {
        Object grad = UIManager.get("Button.gradient");
        List gradient;
        if (grad instanceof List) {
            gradient = (List) grad;
            System.out.println(gradient.get(0));
            System.out.println(gradient.get(1));
            System.out.println(gradient.get(2));
            System.out.println(gradient.get(3));
            System.out.println(gradient.get(4));
            //gradient.set(2, new ColorUIResource(221, 232, 243));//origal Color
            //gradient.set(2, new ColorUIResource(255, 255, 255));//origal Color
            //gradient.set(2, new ColorUIResource(184, 207, 229));//origal Color
            gradient.set(2, new ColorUIResource(190, 230, 240));
            gradient.set(3, new ColorUIResource(240, 240, 240));
            gradient.set(4, new ColorUIResource(180, 200, 220));
            //UIManager.put("Button.background", Color.pink);
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GradieltButton().makeUI();
            }
        });
    }

    public void makeUI() {
        JButton button = new JButton("Click");
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(button);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

In a logic case you don't need to override the setText() but you can try it.

MByD
  • 135,866
  • 28
  • 264
  • 277
Baptiste
  • 21
  • 1
  • I've tried like super.setText(text) but it had no effect either – Natalia Jul 17 '12 at 09:53
  • 1
    @Natalia Overriding it with a call which just delegate to the super class is the equivalent of not overriding the method. Of course this has no effect – Robin Jul 17 '12 at 10:00
0

I don't see where you call setText(), if you attempt to do it AFTER the component was painted then you will need to call rePaint() for any graphical changes to have visible effect. And no, you don't meed to override setText(), what will be the point?

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • 1
    I call it later from the code. When you call setText() for ordinary JButton you don't call repaint() after that. – Natalia Jul 17 '12 at 10:39
  • Indeed callin setText will eventually trigger a repaint of the JButton. – Guillaume Polet Jul 17 '12 at 11:41
  • Maybe I though wrong, but how would the system know when to trigger repaint? Does it happen on every update to every visual component? I must admit that GUI is not my primary speciality. – Germann Arlington Jul 17 '12 at 14:41
-1

you should add a constructor with super(label).

public DarkGradientButton(String label) { super(label)}