Align icon of JButton with JButton - java

I am working with a JButton and wanted to add an icon image to it.
When I went to add an image the image has an offset to the left for some reason.
The image is exactly the same size as the button but it is slightly moved to the left for some reason. (The offset is about 13 pixels)
Here is the code from the button:
private JButton getBtnReset() {
if (btnReset == null) {
btnReset = new JButton("Reset");
btnReset.setIcon(new ImageIcon(FrontEnd.class.getResource("/resources/Reset1.png")));
btnReset.setFont(new Font("OptimusPrinceps", Font.BOLD, 17));
btnReset.addActionListener(new BtnResetActionListener());
btnReset.setBounds(400, 11, 167, 69);
}
return btnReset;
}

Related

Remove border from JButton

I would have a problem with the edges of my jbutton. In practice, in the code below I inserted a button that should not have edges but instead appear as in the photo below.
JButton btnRes = new JButton();
btnRes.setBorderPainted(false);
btnRes.setContentAreaFilled(false);
btnRes.setOpaque(false);
btnRes.setBorder(null);
btnRes.setIcon(new ImageIcon(Main.class.getResource(image1)));
btnRes.setPressedIcon(new ImageIcon(Main.class.getResource(image2)));
btnRes.setRolloverIcon(new ImageIcon(Main.class.getResource(image3)));
btnRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//TODO
}
});
btnRes.setBounds(496, 342, 138, 48);
frame.getContentPane().add(btnRes);
and this is the result:
Image of this JButton
The border is however visible, how I can fix this?
You see the border that is added to the button because it is selected. Try:
btnRes.setFocusPainted(false);

I tried to put a JLabel on a JButton but when I put my mouse pointer on the label it disappears

Here I implement a JButton:
btnTotalCall = new JButton("Total calls");
btnTotalCall.setFocusPainted(false);
btnTotalCall.setVerticalAlignment(SwingConstants.TOP);
btnTotalCall.setForeground(Color.BLACK);
btnTotalCall.setBackground(Color.WHITE);
btnTotalCall.setBounds(302, 39, 125, 125);
contentPane.add(btnTotalCall);
Here a JLabel:
lblTotalCall = new JLabel("");
lblTotalCall.setHorizontalAlignment(SwingConstants.CENTER);
lblTotalCall.setBackground(Color.WHITE);
lblTotalCall.setForeground(Color.BLACK);
lblTotalCall.setFont(new Font("Tahoma", Font.BOLD, 20));
lblTotalCall.setBounds(302, 39, 125, 125);
contentPane.add(lblTotalCall);
Here in a MainController I tried to update lblTotalCall with my database all the seconds:
public MainController(MainView mainView)
{
this.mainView = mainView;
ActionListener timerListener = new ActionListener()
{
DAO<Call> call_dao = new CallDAO();
List<Call> call_list;
int call_time;
DAO<User> user_dao = new UserDAO();
List<User> user_list;
public void actionPerformed(ActionEvent e)
{
// Put the current date and time in the label date and update it
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat heureFormat = new SimpleDateFormat("HH:mm:ss");
Date cal = Calendar.getInstance().getTime();
mainView.getLblDate().setText("<html>" + dateFormat.format(cal).toString() + "<br>" +
heureFormat.format(cal) + "</html>");
call_list = call_dao.recupAll();
// Update the lblTotalCall
if(call_list.size() > 0)
{
mainView.getLblTotalCall().setText(Integer.toString(call_list.size()));
}
else
{
mainView.getLblTotalCall().setText("0");
}
for (Call call : call_list)
{
if (call.getDate() == cal && mainView.getRdbtnToday().isSelected())
{
call_time = call_time + call.getDuration();
call_time = call_time / call_list.size();
}
}
user_list = user_dao.recupAll();
if(user_list.size() > 0)
{
mainView.getLblOperators().setText(Integer.toString(user_list.size()));
}
else
{
mainView.getLblOperators().setText("0");
}
}
};
Timer timer = new Timer(1000, timerListener);
// to make sure it doesn't wait one second at the start
timer.setInitialDelay(0);
timer.start();
}
I've already tried to put a default value in the JLabel, and change the value of the properties focusable and enabled to false but that's not fix it.
I launch the application, (sorry I can't post images)
I see the right data but when I put my mouse pointer on the btnTotalCall or btnOperators the labels disapear.
I tried to put a Jlabel on a Jbutton
All I see is:
contentPane.add(btnTotalCall);
...
contentPane.add(lblTotalCall);
Which means you are adding two components to a panel, not adding a label to a button.
but when i put my mouse pointer on the jlabel is disappear
By default Swing assumes components do not overlap and when you move the mouse over the button the button needs to be repainted to paint the mouse over effect of the button, so the entire button is repainted and you no longer see the label.
If you want to paint two components in the same place then you need to override the isOptimizedDrawingEnabled() method of the panel to return false to make sure both components are repainted.
See: Overlap Layout for a layout manager that will allow you to stack multiple components on top of one another, once you override the above method.
Or a simpler approach is to add the label to the button:
JButton button = new JButton("Total Calls");
button.setVerticalAlignment(SwingConstants.TOP);
button.setLayout( new BorderLayout() );
JLabel center = new JLabel("0");
center.setHorizontalAlignment(SwingConstants.CENTER);
button.add(center, BorderLayout.CENTER);
This is not a true layout manager as the size of the button will only be determined by the properties of the button, not the label added to the button.
Another option is to use HTML for the text of the button:
JButton button2 = new JButton("<html>Total Calls<br><br><center>0</center></html>");

JRadio Buttons and images

I am currently teaching myself the java language and I now learing about GUI's by using eclpise, JFrame, and window builder. I have created a GUI with two radiobutton and above them I have two labels each with a picture on them. When I click on the radiobutton on the left I want the left label to display the image and the right label to display nothing. And then when I click on the right radio button I want the label on the right to display the image. I am stuck and i've tried a few different things and was hoping for some guidance. Here is some of the code so far
JLabel lblLeft = new JLabel("");
//This is to retrieve your image and put it in the GUI
Image imgL = new ImageIcon(getClass().getResource ("schlange.gif")).getImage();
lblLeft.setIcon(new ImageIcon(imgL));
lblLeft.setPreferredSize(new Dimension(290, 26));
lblLeft.setOpaque(true);
lblLeft.setBackground(Color.BLACK);
contentPane.add(lblLeft, BorderLayout.WEST);
JLabel lblRight = new JLabel("");
//This is to retrieve your image and put it in the GUI
Image imgR = new ImageIcon(getClass().getResource("schlange.gif")).getImage();
lblRight.setIcon(new ImageIcon(imgR));
lblRight.setPreferredSize(new Dimension(290, 26));
lblRight.setBackground(Color.BLACK);
lblRight.setOpaque(true);
contentPane.add(lblRight, BorderLayout.EAST);
//To get this code to come up you click on your radio button then right click on it go down to add event
//then go to item and itemStateChanged
JRadioButton rdbtnLeft = new JRadioButton("Left");
rdbtnLeft.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0)
{
lblLeft.setIcon((Icon) imgL);
}
});
buttonGroupLeftRight.add(rdbtnLeft);
rdbtnLeft.setFont(new Font("Tahoma", Font.PLAIN, 12));
panel.add(rdbtnLeft);
JRadioButton rdbtnRight = new JRadioButton("Right");
buttonGroupLeftRight.add(rdbtnRight);
rdbtnRight.setToolTipText(" ");
rdbtnRight.setFont(new Font("Tahoma", Font.PLAIN, 12));
panel.add(rdbtnRight);
Wrap each image in its own JPanel (leftImagePanel and rightImagePanel).
Then you can use:
leftImagePanel.setVisible(true);
rightImagePanel.setVisible(false);
or
leftImagePanel.setVisible(false);
rightImagePanel.setVisible(true);
Wherever you need.

color of JButton

After reviewing many previous StackOverflow posts, I am still unable to get my JButton to be black instead of the default color. Here is what my button looks like:
And here's my code:
public void setStartButton() {
JPanel panel = this.jPanel1;
panel.setLayout(null);
JButton button = new JButton("START");
// size and location of start button
int res = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
int length = (int) Math.floor(10.0*res/25.4); // side lengths of square button
int offset = length/2; // because button should be centered...but its x and y location are given by its upper left hand corner
button.setBounds(centerX-offset, centerY-offset, length, length);
// color of start button
button.setBackground(BLACK);
button.setOpaque(true);
button.setContentAreaFilled(false);
// font
button.setFont(new Font("Arial", Font.PLAIN, 8));
button.setVisible(true);
panel.add(button);
}
By the way, when I change setContentAreaFilled to true, it makes no difference.
I know that the function is indeed being called, because the location and font info for my button is working just fine.
Any assistance would be much appreciated! Thanks!
A JButton is made of a series of layers, including the content, border and focus layers. Depending on what you're trying to do, you may need to remove all of them, for example...
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
setStartButton();
}
public void setStartButton() {
JButton button = new JButton("START");
button.setMargin(new Insets(20, 20, 20, 20));
// color of start button
button.setOpaque(true);
button.setContentAreaFilled(true);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setBackground(BLACK);
button.setForeground(WHITE);
// font
button.setFont(new Font("Arial", Font.PLAIN, 8));
add(button);
}
}
I'd also strongly encourage you to consider making use of an appropriate layout manager and using the properties of it and the JButton to generate the desired padding you need, these will work together with the font metrics, which tend to be different between systems, to generate an appropriate size for the button

ActionListener - Radio buttons

I'm trying to get a program working where you press the specific radio button that says a color on it, and it makes the entire page that color. The window opens as a square with 4 different color options in it. I just can't for the life of me get the actionPerformed method working. Here is my code. Any help is appreciated.
public class ColorPanel3 extends JPanel implements ActionListener {
Color darkBlue = new Color(5,41,186);
Color lightBlue = new Color(35,253,253);
Color darkRed = new Color(158,19,47);
Color lightRed = new Color(255,105,105);
JRadioButton lightRedButton = new JRadioButton("Light Red");
JRadioButton darkBlueButton = new JRadioButton("Dark Blue");
JRadioButton lightBlueButton = new JRadioButton("Light Blue");
JRadioButton darkRedButton = new JRadioButton("Dark Red");
public ColorPanel3() {
setName("Color Panel");
setLayout(new GridLayout(1, 0, 0, 0));
JPanel panel = new JPanel();
add(panel);
panel.setLayout(null);
panel.setOpaque(true);
lightRedButton.setHorizontalAlignment(SwingConstants.CENTER);
lightRedButton.setBounds(0, 150, 150, 150);
lightRedButton.setBackground(lightRed);
panel.add(lightRedButton);
darkBlueButton.setHorizontalAlignment(SwingConstants.CENTER);
darkBlueButton.setBounds(0, 0, 150, 150);
darkBlueButton.setBackground(darkBlue);
panel.add(darkBlueButton);
lightBlueButton.setHorizontalAlignment(SwingConstants.CENTER);
lightBlueButton.setBounds(150, 0, 150, 150);
lightBlueButton.setBackground(lightBlue);
panel.add(lightBlueButton);
darkRedButton.setHorizontalAlignment(SwingConstants.CENTER);
darkRedButton.setBounds(150, 150, 150, 150);
darkRedButton.setBackground(darkRed);
panel.add(darkRedButton);
ButtonGroup group = new ButtonGroup(); //creates a button group so that only one radio button may be pressed at a time.
group.add(darkBlueButton);
group.add(lightBlueButton);
group.add(darkRedButton);
group.add(lightRedButton);
lightRedButton.addActionListener(actionListener);
darkRedButton.addActionListener(actionListener);
lightBlueButton.addActionListener(actionListener);
darkBlueButton.addActionListener(actionListener);
}
ActionListener actionListener = new ActionListener(){
public void actionPerformed (ActionEvent e){
if(darkBlueButton.isSelected()){
darkBlueButton.setBackground(darkBlue);
lightBlueButton.setBackground(darkBlue);
lightRedButton.setBackground(darkBlue);
darkRedButton.setBackground(darkBlue);
}
if(lightBlueButton.isSelected()){
darkBlueButton.setBackground(lightBlue);
lightBlueButton.setBackground(lightBlue);
lightRedButton.setBackground(lightBlue);
darkRedButton.setBackground(lightBlue);
}
if(darkRedButton.isSelected()){
darkBlueButton.setBackground(darkRed);
lightBlueButton.setBackground(darkRed);
lightRedButton.setBackground(darkRed);
darkRedButton.setBackground(darkRed);
}
if(lightRedButton.isSelected()){
darkBlueButton.setBackground(lightRed);
lightBlueButton.setBackground(lightRed);
lightRedButton.setBackground(lightRed);
darkRedButton.setBackground(lightRed);
}
}
And I made a new class file to put the panel in. This worked.
public class ColorFrame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new ColorPanel3());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Color Frame");
}
}
};
}
Final edit: the above code is what I ended up with.
Your action listener should look like this:
ColorPanel3.setOpaque(true);
ColourPanel3.this.setBackground(yourcolor);
And moreover, if you are not writing this code for a code obfuscation contest please use different actionlisteners for all the radio buttons.
Cheers.
Edit: You will have to add the panel to a JFrame
First of all, get rid of these lines:
JRadioButton darkBlueButton = (JRadioButton) e.getSource();
JRadioButton lightBlueButton = (JRadioButton) e.getSource();
JRadioButton darkRedButton = (JRadioButton) e.getSource();
JRadioButton lightRedButton = (JRadioButton) e.getSource();
What is happening is that you are creating four JRadioButton instances with a reference to the JRadioButton given by e.getSource(). If you want the background to change color, then this isn't necessary. I get the feeling that the effect of this isn't what you intended to do, anyway.
When creating your JRadioButton objects with the constructor that takes a String, you're giving it a label (i.e. Dark Blue), along with an action command, which is the same as the text. You can take advantage of this by using the getActionCommand method of the ActionEvent class to determine what color the JPanel should be:
if (e.getActionCommand().equals("Dark Blue")) {
ColorPanel3.this.setBackground(darkBlue);
}
But in order for any color change to be apparent, you must set the transparency of the top panel:
panel.setOpaque(false);

Categories