Rich swing radiobutton - java

Developing a desktop application based on Java + Swing I faced the problem of creating a radio button which instead of text next to it, should have and image or, say, another widget like a spinner.
Clicking on the image or the spinner should select also the corresponding radioButton.
Is it possible? if so, how?

To me, the JRadioButton with icon given for constructor doesn't seem to work; it replaces the "native radio button icon" with given icon. I think to original asked wanted for radio button with icon in addition to the "radio button icon".
There has been some debate on the behaviour at Sun bug database with Bug #4177248 but no changes have been made.
Instead, one could try JRadioButtonMenuItem, even though there will probably be some non-wanted behaviour with that?
Short evaluation for both JRadioButton and JRadioButtonMenuItem:
public class IconRadioButtonEval {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
// Use some arbitrary working URL to an icon
URL url =
new URL(
"http://mikeo.co.uk/demo/sqlspatial/Images/RSS_Icon.png");
Icon icon = new ImageIcon(url);
JRadioButton button = new JRadioButton(icon);
panel.add(new JLabel("RadioButton with icon:"));
panel.add(button);
panel.add(new JLabel("RadioButtonMenuItem with icon:"));
panel.add(new JRadioButtonMenuItem(icon));
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Justin's suggestion for another component next to JRadioButton with empty string should probably work well in most cases.

Well, there is a constructor for JRadioButton that takes an Icon. You can use that to make it have an icon next to it.
JRadioButton(Icon icon)
-- Creates an initially unselected radio button with the specified image but no text.
Otherwise, it is fairly easy to make the JRadioButtons text empty, and place another component next to it. Then you will need to add a Listener to that component, so that the JRadioButton gets clicked if the other component gets clicked.

Related

How to navigate to previous card with button in card layout?

I have created a Java application in Netbeans, and used CardLayout to make three cards, which appear when I click three buttons.
All that is fine but I want to make a 'back' icon that, when clicked on, brings the previous card, so that if I am in the third card, clicking on the 'back' icon brings the second card, and from the second card to the first and so on.
The problem is that I want the program to know that we are in the second card for example, so clicking the 'back' icon brings the first card.
Also the back icon is on another panel in the same JFrame. I hope someone helps!
The icon on the lower left is the back button
and cards are located inside-the white portion.
I suppose somewhere in your project you already have the following code pieces:
For building the panel on the right (the one with the CardLayout):
JPanel panel1 = ...;
JPanel panel2 = ...;
JPanel panel3 = ...;
JPanel rightPanel = new JPanel();
CardLayout cardLayout = new CardLayout();
rightPanel.setLayout(cardLayout);
rightPanel.add(panel1);
rightPanel.add(panel2);
rightPanel.add(panel3);
and for building the "back" button (the one with the <- icon):
JButton backButton = ...;
Then all you need to add is the following:
backButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
cardLayout.previous(rightPanel);
}
});
or equivalently, if you prefer the concise lambda-syntax of Java 8:
backButton.addActionListener(e -> cardLayout.previous(rightPanel));

How to add button in JTabbedPane background?

I want to add button in my JTabbedPane background like Google Chrome so that every time I can add new tabs by clicking it.
How can I do it? Thanks in advance!
EDIT: I have taken an undecorated JFrame.
Look into the JTabbedPane.setTabComponentAt( int index, Component component ) method. This method allows you to set the component with which to render the title.
Description from documentation:
Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.
Note: The component must not be one that the developer has already added to the tabbed pane.
What you can do:
Create the JTabbedPane
Add a new tab to it, its intended function like the chrome "add tab page"
Set the title component of that tab to a button (style it appropriately)
When that button is clicked, add a new tab right before the button tab and show the newly added tab
This code will create only one tab and button to it.
class Test extends JFrame
{
JTabbedPane jtab;
JButton but;
JPanel panel;
Test()
{
super("JTabbedPane");
jtab=new JTabbedPane();
but=new Button("Click");
panel=new JPanel();
panel.add(but);
jtab.add("Tab",panel);
add(jtab);
setVisible(true);
setSize(400,400);
}
public static void main(String[] args)
{
new Test();
}
}

Java custom tooltip for minimize button

I am creating a JFrame in the following way.
JFrame f=new JFrame("Title");
f.setUndecorated(true);
f.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
However, I am trying to display a custom tooltip text for the JFrame (by default it shows "Iconify"). Please help me out.
Thanks !
One way add some panel to frame with tooltip you needed:
JPanel panel = new JPanel();
panel.setToolTipText("Message");
frame.add(panel, BorderLayout.CENTER);
Another way if you have custom minimize button:
JButton button = new JButton() {
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
If you want to change text of minimize button in L&F, you can set InternalFrameTitlePane.minimizeButtonText value if UIDefaults.
UIManager.getDefaults().put("InternalFrameTitlePane.minimizeButtonText", "my text");
It can be used as a solution if you definitely know which L&F your application will use.

In JAVA, JButton, Button displayed only when cursor is on the button and works even on clicking anywhere in contentPane

I am a beginner into Java and OOPS in general. Am studyin Head First Java to start, and studying GUI and Swing concepts in it.
The below code is just for understanding purposes.
On running the code, The frame window is displayed with Button, and when I expand it I can see Radio Button too.
Issues-
Button works till the window size is not more than the button size . As soon as I increase the window size even slightly more than button's dimensions, then the button is displayed only when cursor is on it.
I am changing window size using mouse.
Also even if I set Frame size to be more than button. say frame.setSize(800,800); then the button covers whole contentPane. and still behaves same way on resizing.
And the button responds to clicking on mouse, irrespective of where I click in the contentPane. It should respond only when i click directly on the button.
Please inform me why it is behaving this way.
And if possible,corrections in code or additions to correct this.
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
public class Test1 implements ActionListener {
JFrame frame = new JFrame("Frame");
JButton button = new JButton("Button!");
JRadioButton radio = new JRadioButton("VideoKilledTheRadioStar!",true);
int j=0;
public static void main(String[] args) {
Test1 t = new Test1();
t.method1();
}
public void method1()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setSize(100,100);
button.setBackground(Color.ORANGE);
frame.add(button);
frame.setSize(100,100);
frame.setVisible(true);
button.addActionListener(this);
frame.getContentPane().add(radio);
radio.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{j++;
button.setText("clicked .. " + j);
if(button.getBackground()==Color.ORANGE)
button.setBackground(Color.BLUE);
else
button.setBackground(Color.ORANGE);
}
}
P.S I did not know which segment of code is important or more relevant to this question, so I have put complete code.
You are trying to add the JButton button and JRadioButton objects in the default layout(BorderLayout) of the JFrame.
Whenevery you add a component to JFrame having BorderLayout the components goes in the Middle Section and BorderLayout center section has tendency to occupy the complete space, so to position elements properly you will need to specify the location as well as set the PreferredSize of the component.
frame.add(radio, BorderLayout.SOUTH);
component.setPreferredSize(Dimension);
You are adding the JButton button and the JRadioButton both in the BorderLayout.CENTER location so only one is being displayed. Components at this location will be sized in the X and Y axis.
The JButton only displays when the cursor is over it due to the fact that it has its own MouseListener used for painting.
Also, the statements
frame.add(myComponent);
and
frame.getContentPane().add(myComponent);
both add the component to the frame's ContentPane & are equivalent but the first is chosen for convenience.
Note that components cannot co-exist in the same position in a BorderLayout. You could place the button at the BorderLayout.SOUTH position (& add directly to the frame):
frame.add(radio, BorderLayout.SOUTH);
BorderLayout disregards any preferred sizes for components so you would have to use a different layout manager such as BoxLayout to maintain a fixed size JButton.
See more about Layout Managers

performing multiple actions on one button in java swing

I am trying to create a wizard in java swing programatically.
On the wizard pane I have a next button and it has to perform multiple actions according to which panel is displayed on the wizard.
Is it possible to use java command pattern? may I know how?
thanks in advance.
the code i used for the wizard is
this.mainPanel.add(fileSelectionPane,"SELECT FILE");
this.mainPanel.add(sqlConnectionPane,"SQL CONNECTION");
this.mainPanel.add(devicePane,"PARSER");
this.mainPanel.add(detailsPane,"DISPLAY");
thisLayout.show(this.mainPanel,"SELECT FILE");
this.finishButton.setEnabled(false);
this.backButton.setEnabled(false);
if(newValue==1) {
this.thisLayout.show(this.mainPanel, "SQL CONNECTION");
this.nextButton.setEnabled(true);
this.nextButton.setText("Connect..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
if(newValue==2) {
this.thisLayout.show(this.mainPanel, "PARSER");
this.nextButton.setEnabled(true);
this.nextButton.setText("Parse..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
i want the next button to perform specific actions on SELECT FILE and SQL CONNECTION.
is it possible to use command patterns?
Ok, so, You add action listeners to buttons. These action listeners do something when an event occurs.
You want to change the functionality of the button depending on which panel is being displayed? Why not set a instance variable which reflects the state of the Wizard?
For example (roughly),
int state = 0; // home panel
change panel to help page, event listener is fire, set 'state' to 1. You are now tracking which panel is being displayed.
Now, in your original problem, when the button (the one you want multiple functionality with) fires, you can choose the action it will take based on the 'state' var.
have look at CardLayout
those cards put to the JDialog (JDialog has preimplemented by default BorderLayout) to the CENTER area
create a new JPanel and place there JButtons
JPanel with JButtons put to the SOUTH area
search here, on this forum, there are a few excelent examples for wizard or image previue based on CardLayout
try the following code for button:
JButton btn1;
btn1= new javax.swing.JButton();
btn1.setToolTipText("Submit");
btn1.setContentAreaFilled(false);
btn1.setBorderPainted(false);
btn1.setMargin(new java.awt.Insets(2, 2, 2, 2));
btn1.addActionListener(this);
btn1.setIcon(this.getIcons()[21]);
add(btn1); // add to Jpanel
btn1.setBounds(250,10, 12, 12);
public void actionPerformed(java.awt.event.ActionEvent evt) {
Object obj = evt.getSource();
if (obj == btn1) {
// your function on on click of button
return;
}

Categories