Adding Second Border to JButton - java

Here's the issue: When I try to add a border to a JButton via setBorder(), the normal background styling of the button:
disappears, to be replaced by what is essentially a clickable JLabel:
Basically, what I'd like to do is add a colored border around the current default border. If there's another process other than using setBorder() that would work, I would be interested in hearing about it.
Also, I should add that I cannot subclass or override methods of the graphics elements, as this needs to be inserted as a standalone tool in a far larger code repository.
Edit: Specifying question better

You should create a compound border. You can do this:
JButton myButton = new JButton("BUTTON TEXT");
myButton.setBorder(BorderFactory.createCompoundBorder(myButton.getBorder(), BorderFactory.createLineBorder(Color.RED));
This will preserve the look/feel of the button and will add a red border.

Related

Removing the added border from JTextField and bringing it back to normal

Please have a look at the below code
Border border = BorderFactory.createLineBorder(Color.RED, 1);
introducerFeesTxt.setBorder(border);
I used this code to create a line border for the JTextField. However now I need to remove it and replace it's normal view. Below is what I tried.
introducerFeesTxt.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
That code above again created a border which is not similar to other normal JTextFields. Below is a screenshot.
You can clearly see the differnece between the "Normal" JTextField and the JTextField with the added border. How can I reset it to be "normal" ?
You could keep the original border in a variable before change it and then use this border to set it back to its original state:
Border originalBorder;
...
JTextField textField = new JTextField(20);
originalBorder = textField.getBorder();
// here you can safely change text field's border
Of course the scope of this originalBorder variable should be wide enough to use it when needed (f.e.: class member).
Note: Please note this approach is independent of the PLAF used by your application.
You should use the original border from the L&F (Look and Feel).
Border border = BorderFactory.createLineBorder(Color.RED, 1);
introducerFeesTxt.setBorder(border);
// some operation
introducerFeesTxt.setBorder(UIManager.getBorder("TextField.border"));

JComponent Titled Border

I am making a simple swing application and I want to add some titled borders to my components. The border on both of my JScrollPanes work fine, but the JTextField and the JButtons don't. Allow me to share some screen shots.
I just have simple code for this. i.e
TitledBorder border = new TitledBorder("Border");
convert.setBorder(border); //convert is the JButton
I don't see why it would not work for one thing, and work for the other. Can anyone help me out?
A JTextField and JButton both use a Border already. So the titled border works but it changes the appearance of the component because you lose the default Border.
I also agree that normally you don't use a TitledBorder for an individual component but I suppose you could try to use a CompoundBorder to see if it looks any better:
CompoundBorder border = new CompoundBorder(titledBorder, button.getBorder());
button.setBorder( border );
but then the problem with the above approach is that you lose dynamic repainting of the border when you press/release the mouse on the button.

Draw something on JButton's right and top corner

I am trying to create JButton such as there must be a number painted on the top and right corner of JButton.
For example, in case of notification buttons there is a message in the button, how is that possible? Can the help be taken of paint method to draw the label?
there are three ways, by using
GlassPane
JLayer (Java7) based on JXLayer(Java6)
JButtton (all JComponents) is container too, there is easy and possible use standard LayoutManagers (only JFrame == BorderLayout and JPanel == FlowLayout have got implemented LayoutManager in API directly), then basically everything is possible
JButton and any JComponent extend Container class, so you should be able to add elements into JButton as if it were a simple panel. So in your case you can add a JLabel with your text into a button.
Also consider implementing Icon to decorate the button; ColorIcon is a simple example. You can use the color to signify buttons that are likely to need attention, and you can use drawString() specify a number.

Creating a fancy search bar with Swing components

I'm trying to come up with an elegant recreation of the search bar component in Thunderbird. The clear button doesn't appear until there is text in the box, so that screen-shot is a bit inaccurate.
Should I use a layered pane and get some buttons to float above the box? (but then getting the text to not appear below the buttons would be hacky)
Maybe just put buttons at the ends of the search bar and have it somehow blend in?
Any ideas or maybe a style reconsideration is welcome, thank you.
What about a white panel with a border and a JTextField without borders inside. Two buttons (or more) in the west and east. Button will appear/hide depending on the text field content.
You might be able to use the Text Prompt for the "Search all text" display.
Check out JideSoft's Common Layer and the Overlayable class.
Demos
For building a very similar component I've used JXLayer (for drawing the buttons) in conjunction with IntelliHints from JIDE OSS project (for implementing a drop down list of values).
This code adds a label with given icon to the right of the JTextPane. One thing to work on: don't let the text go under the label. You can use setMargin(), but it shifts the label too.
JTextField searchField = new JTextField(30);
searchField.setLayout(new BorderLayout());
JLabel label = new JLabel(icon);
label.setCursor(Cursor.getDefaultCursor());
searchField.add(label, BorderLayout.LINE_END);
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
searchField.setText("");
}
});

Changing Font Size and leaving space between button in a JOptionPane

The text in my JOptionPanes are way to small I was wondering how I could change the font of the text inside the pane. Also, I would like to be set a space between two buttons.
Something like
|Canecl| |DoneSave| |Save|
I guess you mean the fonts on your JButtons inside the JOptionPane are too small.
For that I suggest using Swing Utils by Darryl Burke. Now you can easily do e.g.
for (JButton button : SwingUtils.getDescendantsOfType(JButton.class, pane)) {
button.setFont(new Font("Verdana", Font.BOLD, 32));
}
to set the fonts on all JButtons inside of JOptionPane pane.
If you want to set the font of the JOptionPane message itself, use e.g.
UIManager.put(
"OptionPane.messageFont",
new FontUIResource(new Font("Verdana", Font.BOLD, 32))
);
In regard to the "spacing of buttons" question: I don't know if that can be done without extending & make a customized JOptionPane yourself.
You cannot specify a spacing between just 2 buttons out of 3 (as per the OP's question) but you can increase the size of the spacing between ALL the buttons :
JPanel buttonPanel = (JPanel) myOptionPane.getComponent(1);
BasicOptionPaneUI.ButtonAreaLayout lm2 = (BasicOptionPaneUI.ButtonAreaLayout) buttonPanel.getLayout();
lm2.setPadding(20);
lm2.setSyncAllWidths(false); // buttons will vary in size as needed.
or perhaps something like :
lm2.setPadding(lm2.getPadding() * 2) // double the spacing between ALL buttons.
If there were just 2 buttons, then using these LayoutManager calls you could achieve the desired outcome.
However for varying-sized spaces between the buttons you need to implement your own JDialog where you control the layout of its JPanel yourself.
A JOptionPane is made up of 2 JPanels... one for the message (and icon) itself, and one for the buttons, that's why we getComponent(1) above.
I know the question is 4 years old but I made this answer because I had a similar need today and couldn't find the answer ANYWHERE on Stack overflow or elsewhere.

Categories

Resources