How I can define a JRadioButton with large size? - java

I'm learning java. for my GUI program need to large radio buttons (larger than the standard). What can I do?
I use Java Netbeans IDE - the latest version.

You can supply you're own images for radio button, see JRadioButton#setIcon, JRadioButton#setSelectedIcon and How to Use Buttons, Check Boxes, and Radio Buttons for more details...
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class RadioButtonTest {
public static void main(String[] args) {
new RadioButtonTest();
}
public RadioButtonTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
try {
BufferedImage checked = ImageIO.read(getClass().getResource("/Checked.png"));
Image unchecked = ImageIO.read(getClass().getResource("/Unchecked.png")).getScaledInstance(300, 300, Image.SCALE_SMOOTH);
JRadioButton btn = new JRadioButton("I'm not fat, I'm just big boned");
btn.setSelectedIcon(new ImageIcon(checked));
btn.setIcon(new ImageIcon(unchecked));
btn.setHorizontalTextPosition(JRadioButton.CENTER);
btn.setVerticalTextPosition(JRadioButton.BOTTOM);
setLayout(new GridBagLayout());
add(btn);
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}

Related

Making a slideshow program

I am trying to make a slideshow program. I want the next slide to appear when I click (There are only two slides not, but I will add more once the errors r sorted out).
The code compiles fine. But when i click, nothing happens.
What could possibly go wrong?
package project;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Frame extends JFrame{
Frame() {
setLayout(new BorderLayout());
ImageIcon slide = new ImageIcon("E:\\Books\\Computer\\Java\\Introduction to Java Programming\\exercise9e\\image\\slide0.jpg");
JLabel slidesLabel = new JLabel(slide);
add(slidesLabel,BorderLayout.CENTER);
slidesLabel.addMouseListener(new ClickListener());
}
public void nextSlide() {
ImageIcon slide = new ImageIcon("E:\\Books\\Computer\\Java\\Introduction to Java Programming\\exercise9e\\image\\slide1.jpg");
JLabel slidesLabel = new JLabel(slide);
add(slidesLabel,BorderLayout.CENTER);
System.out.println("x");
}
public static void main(String args[]) {
Frame frame = new Frame();
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class ClickListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
nextSlide();
}
}
}
So, the "main" problem is BorderLayout will only manage a single component within any of the five available locations it manages.
Adding another component into the position tends to cause issues, where the component that was first added won't be displayed, or in your case, will remain and could interfere with the new component
"A" solution would be to re-use the same JLabel for each slide, simply supply a new value for the icon property (or in this example, the text property)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Frame extends JFrame {
private JLabel slidesLabel = new JLabel("Apple");
public Frame() {
setLayout(new BorderLayout());
add(slidesLabel, BorderLayout.CENTER);
slidesLabel.addMouseListener(new ClickListener());
}
public void nextSlide() {
slidesLabel.setText("Banana");
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ClickListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
nextSlide();
}
}
}
This approach would allow you to place each icon into an array and simply have a counter which determines which slide is current, so when you click for the next slide, you simply increment the counter, get the next value from the array and apply it to the label
A better (and more appropriate) solution would be to actually use a CardLayout, see How to Use CardLayout for more details
With icons...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Frame extends JFrame {
private JLabel slidesLabel = new JLabel();
private Icon[] icons;
private int currentSlide = -1;
public Frame() {
try {
// Personally, I'd use File#listFiles to list all the
// images in a directory, but that might be consider
// using our initiative...
icons = new Icon[]{
new ImageIcon(ImageIO.read(new File("..."))),
new ImageIcon(ImageIO.read(new File("..."))),
new ImageIcon(ImageIO.read(new File("...")))
};
slidesLabel.setVerticalAlignment(JLabel.CENTER);
slidesLabel.setHorizontalAlignment(JLabel.CENTER);
setLayout(new BorderLayout());
add(slidesLabel, BorderLayout.CENTER);
slidesLabel.addMouseListener(new ClickListener());
nextSlide();
} catch (IOException exp) {
exp.printStackTrace();
}
}
public void nextSlide() {
if (currentSlide < icons.length - 1) {
currentSlide++;
slidesLabel.setIcon(icons[currentSlide]);
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ClickListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
nextSlide();
}
}
}
Why simply adding components to a BorderLayout is bad...
All I did was when nextSlide was called, create a new JLabel assign it the next icon and add it to the Frame (which is using a BorderLayout) and then re-sized the frame. Because the labels are transparent, they remain visible...
This is why you shouldn't simply add new components to a BorderLayout, but, in your case, simply update the properties of the existing JLabel to meet the changing needs.

How to untoggle jtogglebuttons on second click that belongs to ButtonGroups

How to untoggle jtogglebuttons that belongs to ButtonGroups on second click? What listener should I use for this? Thanks!
JtoggleButton toggleButton;
toggleButton.addActionListener?
toggleButton.addChangeListener?
How to untoggle jtogglebuttons that belongs to ButtonGroups on second click? What listener should I use for this?
None, the ButtonGroup will take care of it automatically, that's the point. The ButtonGroup will only allow a single button to be selected within the group at a time.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public JavaApplication243() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
ButtonGroup bg = new ButtonGroup();
JToggleButton btn1 = new JToggleButton("One");
JToggleButton btn2 = new JToggleButton("Two");
bg.add(btn1);
bg.add(btn2);
add(btn1);
add(btn2);
}
}
}
public class CustomButtonGroup extends ButtonGroup {
#Override
public void setSelected(ButtonModel model, boolean selected) {
if (selected) {
super.setSelected(model, selected);
} else {
clearSelection();
}
}
}

Can we put combobox, radio button, text field, table on text area?

I need this type of functionality. Like in MS Word, we choose table in menu bar and then we draw in our sheet.
So how it can be done in Java? I thought for sheet I can use JTextArea.
No (to a JTextArea), but you should be able to do it using a JTextPane, see JTextPane#insertComponent for more details
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextPane tp = new JTextPane();
Document doc = tp.getDocument();
try {
tp.insertComponent(new JTextField("Hello world"));
doc.insertString(doc.getLength(), "\n", null);
tp.insertComponent(new JComboBox(new String[]{"Banana", "Apple", "Grape"}));
doc.insertString(doc.getLength(), "\n", null);
tp.insertComponent(new JRadioButton("Option A"));
doc.insertString(doc.getLength(), "\n", null);
tp.insertComponent(new JTable(new DefaultTableModel(10, 5)));
doc.insertString(doc.getLength(), "\n", null);
} catch (BadLocationException exp) {
exp.printStackTrace();
}
add(new JScrollPane(tp));
}
}
}

Java - is it possible to apply setEditable(boolean b) to JSlider/

in my program I need to disable my JSlider under certain circumstances, but do not know how. I tried setFocusable(false) but that did not work... Thanks in advance!
You change/restrict user interactions with the JSlider through the use of the enabled property.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SliderTest {
public static void main(String[] args) {
new SliderTest();
}
public SliderTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JSlider slider = new JSlider();
final JCheckBox checkBox = new JCheckBox();
checkBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
slider.setEnabled(checkBox.isSelected());
}
});
checkBox.setSelected(true);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(slider, gbc);
frame.add(checkBox, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Try the setEnabled(boolean) method. All JComponents inherit it by default.

Java mac add notifications to the dock icon (1), (2), etc

Im trying to figure out how i can add number notifications to my dock icon when a new message is received (I've got a small chat app)
This is what i mean:
Any ideas how to accomplish this?
Try taking a look at com.apple.eawt.Application (I'm having a hard time finding JavaDocs)
import com.apple.eawt.Application;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestBadge {
public static void main(String[] args) {
new TestBadge();
}
public TestBadge() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
private int count;
public TestPane() {
setLayout(new GridBagLayout());
add((label = new JLabel("0")));
Timer timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText(Integer.toString(count));
Application.getApplication().setDockIconBadge(Integer.toString(count));
}
});
timer.start();
}
}
}
I should point out, that if you're running in an IDE capable of inspecting the installed classes, you should be able to get a list of the functionality provided by the Application class. I was using NetBeans and was able to find a listing of all the methods, problem is, some were documented and some weren't :P

Categories