So I have created multiple JButtons in my app. Now I want to modify their design, like, change color, size, style, etc. Is there any way to avoid having to set these things to every JButton separately?
Custom Class that extends JButton
As already suggested, you can use a custom class that extends JButton change the styling of the button there, and then create instances from this custom class.
An example:
import javax.swing.*;
import java.awt.*;
public class CustomJButton extends JButton
{
private Font font = new Font("MS UI Gothic", Font.BOLD, 30);;
public CustomJButton(String buttonName)
{
this.setText(buttonName);
this.setFont(font);
this.setForeground(Color.BLUE);
}
}
You then create an instance of this class in your main class like this:
.
.
CustomJButton button = new CustomJButton("Click Me");
.
.
And you have as a result:
Alternative solution
You can use the Font object to declare "global" settings.
An example:
Font font = new Font("MS UI Gothic", Font.BOLD, 30);
And lets assume that you have the following jButtons: jButton1, jButton2:
jButton1.setFont(font);
jButton2.setFont(font);
The font class as can be seen from the above link it doesn't include color attribute, unfortunately, but an easy fix, is to declare a global Color object lets say: Color colorButton = Color.BLUE that you can later use to set the color to all the buttons.
This becomes useful when you want to change the color of the buttons, you just have to change one line of code, and will affect all the buttons.
Conclusion
Both solutions may solve your problem, but, in my opinion, implementing your custom class that extends JButton is the way to go, as your code will look much clear and straightforward.
Related
So I'm writing a program in which I would eventually like a JButton to change color when pressed. So far, here is my dilemma:
-I cannot get the JButton I currently have set up to show the background color, despite toggling the true/false of setOpaque() and setContentAreaFilled(). I would like to be able to do this before even attempting to add an ActionListener.
-I would like to use something similar to JToggleButton() to change the color, however I would like to do this with just a background instead of an icon.
I am using a mac, and I'm wondering if it has something to do with the default button settings, but I don't know how to change/override these.
I would post a screenshot, but unfortunately my reputation isn't high enough yet.
Here is an example of what I am getting:
import java.awt.*;
import javax.swing.*;
public class Example extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame main = new JFrame("Example");
JPanel content = new JPanel();
JButton button = new JButton("Press");
button.setBackground(Color.orange);
button.setContentAreaFilled(false);
button.setOpaque(true);
content.add(button);
main.setContentPane(content);
main.setVisible(true);
main.setSize(40,60);
main.setLocation(500, 200);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
For those (maybe on a windows or linux platform) who can't see what I'm talking about, the button appears and the layout behind the button (not the border or the JPanel, just a small rectangle around the button) is colored to what was specified.
Thank you in advance for your time.
Also, I know that inside the ActionListener the getSource() method will return the text associated with the button. Is there any way to use something similar to change the color of the button from inside the ActionListener or is this idea just a redundancy for something that can be done in a much simpler fashion.
Much appreciated.
i think you want play something around with the look and feel.
try
button.setUI(new MetalButtonUI());
or try some other background color supported UI for the button.
I would eventually like a JButton to change color when pressed
here are two different ways (note JButton has an arrays of colors in UIManager)
override events from ButtonModel (ChangeListener or the same methods are implemented e.g. isPressed, isArmed in JButtons API), accelators are valid for mouse and KeyEvens (selection or focusInWindow)
override BasicsButtonUI (for real project)
I am using a mac, and I'm wondering if it has something to do with the
default button settings, but I don't know how to change/override
these.
depends of Look and Feel that your Java uses (Quaqua - default on OSX or standards by Oracle Metal, Nimbus ...)
I think that button.setOpaque(true); is unnecessary.
Remove it, or change it to: button.setOpaque(false); and see what happened
Newbie here trying to make a simple GUI with JTabbedPane. I've looked through a lot of examples but have been unable to find a solution. Basically, I'm trying to print out a String to a JTextArea. While it seems very simple, I have been unable to get everything to work together. I understand the difference between local and global variables, but I think that is where my problem lies. Any guidance would be greatly appreciated. *Please note that we are unable to use a layout manager for this project.
The code below represents part of the tab that has the JButton and JTextArea.
//Text area that shows details. Scrolls.
JTextArea areaDeets = new JTextArea();
areaDeets.setBounds(65, 300, 250, 300 );
areaDeets.setText("");
JScrollPane scroll = new JScrollPane (areaDeets);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
panel2.add(areaDeets);
panel2.add(scroll);
areaDeets.addActionListener(new StopTest());
//Stop button--stops tests when pressed.
JButton stop = new JButton("Stop");
stop.setBounds(215, 650, 100, 40);
panel2.add(stop);
stop.addActionListener(new StopTest());
The code below is the method that calls the ActionListener.
//Panel 1 - Stop, shows that the test has been stopped
static class StopTest implements ActionListener{
public void actionPerformed(ActionEvent e){
String stop = "The test has been stopped";
areaDeets.setText(stop);
panel2.repaint();
}
}
Edit: Code does not compile. Eclipse says that I cannot call addActionListener on a JTextField.
You can pass the reference of the JTextArea to the constructor of the ActionListener:
public class StopTest implements ActionListener {
private JTextArea area;
public StopTest(JTextArea area) {
this.area = area;
}
public void actionPerformed(ActionEvent e) {
String stop = "The test has been stopped";
area.setText(stop);
}
}
One possible solution (guessing here due to limited information): don't make the StopTest class static. Rather make it a private inner non-static class or a stand-alone class. This will allow it access to non-static fields of your outer class.
Don't add an ActionListener to your JTextArea as this is not allowed and has no real meaning, since JTextAreas allow returns to be entered, and use this to start a new line in the JTextArea.
Other unrelated recommendations:
Also, as a general rule, you should avoid use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.
Much better would be to use nested JPanels, each using its own layout manager, and then calling pack() on your JFrame after adding all components but prior to displaying it.
Never call setBounds(), setSize(), setPreferredSize() or any similar call on a JTextArea that goes inside of a JScrollPane as it will prevent the JScrollPane from working correctly within your JScrollPane due to your setting bounds. This will prevent the JTextArea from expanding when more lines are added, sometimes preventing display of the scrollbars, or if their displayed, preventing them from working properly. Better to set the JTextArea's viewable columns and rows via one of its constructors that has int parameters.
There's no need to call repaint() after setting the text of a JTextComponent such as a JTextArea, since the textarea's model will notify its view (the part that is rendered on the GUI) of changes, and the view will then automatically call repaint itself.
It was hard to word the title for this one, but here's the explanation:
I have a menu bar that I'm adding as an external object from my MenuBar.java that extends JMenuBar to my main program file APP.java that extends JFrame.
MenuBar and a JPanel (which is in my main program file, APP.java) are added to the JFrame. How do I make buttons from the MenuBar perform actions on the JPanel.
Here's how my JMenuItem objects look like right now in MenuBar.java:
item = new JMenuItem("New);
item.setMnemonic(KeyEvent.VK_N);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
ActionEvent.ALT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JLabel block = new JLabel();
block.setPreferredSize(new Dimension(120, 160));
//***This is where I run into a problem... I want to add this JLabel to my JPanel in
// the main file, and I also want to revalidate/repaint the JPanel to take show
// the new JPanels as they're added.....
}
});
file.add(item);
I'm not sure if I need to extend my APP to implement ActionListener.... but then I am not sure what to do afterwards.
EDIT:
Well, I was able to perform the intended action by making my content panel public and static, thus making it available without instantiating the APP object. And then I was able to implement this code into my actionPerormed methods in ActionListeners:
APP.content.add(new Thumb());
APP.content.validate();
Thumb() method creates a new JLabel;
Hopefully this won't mess up my stuff later on down the line, being that my content panel is static now.
This is hard to answer.
I would use setAction(Action) (indirectly). One can make an Action as child of AbstractAction, and an Action can hold its text, an icon, mnemonic key and more.
One typical usage is a JTextPane that provides a Action[] getActios() and those actions might be added to the menu bar or a JToolBar.
Please look up some code samples.
I leave it at this half of an answer.
An intro.
It would depend on what actions you want to perform, but the overall solution is the same. You need to pass a reference of the object of the object you want to work with to the menu class.
If you can, its better to pass a model of interace, limiting your actions to only performing work you really want them to
I'm suppose to create a game using java app. I have a few JLabel with images in a JPanel and I would like to link these JLabels from a JPanel to different JPanel. Is it possible to do so? As in when the Jlabel is being clicked, another page will appear.
Thanks in advance.
Set the cursor for the label to Cursor.HAND_CURSOR.
Add a MouseListener to each label.
On mouseEntered(), set the color of the link text to a different color to high-light it (like a browser would).
On mouseClicked(), change cards in a CardLayout to show the other components, or otherwise reveal the other components.
Swing components can't be shared as they can only have a single parent.
However you can share the Icon of the label with another Swing component. So in your MouseListener you can use the getIcon() method of the label you clicked on. Then you can add the icon to another component is the second panel using the setIcon(...) method.
You have to be more specific. You need to include things like what technologies you're using. Is this a web based project? Is it a stand alone application? And what specifically you're trying to do. And "link" has too broad of a meaning. That could mean a lot of different things. You have to be more specific in order to get the proper help.
You can share the JLabel by making it a singleton like class.
public class SharedJLabel {
private static JLabel imageLabel;
static {
//init the imageLabel here
}
public static JLabel getImageLabel() {
return imageLabel;
}
}
In your different JPanel classes, you can just use this class to use the shared JLabel:
SharedJLabel.getSharedImageLabel();
I'm trying to extend a class to eventually make a custom button. I read in several places that it's best to extend as high as possible on the heirarchy (for better polymorphism?), so I'm trying to extend JComponent:
import javax.swing.*;
import java.awt.*;
public class TestButton extends JComponent {
private static final long serialVersionUID = 1L;
public TestButton() {
super();
}
}
And the code that calls this is:
b1 = new TestButton();
basePanel.add(b1,gbc); // (gbc is GridBagConstraints object)
The thing is, my JComponent isn't displayed in my layout. If I extend the class as JButton, it shows no problem. What's the deal?
Update:
FYI, this is sort of a noob conceptual question, I'm far from proficient here obviously.
Here's a picture to describe. The only thing changed is extends ______.
What should be happening is a purple-filled block, the same height as the yellow block on the bottom.
What is happening is a default sized block that has no background (the black is from the JFrame).
One of the main differences between JComponent and it's subclasses is that the latter have UI delegates, while JComponent does not. Note that the setBackground() "color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations." As a result, you "must override paintComponent() to honor this property."
What do you expect to see in the place where JComponent is supposed to be? when you extend JButton, you get all its graphic with it, but you created an empty component, with nothing in it. Try putting something in the component (such as a JLabel or similar)
You still need to extends JButton if you want JButton's functionality. Otherwise everybody can extend Object and expect everything.
It is a blank component (basically a template). It has no properties. You have to add your own graphical elements by overriding the paintComponent method and then add logical elements by overriding the update method.
Either use the JButton as it is:
JButton b1 = new JButton("Enter JButton text here");
basePanel.add(b1,gbc);
or extend the JButton class if you wish to change certain properties and customize it for your own preferences:
public class TestButton extends JButton {
private static final long serialVersionUID = 1L;
public TestButton(String buttonText)
{
super(buttonText);
}
}