Given my code below, I'm a little confused as to what I am missing in order to allow it to function so that when the user inputs their hexadecimal value with a maximum of five digits, they will get their binary equivalent and the decimal equivalent. I thought the code at the bottom automatically converts given a certain number in hex. Also would it be difficult to store the binary values in an array?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ConvertPanel {
public static void main(String[] args) {
JFrame frame = new JFrame ("Hexadecimal to Binary and Decimal.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NumberConverter());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NumberConverter extends JPanel {
private JLabel binaryLabel = new JLabel();
private JLabel totalone = new JLabel();
private JLabel totaltwo = new JLabel();
private JLabel decimalLabel = new JLabel();
private JTextField hexdecString = new JTextField();
private JButton convert;
public NumberConverter() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400, 300));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel converterName = new JLabel("Hexadecimal Input");
convert = new JButton ("Convert");
convert.addActionListener (new ButtonListener());
JPanel panelName = new JPanel(new GridLayout(2,2));
panelName.add(converterName);
panelName.add(hexdecString);
add(panelName, BorderLayout.NORTH);
add (convert);
JPanel totalPanel = new JPanel(new GridLayout(1,3));
totalPanel.add(new JLabel("Binary:"));
totalone = new JLabel("---- ---- ---- ---- ----");
totalPanel.add(totalone);
totalPanel.add(binaryLabel);
JPanel totalPanel2 = new JPanel(new GridLayout(2,3));
totalPanel2.add(new JLabel("Decimal:"));
totaltwo = new JLabel("------");
totalPanel2.add(totaltwo);
totalPanel2.add(decimalLabel);
JPanel south = new JPanel(new GridLayout(2,1));
south.add(totalPanel);
south.add(totalPanel2);
add(south, BorderLayout.SOUTH);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event){
Integer n = Integer.valueOf(hexdecString.getText(), 16);
decimalLabel.setText(String.valueOf(n));
binaryLabel.setText(Integer.toBinaryString(n));
} {
}
}
}
Related
When I click the buttons I added for ok and cancel, actionPerformed does not get called
code:
void testServerFlags()
{
dlgEmpty mdlgCreateBot = new dlgEmpty(null);
mdlgCreateBot.show();
ted=0;
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import Indicator.cGenIndicator;
import Indicator.cIndicatorUtil;
import MyLib.cLib;
public class dlgEmpty extends JDialog implements ActionListener
{
Box parPan, topPan, butPan,centerPan;;
public dlgEmpty(JFrame parent)
{
super(parent,"Indicators", true);
Container content = getContentPane();
content.setLayout(new BorderLayout());
butPan = Box.createHorizontalBox();
JButton okBut = new JButton("OK");
okBut.setActionCommand("ok");
butPan.add(okBut, BorderLayout.SOUTH);
JButton CancelBut = new JButton("Cancel");
CancelBut.setActionCommand("Cancel");
butPan.add(CancelBut, BorderLayout.SOUTH);
content.add(butPan, BorderLayout.SOUTH);
centerPan = Box.createVerticalBox();
content.add(centerPan, BorderLayout.CENTER);
pack();
show();
}
boolean isOk=false;
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))
isOk=true;
setVisible(false);
}
}
You are setting the action command to:
okBut.setActionCommand("ok");
But are checking for:
if ("OK".equals(e.getActionCommand()))
This won't match, because the case is different.
setActionCommand only sets the message in the ActionEvent. What you need is addActionListener. I have updated your code.
But the other thing you should know is that there is no need for this. Instead use JOptionPane.showConfirmDialog with a JComponent as the message parameter: It does all the button handling and modal in-line waiting automatically
public dlgEmpty(JFrame parent)
{
super(parent,"Indicators", true);
Container content = getContentPane();
content.setLayout(new BorderLayout());
butPan = Box.createHorizontalBox();
JButton okBut = new JButton("OK");
okBut.setActionCommand("ok");
okBut.addActionListener(this); // added this line
butPan.add(okBut, BorderLayout.SOUTH);
JButton CancelBut = new JButton("Cancel");
CancelBut.setActionCommand("Cancel");
CancelBut.addActionListener(this);
butPan.add(CancelBut, BorderLayout.SOUTH); //added this line
content.add(butPan, BorderLayout.SOUTH);
centerPan = Box.createVerticalBox();
content.add(centerPan, BorderLayout.CENTER);
pack();
show();
}
boolean isOk=false;
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))
isOk=true;
setVisible(false);
}
}
I have this interface to create. I have a problem with the JScrollPane:
I declared a JPanel with a Gridlayout(8,1,0,2), I want 8 rows appear in this panel.
A row is a JPanel to, I set the size to make the 8 row panels appear in the big panel.
If the number of rows pass 8, I get two columns ...
I added a JScrollPane but it doesn't appear.
Testing button at the place of button, the scrollpane appear but returning to panel it disappear..
How can I do ??
I found a solution:
package d06.m03;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.BoxLayout;
public class ActionExample4 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ActionExample4 frame = new ActionExample4();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ActionExample4() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 778, 426);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 101, 742, 276);
//scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
getContentPane().add(scrollPane);
JPanel borderlaoutpanel = new JPanel();
scrollPane.setViewportView(borderlaoutpanel);
borderlaoutpanel.setLayout(new BorderLayout(0, 0));
JPanel columnpanel = new JPanel();
borderlaoutpanel.add(columnpanel, BorderLayout.NORTH);
columnpanel.setLayout(new GridLayout(0, 1, 0, 1));
columnpanel.setBackground(Color.gray);
for(int i=0;i<32;i++) {
JPanel rowPanel = new JPanel();
rowPanel.setPreferredSize(new Dimension(300,30));
columnpanel.add(rowPanel);
rowPanel.setLayout(null);
JButton button = new JButton("New button");
button.setBounds(20, 5, 89, 23);
rowPanel.add(button);
if(i%2==0)
rowPanel.setBackground(SystemColor.inactiveCaptionBorder);
}
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javafx.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class View extends JFrame{
JTextField tField = new JTextField(10);
JButton sortButton = new JButton("Sort");
JButton shuffleButton = new JButton("Shuffle");
JButton reverseButton = new JButton("Reverse");
JTextArea tArea = new JTextArea();
JLabel label = new JLabel("Enter a number: ");
JPanel upperPanel = new JPanel();
JPanel buttonPanel = new JPanel();
public View(){
setTitle("Exercise 22.2");
setSize(500,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
sortButton.addActionListener(new Listener());
upperPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
upperPanel.add(label);
upperPanel.add(tField);
buttonPanel.add(sortButton);
buttonPanel.add(shuffleButton);
buttonPanel.add(reverseButton);
add(upperPanel, BorderLayout.NORTH);
add(tArea, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
/*Getting error here!*/
public class Listener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello");
}
}
}
I'm trying to create a class that implements ActionListener to that I can register the class to my buttons.
I'm getting:
View.Listener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
I've tried implementing as an Anonymous Class and get the same error. I cannot figure it out.
I relied on auto imports and it imported the wrong library for ActionEvent.
import javafx.event.ActionEvent;
I needed import:
java.awt.event.ActionEvent;
My bad.
I have this code below. How do I make this JComboBOx run in an Applet? I'm trying to create an applet that allows the user to select a font and type in that font in a JTextArea. I have been searching for answers and made little progress. The error java.lang.reflect.InvocationTargetException keeps popping up. If you need more specifics please just comment.
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Font_Chooser extends JApplet {
JLabel jlbPicture;
public Font_Chooser(){
// Create the combo box, and set first item as Default
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();
final JComboBox comboTypesList = new JComboBox(names);
comboTypesList.setSelectedIndex(0);
comboTypesList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox jcmbType = (JComboBox) e.getSource();
String cmbType = (String) jcmbType.getSelectedItem();
jlbPicture.setIcon(new ImageIcon(""
+ cmbType.trim().toLowerCase() + ".jpg"));
System.out.println(comboTypesList.getSelectedItem());
//Font myFont = new Font((String)comboTypesList.getSelectedItem(), Font.BOLD, 12);
}
});
// Set up the picture
jlbPicture = new JLabel(new ImageIcon(""
+ names[comboTypesList.getSelectedIndex()] + ".jpg"));
jlbPicture.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
jlbPicture.setPreferredSize(new Dimension(177, 122 + 10));
// Layout the demo
setLayout(new BorderLayout());
add(comboTypesList, BorderLayout.NORTH);
add(jlbPicture, BorderLayout.SOUTH);
}
public static void main(String s[]) {
JFrame frame = new JFrame("JComboBox Usage Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new Font_Chooser());
frame.pack();
frame.setVisible(true);
}
}
My code is duplicating the creation of the main screen when I hit the button of the search..the search event should only open one new frame with a textbox to type query. The creation of another frame causes this duplication or is this some sort of bug? I tryed to use the mainFrame to upload the date of the searchPane (p1 in the code) but when I do this, this solves my problem of the window, but the defaultCloseOperation() on the Search Windows causes the close of the entire program - how may I solve this situtation?
thanks in advance
SearchScreen
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SearchScreen extends MainScreen{
JButton btsearch;
JLabel lbsearch;
protected JTextField txtsearch;
JPanel p1;
protected JFrame searchFrame = new JFrame();
public SearchScreen(){
//Button Creation
btsearch= new JButton("Search");
//Label Creation
lbsearch= new JLabel("Type Keywords in english to be searched below:");
//TextBox
txtsearch= new JTextField();
//Pane Creation
p1=new JPanel();
p1.setBackground(Color.gray);
//Pane Components
p1.add(lbsearch);
p1.add(txtsearch);
p1.add(btsearch);
//JFrame Layout Setup
p1.setLayout(new GridLayout(3,3));
btsearch.setEnabled(true);
//Adding JPaneel
searchFrame.add(p1);
//JFrame Setup
searchFrame.setTitle("SHST");
searchFrame.setSize(400, 400);
searchFrame.setVisible(true);
searchFrame.setDefaultCloseOperation(1);
}
}
MainScreen
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class MainScreen implements ActionListener {
JMenuBar bar;
JMenu file, register;
JMenuItem close, search;
JPanel entrance = new JPanel();
JFrame mainFrame = new JFrame();
public MainScreen()
{
bar= new JMenuBar();
file= new JMenu("File");
register= new JMenu("Search");
close= new JMenuItem("Close");
close.addActionListener(this);
search= new JMenuItem("Request Query");
search.addActionListener(this);
//mainFrame Setup
bar.add(file);
bar.add(register);
file.add(close);
register.add(search);
mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH);
mainFrame.setTitle("SHST");
mainFrame.setJMenuBar(bar);
mainFrame.setDefaultCloseOperation(0);
mainFrame.setVisible(true);
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
mainFrame.addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==search){
SearchScreen s= new SearchScreen();
}
public static void main (String[] args){
MainScreen m= new MainScreen();
}
}
Your issue is here
if(e.getSource()==search){
SearchScreen s= new SearchScreen();
}
Using inheritance implementation is dangereous, a SearchScreen is a MainScreen then the constructor of this class is always called.