Setting radio buttons to a background - java

I was trying to set Radio Button to a background in order to allow the user to select.
Here is the code ..
public class FirstWindow extends JFrame {
private JTextField search;
private JRadioButton author,title,both;
private ButtonGroup grp;
public FirstWindow() {
super("My App");
setLayout(new BorderLayout());
JLabel backGround = new JLabel(new ImageIcon("C:\\Users\\Kareem Abdo\\Desktop\\3.Jpg"));
backGround.setLayout(null);
add(backGround);
search = new JTextField("Search...");
search.setFont(new Font("Arial",Font.PLAIN,16));
search.setSize(150, 30);
search.setLocation(20, 20);
backGround.add(search);
author = new JRadioButton("Author",true);
author.setLocation(20, 25);
backGround.add(author);
title = new JRadioButton("Title",false);
title.setLocation(25, 25);
backGround.add(title);
both = new JRadioButton("Both",false);
both.setLocation(250, 250);
backGround.add(both);
grp = new ButtonGroup();
grp.add(author);
grp.add(title);
grp.add(both);
But the radio buttons doesn't appear on the screen!

JLabel (JLabel backGround = new JLabel) haven't implemented any LayoutManager, then you have to set the proper one, otherwise any JComponent added to JLabel isn't visible
maybe better could be start with Image (BufferedImage) painted in paintComponent to the JPanel (pre_implemented FlowLayout in API)

Related

Jframe is setting everything in the middle of the screen

My Jframe is setting every component at the middle of the screen even if i set a specific location...
for exmaple, the button and the searchbar are right next to each other.
Here is the image of the program:
thanks!
It really seems easy but i can't figure out why it doesn't work
private String msg;
private JFrame frame;
private JButton button;
private JPanel panel;
private JTextField searchBar;
private JLabel name;
private JLabel logo;
public GUI() {
this.frame = new JFrame();
this.panel = new JPanel();
this.panel.setBorder(BorderFactory.createEmptyBorder(300, 300, 300, 300));
//this.panel.setLayout(new GridLayout(10, 10));
this.frame.add(panel, BorderLayout.CENTER);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setTitle("emdb (ezon's movie database)");
Icon icon = new ImageIcon("images/search.png");
this.button = new JButton(icon);
this.button.setBounds(100, 20, 25, 25);
this.button.addActionListener(this);
this.searchBar = new JTextField(30);
this.searchBar.setBounds(100, 20, 200, 25);
this.name = new JLabel();
this.name.setBounds(50, 20, 80, 25);
ImageIcon log = new ImageIcon("images/logo.png");
this.logo = new JLabel(log);
this.logo.setBounds(100, 0, 300, 150);
}
public void clearScreen() {
this.panel.removeAll();
this.panel.revalidate();
this.panel.repaint();
}
public void searchScreen() {
this.panel.add(this.searchBar);
this.panel.add(this.button);
this.panel.add(this.logo);
this.frame.pack();
this.frame.setVisible(true);
}
public void searchedScreen() {
this.panel.add(this.name);
}
public String getMsg() {
return(this.msg);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Searched: " + this.searchBar.getText());
this.name.setText(this.searchBar.getText());
this.msg = this.searchBar.getText();
clearScreen();
searchedScreen();
}
Swing was designed to be used with layout managers.
even if i set a specific location.
The job of the layout manager is to set the size and location of the component based on the rules of the layout manager. So your setBounds() statements will be overridden. Don't attempt to use setBounds().
the button and the searchbar are right next to each other
The default layout manager for a JPanel is the FlowLayout, which does exactly that.
this.frame.add(panel, BorderLayout.CENTER);
Change the above statement to:
this.frame.add(panel, BorderLayout.PAGE_START);
to see a difference.
Read the section from the Swing tutorial on Layout Managers for more information and examples. Decide which layout manager (or combination of layout managers on different panels) will achieve your desired layout.

JAVA JTextField doesnt show up

So Im trying to make a little program to calculate the area of a specific shape.
The user should be able to make a input via a textfield (Like the height and stuff of the shapes). The he should press a button and the price should get printed.
But it doesnt show up.
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rechner extends JFrame implements ActionListener{
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JButton button4;
private static JTextField numberField;
private JPanel jpanel;
public Rechner(String titel){
super(titel);
jpanel = new JPanel();
numberField = new JTextField(1500);
add(numberField, BorderLayout.CENTER);
button1 = new JButton("Rechteck");
button1.setBounds(10, 10, 150, 30);
button1.addActionListener(this);
add(button1);
button2 = new JButton("Dreieck");
button2.setBounds(170, 10, 150, 30);
button2.addActionListener(this);
add(button2);
button3 = new JButton("Trapez");
button3.setBounds(330, 10, 150, 30);
button3.addActionListener(this);
add(button3);
button4 = new JButton("Parallelogramm");
button4.setBounds(490, 10, 150, 30);
button4.addActionListener(this);
add(button4);
setResizable(false);
}
public static void main(String[] args) {
Rechner frame = new Rechner("Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(660, 400);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
System.out.println("fff");
}
String numberStr = numberField.getText();
}
}
The default layout manager of a JFrame (well, of its content pane in fact) is BorderLayout.
Without specified constraints , the component is added to BorderLayout.CENTER, so
add(component);
is the same as
add(component, BorderLayout.CENTER);
and each component added this way will replace the last component added to the center.
Also note that setBounds will have no effect if there is a layout manager, and that you create a JPanel that you never use.
Finally, you may want to have a look at this guide : A Visual Guide to Layout Managers
This line is mainly the problem:
add(numberField, BorderLayout.CENTER);
Is causing the TextField to fill the entire space. Then, the next time you add a component to the JFrame with BorderLayout.CENTER, the JTextField gets replaced. To fix this:
super(titel);
jpanel = new JPanel();
add(jpanel, BorderLayout.NORTH); //adding the jpanel
button1 = new JButton("Rechteck");
jpanel.add(button1);
button1.setBounds(10, 10, 150, 30);
//adding the other buttons to the JPanel...
//...
//...
button4.addActionListener(this);
button3.addActionListener(this);
button2.addActionListener(this);
button1.addActionListener(this);
numberField = new JTextField(1500);
add(numberField);//this will cause it to fill the remaining space
setResizable(false);
Explanation:
The buttons should go into the JPanel you created, and the JPanel should go into the JFrame's NORTH. That way they don't cover the JFrame

How do I left align a JLabel inside a JPanel?

I am trying to build a status bar for my login dialog box but the label doesn't align to the left of the status panel. Here is my code.
public class LoginDialog extends JDialog {
private static final long serialVersionUID = 1L;
protected JLabel lblTopSpace = null;
protected JPanel loginPanel = null;
protected JPanel statusPanel = null;
public LoginDialog(String title) {
super((Dialog)null);
this.setTitle(title);
Initialize();
}
protected void Initialize() {
lblTopSpace = new JLabel("Login into Bookyard");
lblTopSpace.setForeground(this.getBackground());
loginPanel = new LoginPanel();
statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
statusPanel.setSize(this.getWidth(), 50);
JLabel lblStatus = new JLabel("Status");
lblStatus.setFont(new Font("Verdana", Font.PLAIN, 12));
lblStatus.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(lblStatus);
this.setLayout(new BorderLayout());
Container container = this.getContentPane();
container.add(lblTopSpace, BorderLayout.NORTH);
container.add(loginPanel, BorderLayout.CENTER);
container.add(statusPanel, BorderLayout.SOUTH);
this.pack();
}
}
Here is what it looks like presently.
What am I missing?
Your label is inside a panel that is inside the contentpane of the dialog. So the label is managed with the layout of its parent panel. But you don't set any particular layout for it, then it's a FlowLayout, and your label is then centered in it with a size the minimal one required to let text appear. Then the label is left aligned in its own area, but this one is centered in the panel.
Either change the layout of the panel to let the label extends in it (add a BorderLayout and set the label in north, center or south of it), or remove the panel that seems not useful (and let the label extends in the south of the contentpane.
statusPanel.setLayout(new BorderLayout());
statusPanel.add(lblStatus,BorderLayout.SOUTH);
or
container.add(lblStatus,BorderLayout.SOUTH);

Search function with 2 JComboBox - searching with 1st the 2nd is not displayed

It runs fine but when choosing the idCombo with which I'm searching, the nameFIeld which is also a JComboBox does not change but remains the first in the list, while all other fields change accord.
package GUI;
import Logic.*; //Inherits from vector class found in the package Logic
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; //Works with the ActionListener
public class Search extends JFrame implements ActionListener {
//Panels
private JPanel north = new JPanel();
private JPanel south = new JPanel();
private JPanel west = new JPanel();
private JPanel east = new JPanel();
private JPanel center = new JPanel();
//Labels
private JLabel mainTitle = new JLabel ("Search Records"); //Main title
private JLabel westLabel = new JLabel (" "); //To create margins
private JLabel eastLabel = new JLabel (" ");
//Labels and Text Fields
private JLabel idLabel = new JLabel("CD ID");
private JComboBox idCombo = new JComboBox();
//To create a drop down list of CD IDs
private JLabel nameLabel = new JLabel("Customer Name");
private JComboBox nameField;
private JLabel dateLabel = new JLabel("Date");
private JTextField dateField = new JTextField();
private JLabel subjectLabel = new JLabel("Subject");
private JTextField subjectField = new JTextField();
private JLabel fileLabel = new JLabel("File");
private JTextField fileField = new JTextField();
private JLabel pageLabel = new JLabel("Page");
private JTextField pageField = new JTextField();
//Buttons
private JButton exitButton = new JButton("Exit");
//Creating an instance of type VectorRecord
private VectorRecord vr;
private VectorCustomer vc;
public Search(VectorRecord vr, VectorCustomer vc){
//NORTH PANEL
super("Search Records");
this.vr = vr;
this.vc = vc;
this.setLayout(new BorderLayout()); //Setting the 'Search Records' window layout to Border Layout
this.add(north,BorderLayout.NORTH); //Adding the north panel to the Border Layout
mainTitle.setFont(new Font ("Arial",Font.BOLD,20)); //Arranging the font of the main title
mainTitle.setForeground(Color.black); //Arranging the color of the main title
north.add(mainTitle); // Adding the maintitle to the north panel
//CENTER PANEL
center.setLayout(new GridLayout(6,2,0,15)); //6 rows, 2 columns and spacing
this.add(center,BorderLayout.CENTER); //Adding the center panel to the Border Layout
//Adding Labels and TextFields to the center panel
center.add(idLabel);
center.add(idCombo);
idCombo.setModel(new DefaultComboBoxModel(vr.fillIdCombo())); //fillIdCombo is a method in VectorRecords class
idCombo.addActionListener(this);
idLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(nameLabel);
center.add(nameField);
nameField.setModel(new DefaultComboBoxModel(vc.fillCustomerCombo()));
nameField.addActionListener(this);
nameLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(dateLabel);
center.add(dateField);
dateLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(subjectLabel);
center.add(subjectField);
subjectLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(fileLabel);
center.add(fileField);
fileLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
center.add(pageLabel);
center.add(pageField);
pageLabel.setFont(new Font ("Arial", Font.BOLD, 16)); //Arranging the font and size
//WEST & EAST PANEL
//To create margins
west.setLayout(new FlowLayout()); //Setting the west panel in the border layout, to Flow layout
this.add(west,BorderLayout.WEST);
west.add(westLabel);
east.setLayout(new FlowLayout()); //Setting the east panel in the border layout, to Flow layout
this.add(east,BorderLayout.EAST);
east.add(eastLabel);
//SOUTH PANEL
south.setLayout(new FlowLayout()); //Setting the south panel in the border layout, to Flow layout
this.add(south,BorderLayout.SOUTH); //Adding the south panel to the BorderLayout
south.add(exitButton); //Adding the Exit button to the south panel
exitButton.addActionListener(this); //In order for the Exit button to work
this.setSize(450,350); //Giving the window a size and location a
this.setLocation(320,250);
this.setVisible(true);
}
public void actionPerformed (ActionEvent event){
Record tempRecord= new Record();
Record modifiedRecord = new Record();
String recordId = (String)idCombo.getSelectedItem();
tempRecord = vr.getRecordRec(Integer.parseInt(recordId));
if(event.getSource() instanceof JComboBox){
if(event.getSource() == idCombo){
nameField.setSelectedItem((Customer) tempRecord.getCustomer());
dateField.setText(tempRecord.getDate());
subjectField.setText(tempRecord.getSubject());
fileField.setText(tempRecord.getFile());
pageField.setText(tempRecord.getPage() + "");
}
}
if(event.getSource().equals(exitButton)){
vr.saveRecordsToFile();
this.dispose();
}
}
}
When you invoke setSelectedItem on nameField with the Customer object returned by tempRecord.getCustomer() it will try to find the whether the object is already present in the nameField combobox.
While checking it will invoke equals method for object equality, make sure you override equals method of the Customer class.

how to add actionlisters for 2 sets of radiobuttons

I am making a frame with 3 panels. 1 panel has some text on it, 1 has radio buttons to alter the font that the saying appears in and the last panel has radio buttons to alter the size of the font. I have everything done, but I am confused as to how to add the action listener for the size radio buttons. Can anyone help me with this? The code for the actionlistener is near the bottom. Thank you very much!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Layouts extends JPanel implements ActionListener
{
int style = Font.PLAIN;
String font = "Arial";
private JLabel saying, overAll, top, sizeP, fontP;
private JPanel panel1, panel2, panel3, panel4;
private JRadioButton style1, style2, style3, style4, size1, size2, size3, size4;
//-----------------------------------------------------------------
// Sets up a panel with a label and some check boxes that
// control the style of the label's font.
//-----------------------------------------------------------------
public Layouts()
{
style1 = new JRadioButton ("Arial", true);
style2 = new JRadioButton ("Times New Roman", false);
style3 = new JRadioButton ("Verdana", false);
style4 = new JRadioButton ("Thonburi", false);
size1 = new JRadioButton ("36", false);
size2 = new JRadioButton ("24", false);
size3 = new JRadioButton ("20", false);
size4 = new JRadioButton ("16", true);
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
saying = new JLabel ("Say it with style!");
saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later
ButtonGroup group = new ButtonGroup(); //use this code for the homework
group.add (style1);
group.add (style2);
group.add (style3);
group.add (style4);
style1.addActionListener (this);
style2.addActionListener (this);
style3.addActionListener (this);
style4.addActionListener (this);
ButtonGroup group2 = new ButtonGroup();
group.add(size1);
group.add(size2);
group.add(size3);
group.add(size4);
size1.addActionListener (this);
size2.addActionListener (this);
size3.addActionListener (this);
size4.addActionListener (this);
setBackground (Color.red);
setPreferredSize (new Dimension(500, 100));
setLayout(new BorderLayout());
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.EAST);
add(panel3, BorderLayout.WEST);
//add(panel2, East);
//add(panel3, West);
panel1.add(saying);
panel1.setBackground(Color.yellow);
panel2.setLayout(new GridLayout());
panel2.setBackground(Color.red);
panel2.add(style1);
panel2.add(style2);
panel2.add(style3);
panel2.add(style4);
panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
panel3.setBackground(Color.red);
panel3.add(size1);
panel3.add(size2);
panel3.add(size3);
panel3.add(size4);
}
//*****************************************************************
// Represents the listener for both check boxes and the radio boxes.
//*****************************************************************
public void actionPerformed(ActionEvent e) // this is our bread and butter, it is basically what we will be changing
{
Object source = e.getSource();
if (style1.isSelected())
font = "Arial";
else
if (style2.isSelected())
font = "Times New Roman";
else
if (style3.isSelected())
font = "Verdana";
else
if (style4.isSelected())
font = "Thonburi";
saying.setFont(new Font (font, style, 36));
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("Layouts");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Layouts panel = new Layouts();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
You don't have to hard code style1-4 into the listener. You see how you got the source of the action, but you never used it? That source is the radio button object that fired this event. You can just get the label on the radio button for the value. And then just set the font according the label value of the radio button that just fired the event. Also you might want to split this off into two eventhandlers, one for each set of radio buttons. Makes it easier to read and easier to discern which set is being clicked.
instead of
saying.setFont(new Font (font, style, 36));
in your actionPerformed method try this:
saying.setFont(new JLabel().getFont()); // get the default font from a new JLabel
this.repaint();
so it seems there's something wrong with the fonts you wanna display. Are the fonts actually on your machine?
Does System.out.println(new Font("Arial",style,36).getFamily()); actually print out Arial or sth. else?

Categories

Resources