How to stop text going grey when disabling a button? - java

My current java version is:
$java -version
java version 1.8.0_40
I'm very new to swing and trying to write my first minesweeper app. Now, I need to disable a button when clicking on it and print a number of adjacent mines (with the appropriate text color depending on a number: 1- blue, 2 - green, 3 - red, etc). So as suggested in this answer I wrote the following:
JFrame frame = new JFrame("Hello swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JButton button = new JButton();
button.addActionListener((ActionEvent ev) -> {
button.setSelected(true);
button.setText("<html><font color = red>3</font></html>");
button.setEnabled(false); });
But it still goes grey when disabling:
But enabled button look just as I want it to:
What to do with this issue? How to fix this problem with disabling buttons?

Maybe you know some workarounds
Use Icons instead of text.
Then you can use:
JButton button = new JButton( icon3 );
button.setDisabledIcon( icon3 );

Related

JButton Default Selection Color

I want to change the default color when selecting a JButton. Per default it is "lightblue". When pressing a JButton the background changes for some 0.2 seconds to lightblue.
I don't want to setBackground(). I want to change the default property. Is there something like:
JButton button = new JButton(text);
button.setSelectionColor(Color);
This is controlled by the LAF.
You might be able to use:
UIManager.put("Button.select", new Color( 0, 255, 0) );
at the start of your program before you create your buttons.
However, this will apply to all buttons in your application.

Change size of JButtons

I have a very big problem. I need to do vertical menu in the center of window. What could be easier? What I did:
I create JFrame and set BorderLayout to it:
JFrame jfr = new JFrame("Frame");
Then I create 4 buttons:
JButton b1 = new JButton("b1");
JButton b2 = new JButton("b2");
JButton b3 = new JButton("b3");
JButton b4 = new JButton("b4");
I created panel and add all buttons to panel:
JPanel jpan = new JPanel();
jpan.setLayout(new BoxLayout(jpan, BoxLayout.Y_AXIS));
jpan.add(b1);
jpan.add(b2);
jpan.add(b3);
jpan.add(b4);
Aligned all buttons
b1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b4.setAlignmentX(JComponent.CENTER_ALIGNMENT);
And add panel to JFrame
jfr.add(jpan, BorderLayout.CENTER);
Please help me to understand this layouts!
Only say like this: "You should use this, when you use this layout"
And now main question: How can I change size of buttons?
There are a number of easy ways to change the size of buttons:
Give the buttons an icon of a different size.
Make the font of the buttons a different size.
Set more / less space between the button contents (the icon and text) and the border of the button.
Give the buttons more / less text.
Given the last is quite arbitrary, here is a demonstration of the first 3 techniques:

Grid layout not working?

I am trying to make a 2x2 grid layout that has a JLabel on the top left, and three buttons on the other three spaces. When I do this, I get the unexpected result of one big button (filling up the entire JDialog) that says "Do you want to push me". I don't know why this result shows up, please help, Thanks!
public void sinceyoupressedthecoolbutton() {
JDialog replacementwindow = new JDialog(); //Like a window
JButton best = new JButton("best");
JButton first = new JButton("FIRST");
JButton second = new JButton("Second");
replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
JPanel panel = new JPanel();
replacementwindow.add(panel); //adding the JPanel itself
replacementwindow.add(first);
replacementwindow.add(second);
replacementwindow.add(best);
replacementwindow.setSize(500, 500);
replacementwindow.setTitle("NEW WINDOW!");
replacementwindow.setVisible(true);
}
It's because you set the layout of your JButton, and not of your JDialog
Change
label.setLayout(new GridLayout(2,2,0,0));
to
YES.setLayout(new GridLayout(2,2,0,0));
Also, your variable called label is a JButton, you probably want to change that.
Don't add components to a button. You add components to a panel.
So the basic code should be:
JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);
Also, variable names should NOT start with an upper case character! "Yes" does not follow Java standards. The other variables do. Be consistent!

Using a function to create multiple components and change line automatically [Java]

So i lost some hours already with this and i can't seem to find a solution.
Basically i have a Jframe and inside, i have a Scrollpane and a panel
I have 1 Jlabel, 1 JTextField and 1 JButton inside that panel in a single line.
The JButton can add a new JLabel, a new JTextField and a new JButton, but i can't get them to be positioned in the next line.
I have been messing around with the layouts, but none of them fits my needs, and unfortunaly i never understand or learned how the GUI of java Works.
How's the best way to just keep adding those componentes (Jlabel, Jtextfields and Jbuttons) on a next line for every click i made?
This is my code:
private void BtnaddvariableActionPerformed(java.awt.event.ActionEvent evt) {
JLabel Lblvariablextra = new JLabel("Testing");
PanelVariable.add(Lblvariablextra);
ScrollPaneVariable.setViewportView(PanelVariable);
}
The code only contains an exemple of the label tough.
Create a main panel that is added to the scroll pane when the GUI is created:
Box main = Box.createVerticalBox();
scrollPane.setViewportView( main );
Then in the ActionListener you create a child panel contain the 3 components every time the button is pressed:
JPanel child = new JPanel();
child.add( new JLabel("I'm a label") );
child.add( new JTextField(10) );
child.add( new JButton("Click Me") );
main.add(child);
Read the section from the Swing tutorial on Layout Manager to understand how layout management works.

Can't switch between tabs having ToolTipText assigned (JTabbedPane)

I have a JFrame extended class that implements a multi-tab chat. Every tab is a chat with someone or with a group of people. What I have implemented works fine, except when I assign a ToolTipText to the label of a tab. In this case I can't click anymore (and select) the tab that has a ToolTipText assigned. The others work fine.
Graphical example:
As you can see the tabs are properly being added, and the first two tabs ("Gruppo prova" and "Gruppo test") have a ToolTipText, the other two don't. I can switch between the last two, but I can't do the same with the first two. I thought that the icon next to the label could be a problem, but I removed it and still doesn't work. However I can still click all the 'X' (close) buttons (working properly).
This is a piece of the code I used to add a tab:
// Some stuff...
JChat chat = new JChat(gui.chatClient, email, name, group);
jTabbedPane.add(email, chat); // I instantiated this before
int index = jTabbedPane.indexOfTab(email);
JPanel pnlTab = new JPanel(new GridBagLayout());
pnlTab.setOpaque(false);
// Core function
JLabel lblTitle;
if (group == 1) {
// If it's a group and not a single chat I assign a name, an icon and a ToolTipText to the tab
lblTitle = new JLabel(name, icon, JLabel.LEFT);
lblTitle.setToolTipText(membersList.toString());
} else {
// otherwise I only assign a name to the tab
lblTitle = new JLabel(name);
}
jTabbedPane.setTabComponentAt(index, pnlTab);
// This applies the 'X' (close) button next to the tab name
CloseButton btnClose = new CloseButton(this, jTabbedPane, tabs, email);
lblTitle.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
pnlTab.add(lblTitle);
pnlTab.add(btnClose);
Is this a Swing bug or am I doing something wrong?
you can use :
void setToolTipTextAt(int, String) to set tool-tip text to specific tab.
void setIconAt(int index, Icon icon) to set the icon to specific tab.
No need to use JLabel for setting tool-tip text or icon.
The above solution, however doesn't however answer your question:
except when I assign a ToolTipText to the label of a tab. In this
case I can't click anymore (and select) the tab that has a ToolTipText
assigned
The only reason i am suspecting:
JLabel doesn't register to any mouse listener by default. When no mouse listener is set to JLabel any mouse clicked event will go through to the UI objects underneath: in this case the JTabbedPane. But when we are setting tool-tip text using setToolTipText(text), the ToolTipManger adds a mouse listener to this JLabel, which will continue to consume the mouse click event.
Check the following code snippets demonstrating the issue and providing a work around setSelectedIndex function:
JLabel label = new JLabel("a Label");
System.out.println(label.getMouseListeners().length); // length is printed as 0
label.setToolTipText("Danger: setting tool tip will consume mouse event");
System.out.println(label.getMouseListeners().length); // length is printed as 1
jTabbedPane1.setTabComponentAt(0, label);
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
int index = jTabbedPane1.indexOfTabComponent((Component)e.getSource());
jTabbedPane1.setSelectedIndex(index);
}
});

Categories