I have simple GUI code as follows, in which I want to make the JButton one translucent, so that the image behind the JButton is visible!
package dealORnodeal;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Deal extends JFrame implements ActionListener
{
private Container contentPane = getContentPane();
private JButton one = new JButton("1"),two = new JButton("2");
private JMenu menu1 = new JMenu("JumpTo");
private JMenuBar bar1 = new JMenuBar();
private ImagePanel bg = new ImagePanel(new ImageIcon("bg.jpg").getImage());
public Deal()
{
super("Deal Or No Deal");
setSize(800,850);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
contentPane.add(bg);
JMenuItem item1;
item1 = new JMenuItem("Start Game");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Rules");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Credits");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Menu");
item1.addActionListener(this);
menu1.add(item1);
bar1.add(menu1);
setJMenuBar(bar1);
//GAME CODE
one.setBounds(25,151,190,49);
one.addActionListener(this);
add(one);
//GAME CODE END
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{}
}
Now how would the code be if I wanted to set the button to be translucent so that the background image would be visible through the button. BTW please don't confuse Translucent with transparent!
I can't comment to your question, so I'll answer you here..
if you use this code:
myButton.setOpaque(false);
It would not paint the button - because now it's a trasnparent.
to create the button translucent I think you should override the button paint method..
take a look at this thread
setOpaque doesn't work for JButtons, the right property is:
button.setContentAreaFilled(false);
Related
I'm having some trouble with a java project. I've made an empty GUI interface, and now I need to add some functionality to it. I'm stuck, however, on how to go about that. The basic layout has 4 radio buttons, Rectangle, Box, Circle, and Cylinder. I have a group panel that has 4 separate panels that each have text boxes with labels for entering height, length, width, and radius. Here's how it looks: GUI layout. Depending on the radio button that is selected, certain boxes that aren't needed should be hidden. For example, if Rectangle is selected, only the boxes for length and width should be visible.
The main frame that will display everything is here:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import java.awt.Font;
public class GUIFrame extends JFrame
{
//private final BorderLayout layout;
private final FlowLayout layout;
private final JLabel lblTitle;
private final JButton btnProc;
public GUIFrame()
{
super("GUI Layout");
Font titleFont = new Font("Verdana", Font.BOLD, 26);
btnProc = new JButton("Click to Process");
lblTitle = new JLabel("Figure Center");
lblTitle.setFont(titleFont);
widthPanel myWidth = new widthPanel();
myWidth.setLocation(0, 400);
lengthPanel myLength = new lengthPanel();
heightPanel myHeight = new heightPanel();
radiusPanel myRadius = new radiusPanel();
radioButtonPanel myButtons = new radioButtonPanel();
//layout = new BorderLayout(3, 2);
layout = new FlowLayout();
JPanel txtGroup = new JPanel();
txtGroup.setLayout(new GridLayout(2, 2));
txtGroup.add(myWidth);
txtGroup.add(myLength);
txtGroup.add(myRadius);
txtGroup.add(myHeight);
setLayout(layout);
add(lblTitle);
add(myButtons);
add(txtGroup);
add(btnProc);
if(myButtons.btnRectangle.isSelected())
{
myHeight.setVisible(false);
myRadius.setVisible(false);
}
}
private class RadioButtonHandler implements ItemListener
{
#Override
public void itemStateChanged(ItemEvent event)
{
}
}
}
I can get this working using if statements, but I'm supposed to be using event handlers and I'm lost on how to code that to get it to work properly.
if it helps, here's the code for the button panel:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
public class radioButtonPanel extends JPanel
{
private final JRadioButton btnRectangle;
private final JRadioButton btnBox;
private final JRadioButton btnCircle;
private final JRadioButton btnCylinder;
private final ButtonGroup radioButtonGroup;
private final JLabel label;
private final JPanel radioPanel;
public radioButtonPanel()
{
radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(5,1));
btnRectangle = new JRadioButton("Rectangle", true);
btnBox = new JRadioButton("Box", false);
btnCircle = new JRadioButton("Circle", false);
btnCylinder = new JRadioButton("Cylinder", false);
label = new JLabel("Select A Figure:");
radioPanel.add(label);
radioPanel.add(btnRectangle);
radioPanel.add(btnBox);
radioPanel.add(btnCircle);
radioPanel.add(btnCylinder);
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(btnRectangle);
radioButtonGroup.add(btnBox);
radioButtonGroup.add(btnCircle);
radioButtonGroup.add(btnCylinder);
add(radioPanel);
}
}
And a sample of one of the panels. They all follow the same setup, just different variable names.
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.GridLayout;
public class heightPanel extends JPanel
{
private final JLabel lblHeight;
private final JTextField txtHeight;
private final JPanel myHeight;
public heightPanel()
{
myHeight = new JPanel();
myHeight.setLayout(new GridLayout(2,1));
lblHeight = new JLabel("Enter Height:");
txtHeight = new JTextField(10);
myHeight.add(lblHeight);
myHeight.add(txtHeight);
add(myHeight);
}
}
The below code should give you a quick introduction of how to use event listener/handler for your button group (JRadioButtons).
but I'm supposed to be using event handlers and I'm lost on how to
code that to get it to work properly.
//add listener to the rectangle button and should be the same for the other `JRadioButtons` but with different `identifiers`.
btnRectangle.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnRectangle){
//TODO
}
}
});
Depending on the radio button that is selected, certain boxes that
aren't needed should be hidden. For example, if Rectangle is selected,
only the boxes for length and width should be visible.
//To hide JTextFields use
void setVisible(boolean visible) method. You should pass false as the argument to the method if you want to hide the JTextField.
Example:
btnRectangle.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnRectangle){
TextFieldName.setVisible(false); // set the textfields that you want to be hidden once the Rectangle button is chosen.
}
}
});
I am actually a beginner in Java Programming (on eclipse and without netbeans), and want to clear a JLabel presents in a JFrame by clicking a JButton without removing the JButton present at the top of this frame.
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ButtonTest extends JPanel implements ActionListener {
private JButton ouvrirButton = new JButton("Ouvrir");
private JButton retirerButton = new JButton("Retirer");
private JButton ajouterButton = new JButton("Ajouter");
public ButtonTest() {
add(ouvrirButton);
add(retirerButton);
add(ajouterButton);
ouvrirButton.addActionListener(this);
retirerButton.addActionListener(this);
ajouterButton.addActionListener(this);}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
Color color = getBackground();
// ACTION Button "OUVRIR"
// I WANT TO REMOVE THIS JLABEL TEXT WHEN I CLICK FOR EXEMPLE ON
// OR "RETIRER"
if (source == ouvrirButton)
{
color = Color.yellow;
JLabel lab1 = new JLabel("Text", JLabel.LEFT);
setLayout(new FlowLayout());
add(lab1 = new JLabel("INVENTAIRE : "));
lab1.setBounds(20, 15, 500, 100);
}
else if (source == retirerButton)
color = Color.red;
else if (source == ajouterButton)
color = Color.red;
setBackground(color);
repaint();
}
// The main
public static void main(String[] args) {
// NOM DE LA FENETRE
JFrame frame = new JFrame("Programme ");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(new ButtonTest());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300, 700);
frame.setVisible(true);
}
}
I tried .setText("") but it doesn't work... Please help me !
I tried .setText("") but it doesn't work...
Yes it does. The problem is you create the label in the ActionListener so that label reference is only valid in the block of code that created it.
You need to create the label as an instance variable (the way you did for all you buttons) and add the label to the fame at the same time you add the buttons to the panel.
Then you will be able to access the label in the ActionListener and change the text.
At the moment I write a little client application. I have a window with a JTextArea (Display area for the server output) and a user-list.
My plan is to show/hide this user-list over a menu item, but I don't know how. My ideas:
Use a BorderLayout: Without a JScrollPane for the list. It works, but I cannot change the width of the user-list (Because BorderLayout.WEST and BorderLayout.EAST ignore the width)
Use a BorderLayout with a JScrollPane for the user list and show/hide the JScrollPane -> Does not work, don't ask me why...anyway, this way is not a nice solution
Use a JSplitPane, set the resize weight to 0.9. When the user-list should disappear, I minimize the right component (aka the user list) -> How ?
My code at the moment:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
public class SplitPaneTest extends JFrame implements ActionListener
{
private JSplitPane splitPane;
private JTextArea textDisplay;
private JList<String> listUser;
private JScrollPane scrollTextDisplay;
private JScrollPane scrollListUser;
private JCheckBox itemShowUser;
public static void main(String[] args)
{
new SplitPaneTest();
}
public SplitPaneTest()
{
setTitle("Chat Client");
setMinimumSize(new Dimension(800, 500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textDisplay = new JTextArea();
listUser = new JList<>();
DefaultListModel<String> modelUser = new DefaultListModel<>();
listUser.setModel(modelUser);
modelUser.addElement(new String("User 1"));
modelUser.addElement(new String("User 2"));
modelUser.addElement(new String("User 3"));
scrollTextDisplay = new JScrollPane(textDisplay);
scrollListUser = new JScrollPane(listUser);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(scrollTextDisplay);
splitPane.setRightComponent(scrollListUser);
splitPane.setResizeWeight(0.9);
setContentPane(splitPane);
JMenuBar menubar = new JMenuBar();
JMenu menuWindow = new JMenu("Window");
itemShowUser = new JCheckBox("Show user list");
itemShowUser.addActionListener(this);
itemShowUser.setSelected(true);
menuWindow.add(itemShowUser);
menubar.add(menuWindow);
setJMenuBar(menubar);
setVisible(true);
}
public boolean isUserListEnabled()
{
return itemShowUser.isSelected();
}
public void setUserListEnabled(boolean status)
{
scrollListUser.setVisible(status);
}
#Override
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == itemShowUser)
{
boolean status = isUserListEnabled();
setUserListEnabled(status);
}
}
}
And the result is:
And with hidden JScrollPane scrollListUser:
Can anybody give me a tipp ? The user-list is still visible ( I thought the JSplitPane would repaint..) .I come from Qt (C++) and in Qt I could use a dock widget - but Swing does not have one and to use third libs....I don't know - maybe there is a solution.
Looks like the splitPane can't handle invisible components well - a way out is to add/remove the scrollPane as appropriate:
public void setUserListEnabled(boolean status)
{
splitPane.setRightComponent(status ? scrollListUser : null);
splitPane.revalidate();
}
I have simple GUI code as follows, in which I want to make the JButton one translucent, so that the image behind the JButton is visible!
package dealORnodeal;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Deal extends JFrame implements ActionListener
{
private Container contentPane = getContentPane();
private JButton one = new JButton("1"),two = new JButton("2");
private JMenu menu1 = new JMenu("JumpTo");
private JMenuBar bar1 = new JMenuBar();
private ImagePanel bg = new ImagePanel(new ImageIcon("bg.jpg").getImage());
public Deal()
{
super("Deal Or No Deal");
setSize(800,850);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
contentPane.add(bg);
JMenuItem item1;
item1 = new JMenuItem("Start Game");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Rules");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Credits");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Menu");
item1.addActionListener(this);
menu1.add(item1);
bar1.add(menu1);
setJMenuBar(bar1);
//GAME CODE
one.setBounds(25,151,190,49);
one.addActionListener(this);
add(one);
//GAME CODE END
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{}
}
Now how would the code be if I wanted to set the button to be translucent so that the background image would be visible through the button. BTW please don't confuse Translucent with transparent!
I can't comment to your question, so I'll answer you here..
if you use this code:
myButton.setOpaque(false);
It would not paint the button - because now it's a trasnparent.
to create the button translucent I think you should override the button paint method..
take a look at this thread
setOpaque doesn't work for JButtons, the right property is:
button.setContentAreaFilled(false);
I have a JMenu and when a person clicks on a JMenuItem I want the middle panel to refresh and display some new stuff. So I tried the .removeAll which works fine but when I try to add something it wont show.
Note: I'm using the WindowBuilder PRO, so I still trying to get use to it and whatnot
Here is my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import java.awt.List;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JSeparator;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Panel;
import java.awt.GridBagConstraints;
public class HomeScreen {
JFrame frame;
private final Panel panel = new Panel();
public HomeScreen(String name) {
initialize(name);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(String name) {
frame = new JFrame("Timzys CMS / Monitor / Account( " + name + " )");
frame.setResizable(false);
frame.setSize(694,525);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnMonitor = new JMenu("Monitor");
menuBar.add(mnMonitor);
JMenuItem mntmUsers = new JMenuItem("Users");
mnMonitor.add(mntmUsers);
JMenuItem mntmContentPosts = new JMenuItem("Content Posts");
mnMonitor.add(mntmContentPosts);
JMenuItem mntmLogs = new JMenuItem("Logs");
mnMonitor.add(mntmLogs);
JMenu mnExtras = new JMenu("Extras");
menuBar.add(mnExtras);
mntmUsers.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.getContentPane().removeAll();
frame.getContentPane().add(new JLabel("Test"));
frame.getContentPane().revalidate();
}
});
JMenuItem mntmFeedbacksuggestions = new JMenuItem("Feedback/Suggestions");
mnExtras.add(mntmFeedbacksuggestions);
frame.getContentPane().setLayout(null);
panel.setBounds(0, 0, 688, 476);
frame.getContentPane().add(panel);
panel.setLayout(null);
JTextPane txtpnWelcomeTimzysCms = new JTextPane();
txtpnWelcomeTimzysCms.setFont(new Font("Arial", Font.PLAIN, 18));
txtpnWelcomeTimzysCms.setEditable(false);
txtpnWelcomeTimzysCms.setText("Welcome Timzys CMS Monitor. If you are here then you are a admin! So please do not tamper with any important things. If you have questions or suggestions goto the extras tab and submit a feedback idea. Enjoy!");
txtpnWelcomeTimzysCms.setBounds(10, 11, 668, 72);
panel.add(txtpnWelcomeTimzysCms);
}
}
Probably you're facing similar problem as someone else in the question:
Java Swing revalidate() vs repaint()
As suggested: try to call repaint instead of revalidate (the one you're calling right now in your implementation of method actionPerformed)
The problem is that because you have set the frame's layout to null, the new JLabel that you add will need to have its size set. e.g.:
JLabel label = new JLabel("Test");
label.setSize(100, 100);
frame.getContentPane().add(label);
As an aside:
Actually I wonder why you are mixing AWT & Swing components here. You have a heavyweight AWT panel added to the frame which blocks out the menus. Switching to JPanel would fix this.