I'm new to Java, and trying to figure out one last thing for my program.
This is the program that I have coded and with the layout it is perfectly fine no problem at all.
Now my program suppose highlight the buttons whenever to press it on the keyboard (NOT BY PRESSING THE BUTTON ON THE SCREEN)
I'm not sure what I have to use since the action that it needs to take is when they type it in the JTextArea. I'm trying to use KeyEvent with KeyPressed but not sure if that is the right thing to do since it doesn't really work.
I can't post my code at the moment here since this is an assignment and I don't want some of my classmate to google and use it if they found it here. (LOL)
As required here is my codes :)
import javax.swing.*; // import all javax.swing
import java.awt.*; // import all java.awt
import java.awt.event.*;
public class Sample extends JFrame implements KeyListener { // start of the
// class
private JTextArea field = new JTextArea(10,15); // create an instance of
// JTextField
private JPanel mainPanel = new JPanel(); // create an instance of JPanel
private JPanel TopFieldPan = new JPanel();
private JPanel MainBtnsPan = new JPanel();
private JPanel FifthRowPan = new JPanel();
JPanel fifthBtn = new JPanel();
private static JButton Space = new JButton("");
public Sample() { // start of the weather constructor
Space.setPreferredSize(new Dimension(280, 45));
fifthBtn.add(Space);
TopFieldPan.add(field);
FifthRowPan.setLayout(new BoxLayout(FifthRowPan, BoxLayout.X_AXIS));
FifthRowPan.add(fifthBtn);
MainBtnsPan.setLayout(new GridLayout(5, 5, 0, 0));
MainBtnsPan.add(FifthRowPan);
mainPanel.add(TopFieldPan);
mainPanel.add(MainBtnsPan);
this.add(mainPanel);
Space.setSelected(true);
field.addKeyListener(this); // important !!!
setTitle("Typing Tutor"); // set the title to the frame
setSize(300, 300); // set the fixed size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true); // make it visible
} // ends of the constructor
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
Space.setBackground(Color.green);
}
}
public void keyReleased(KeyEvent evt) {
Space.setBackground(null);
}
public void keyTyped(KeyEvent evt) {
// TODO Auto-generated method stub
if(evt.getKeyChar() == 'a' || evt.getKeyChar() == 'A')
{
Space.setBackground(Color.green);
}
else if(evt.getKeyChar() == 'b' || evt.getKeyChar() == 'B')
{
Space.setBackground(Color.red);
}
}
public static void main(String[] args) { // start of the main method
new Sample();
} // ends of main method
} // ends of class
I tried to simplified the code as much as I could and here is the final one.
So I'm trying to make it when I press a or A it should highlight that space JButton.
Create a map of your buttons and the keys they map to, like this:
Map<String, JButton> buttonMap = new HashMap<String, Button>();
Then, when you are adding the buttons, add them to the map, like this:
buttonMap.put(FirstRow[i].toLowerCase(), btn);
Then, add something like this into your KeyTyped:
public void keyTyped(KeyEvent evt) {
String keyPressed = new String(""+evt.getKeyChar()).toLowerCase();
JButton tmp = buttonMap.get(keyPressed);
if(null != tmp){
tmp.doClick();
}
}
I did this quickly to your code for row 1 and 2. You'll have to play around with it to get it to work for the special keys, but it should get you where you're trying to go.
I pasted it here, to keep the answer small.
http://pastebin.com/t1v8d6Hi
You're code looks okay for a first pass, you seem to have a basic mechanism in place that works with the KeyListener. You probably need to think about how to stop mouse clicks on the buttons, JButton.setEnabled(false) works but changes how the button is drawn so you may need to override the paint method. You probably only need to hook the keylistener up to one component, the window will get all events, the text area when it has focus. The main task you have is to work out how to map the key pressed events to the buttons, maybe use a hashmap or something to store the JButtons with the key being the character code?
Related
I'm doing a calculator in java, to make easyly, and don't put a document filter to the jtextfield. I opted for making the jtextfield not editable and adding a key listener, but when you press de delete button it makes an error sound.
I've go to change system's sounds in configuration, I have changed the system's sounds, and I discover that the sound it's made by "predetermined bip", and makes the sound "Windows Background". I can change my option and don't listen the sound, but I want this game to do it downloadable in the internet.
Here is a simple example:
If you press the delete key in the Text Field it' going to make sound:
public Example() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBounds(0, 0,250,200);
setLayout(null);
javax.swing.JTextField jTextField1 = new javax.swing.JTextField();
jTextField1.setEditable(false);
jTextField1.setBounds(30,50,180,60);
add(jTextField1);
}
public static void main(String args[]) {
Example a = new Example();
a.setVisible(true);
}
}
In that code the textfield was not editable, in the next code the Text Field it is not going to make sound:
public Example() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBounds(0, 0,250,200);
setLayout(null);
javax.swing.JTextField jTextField1 = new javax.swing.JTextField();
jTextField1.setEditable(true);
jTextField1.setBounds(30,50,180,60);
add(jTextField1);
}
public static void main(String args[]) {
Example a = new Example();
a.setVisible(true);
}
}
It's because the text field is editable.
I'will appreciate if you can help me, telling me how to fix it or how to change systems sound in code, or whatever you think can help me.
The sound that you're getting from pressing the del key will occur even if the JTextField is editable, and is an OS-dependent response to the key being pressed. The way around this is to prevent the del key from registering that it has been pressed, and the way to do this is to use key bindings to make the del key cause no response in the GUI -- give a do-nothing action in response to the del key's being pressed when the text field has focus. For example:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class Example extends JFrame {
public Example() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// setBounds(0, 0,250,200);
// setLayout(null);
JPanel panel = new JPanel();
int gap = 40;
panel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
JTextField jTextField1 = new JTextField(20);
jTextField1.setEditable(false);
panel.add(jTextField1);
// get input and action maps to do key binding
InputMap inputMap = jTextField1.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = jTextField1.getActionMap();
// the key stroke that we want to change bindings on: delete key
KeyStroke delKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
// tell the input map to map the key stroke to a String of our choosing
inputMap.put(delKeyStroke, delKeyStroke.toString());
// map this same key String to an action that does **nothing**
actionMap.put(delKeyStroke.toString(), new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
// do nothing
}
});
add(panel);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
Example example = new Example();
example.pack();
example.setLocationRelativeTo(null);
example.setVisible(true);
});
}
}
Side recommendations:
Avoid using KeyListeners with text components as this can lead to undesired and non-standard behavior. Use DocumentListeners and DocumentFilters instead.
Avoid setting bounds of text components since this will also lead to undesired and non-standard behaviors, especially with JTextAreas that don't show scroll bars when they are placed within JScrollPanes. Instead set the properties of the text component such as the column and Font properties.
This program is just for the sake of teaching myself Java.
While coding around i ran into the following problem:
I'm getting errors (red underlining) when using a listener for a button which I implement in my Main class.
Since I'm new in learning Java please excuse if the solution is obvious.I already tried making both the main and the actionPerfomed method abstract but that lead to further issues. I also tried #Override before the actionPerformed method.
Here is the code:
// Java program to create a blank text
// field of definite number of columns.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame implements ActionListener {
// JTextField
static JTextField t;
// JFrame
static JFrame f;
// JButton
static JButton b;
// label to diaplay text
static JLabel l;
// default constructor
Main()
{
}
// main class
public static void main(String[] args)
{
// create a new frame to stor text field and button
f = new JFrame("textfield");
// create a label to display text
l = new JLabel("nothing entered");
// create a new button
b = new JButton("submit");
// create a object of the text class
Main te = new Main();
// addActionListener to button
b.addActionListener(te);
// create a object of JTextField with 16 columns
t = new JTextField(16);
// create a panel to add buttons and textfield
JPanel p = new JPanel();
// add buttons and textfield to panel
p.add(t);
p.add(b);
p.add(l);
l.setOpaque(true);
// add panel to frame
f.add(p);
// set the size of frame
f.setSize(300, 300);
p.setBackground(Color.cyan);
f.show();
}
// if the button is pressed
public void actionPerformed(java.awt.event.ActionEvent e, JPanel p)
{
String s = e.getActionCommand();
if (s.equals("submit")) {
// set the text of the label to the text of the field
if(t.getText().equals("hue")) {
p.setBackground(changeColor());
}
l.setText(t.getText());
// set the text of field to blank
t.setText(" ");
}
}
public Color changeColor() {
int r = (int)(Math.random())*256;
int g = (int)(Math.random())*256;
int b = (int)(Math.random())*256;
Color color = new Color(r,g,b);
return color;
}
}
Here:
public void actionPerformed(java.awt.event.ActionEvent e, JPanel p)
Should be
public void actionPerformed(java.awt.event.ActionEvent e)
You have to match the expected signature exactly! You can't just add more parameters to a method you are supposed to override!
You see, in the end, it is not your code that will invoke that method when a button is clicked. It is the Swing framework that will do it. You tell it: when an action happens on this thing, then call back my code. How do you expect that this framework could know that you want an additional parameter to be passed?!
Beyond that: make it your practice to put #Override in front of any method that you expect to override a method (like in this case, where you are implementing a method of that interface). Because then the compiler can tell you when you make such mistakes!
But of course, you added that parameter so that the listener can use it. So: make it known to the listener, for example by adding a field to your Main class that you initialize in the constructor! So instead of passing the panel to that one method (where it can't go) pass it when doing new for your Main instance.
When a class implements an interface, then it has to implement the required functions with the same arguments as given in the Interface.
ActionListener implements only only one function as you can see at https://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
void actionPerformed(ActionEvent e);
So you have to implement that function in your class. But you implemented a different function which has the same name but not the same arguments:
void actionPerformed(java.awt.event.ActionEvent e, JPanel p)
I'm trying to find a way how to update a panel content after changing a state variable.
Concretely in the example below, there is simple JPanel inside JFrame with two buttons. When the app starts, its state variable ("window") equals "home" so home button should be invisible. After clicking on the page button the state variable change and so both buttons visibility should change after frame repainting. (i.e. the page button should disappear and the home button should appear).
In this case, it is possible to solve it without the state variable just using setVisibility() method for buttons. But in my app, I would like to have more JComponetns connected to the state variable. Is there a way how to do it?
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JPanelUpdateTest {
private JFrame frame;
private String window = "home";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JPanelUpdateTest window = new JPanelUpdateTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JPanelUpdateTest() {
initialize();
}
private void initialize() {
frame = new JFrame("JPanelUpdateTest");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
JButton btnHome = new JButton("home");
btnHome.setVisible(window == "home" ? false : true);
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
window = "page";
panel.revalidate();
frame.repaint();
}
});
panel.add(btnHome);
JButton btnPage = new JButton("page");
btnPage.setVisible(window == "page" ? false : true);
btnPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window = "home";
panel.revalidate();
frame.repaint();
}
});
panel.add(btnPage);
}
}
The problem is that initialize is only being called once, at object creation, and it should only be called once, and because of this the setVisible(...) code is not being called from the ActionListeners. Instead you need to put the mechanisms for changing the views within the ActionListeners themselves, not just changing state, not unless you are using a "bound property" and PropertyChangeListeners.
Myself, I'd recommend using a CardLayout to assist you in your swapping, and rather than directly changing Strings, call a public method of your class -- planning for when and if the ActionListener (controller) code is ever removed from the view class.
Also, regarding:
btnPage.setVisible(window == "page" ? false : true);
don't compare Strings using == or !=. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here.
Also, if all you want to do is change the text and behavior that a JButton is doing, then you can change this easily by using setText(...) to change only the text, and for a deeper change, call setAction(Action action) to change text and state.
I'm developing Java Swing application. My application has two Java classes. Inside class1.java, I include JFrame, JButton and JPanel (panel1). When I click the button I want to hide panel1 and should be shown panel2 of class2.java. I tried this method in button actionPerformed method of class1.java. But it was not working.
class2 pnl = new class2();
this.remove(panel1);
this.add(pnl);
this.validate();
this.repaint();
Analysis
You simply want the JComponents to be displayed on the JFrame. We can achieve this by using a single JPanel, but adding and removing the JComponents from it, during the JButton's action listener.
Without looking at your actual code, it is better to make a manageable way to reach code and instantiated Objects. The code listed below, creates a nice and manageable way to do so.
Achieving This
The entire class is listed below with comments for explanations.
package swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MultiPaneledFrame {
JFrame frame = new JFrame();
JPanel window = new JPanel();
// As you can see, we create an array containing all your JComponents.
// We have two of these, to simulate multiple JPanel's.
List<JComponent> window1Contents = new ArrayList<JComponent>();
List<JComponent> window2Contents = new ArrayList<JComponent>();
// NOTE: The above Lists can instead be stuck in their own class like asked for,
// and instantiated on Class invocation.
JButton goto2 = new JButton("Goto Panel 2");
JButton goto1 = new JButton("Goto Panel 1");
int panelToShow = 0; // 0 - First "panel".
// 1 - Second "panel".
// Main method of class. Change 'Multi_Paneled_Frame' to the name of your Class.
public MultiPaneledFrame() {
// Execute anything else you want here, before we start the frame.
window1Contents.add(goto2);
window2Contents.add(goto1);
// Here is where I personally am setting the coordinates of the JButton's on the JPanel.
goto2.setPreferredSize(new Dimension(200, 100));
goto1.setPreferredSize(new Dimension(200, 100));
//goto2.setBounds(5, 5, 150, 30); < Used for 'null' layout.
//goto1.setBounds(5, 5, 150, 30); < Used for 'null' layout.
goto2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addComponents(panelToShow = 1);
}
});
goto1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addComponents(panelToShow = 0);
}
});
initialiseFrame();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MultiPaneledFrame();
}
});
}
private void initialiseFrame() {
frame.setSize(600, 400); // Change it accordingly.
// Optional
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
// Needed
frame.setVisible(true);
frame.add(window);
window.setLayout(new BorderLayout()); // Assuming your using a BorderLayout.
//window.setLayout(null); < Uses 'null' layout.
addComponents(panelToShow);
// I always like to make sure that everything is on the frame nicely.
frame.repaint();
frame.validate();
}
private void addComponents(int panelNo) {
if (panelNo == 0) {
for (JComponent component : window1Contents) {
window.removeAll(); // We're removing everything that it contains and replacing it...
window.revalidate();
window.add(component, BorderLayout.CENTER);
//window.add(component); < Uses 'null' layout.
// Since we are using the first panel, we are adding
// everything from the first list of components to the window...
}
} else {
for (JComponent component : window2Contents) {
window.removeAll(); // We're removing everything that it contains and replacing it...
window.revalidate();
window.add(component, BorderLayout.CENTER);
//window.add(component); < Uses 'null' layout.
// Since we are using the second panel, we are adding
// everything from the second list of components to the window...
}
}
// Refreshes the frame.
frame.repaint();
frame.validate();
}
}
Conclusion
Although there are countless ways to achieve something like this, the way I have given, is semi-efficient, and very flexible. Feel free to edit the code, or drop a question if you have any concerns, and I will be happy to respond.
PS: This code was tested and works on a Macbook Air running OS X 10.11 and Java Version 8 Update 65.
CardLayout should be your solution. In this tutorial they show how to switch from panel to another one by selecting a value in ComboBox.
A little bit of explanation for the CarLayout:
The CardLayout lets you place different panel on top of each other but shows only one at the time. With your code, you select the one you want to display.
Initialisation:
this.setLayout(new CardLayout());
class1 pnl1 = new class1();
class2 pnl2 = new class2();
this.add(pnl1, "PANEL1");
this.add(pnl2, "PANEL2");
On your button actionPerformed:
CardLayout cl = (CardLayout)(this.getLayout());
cl.show(this, "PANEL2");
I am having a bit of problem regarding Swing. I have a JFrame called FrameMain. Inside it is a JPanel called panelChoices.
When FrameMain is called/created, it fills up the panelChoices object with a number of PanelEntries objects, which is a JPanel with a number of JButtons in it (it is a different class that I wrote).
What I want to do is when I click one of the buttons inside the PanelEntries object, I want to destroy/remove FrameMain, along with the rest of it components (including the PanelEntries object that contains the JButton).
I've tried using super but it returns the JPanel (the PanelEntries object) that holds the JButton and not FrameMain that holds them all together. How can I achieve this?
EDIT: It seems that I am not clear enough, so here's a bit more information from my work. I don't have the actual code right now because I am on a different machine but I hope this will help elaborate my question.
public class FrameMain() {
private JFrame frameMain;
private JPanel panelChoices;
public FrameMain(args) {
createGUI();
loadData();
}
private void createGUI() {
JFrame frameMain = new JFrame();
JPanel panelChoices = new JPanel(new GridLayout(1,1));
frameMain.add(panel);
// removed formatting and other design codes since they are not important.
pack();
}
private void loadData() {
boolean available;
for (int i = 1; i <= 10; i++) {
// do some if/else and give value to boolean available
PanelEntries panel = new PanelEntries(i, available);
frameMain.add(panel);
// more code here to handle data.
}
}
}
public class PanelEntries() extends JPanel {
public PanelEntries(int num, boolean avb) {
JButton button = new JButton("Button Number " + num);
button.setEnabled(avb);
add(button);
// add action listener to created button so that it calls 'nextScreen()' when clicked.
// more code
pack();
}
private void nextScreen() {
// destroy/dispose MainFrame here.
// See Notes.
AnotherFrame anotherFrame = new AnotherFrame();
}
}
Notes:
All classes are inside their own .java file.
I need to know how to dispose FrameMain from the button inside the PanelEntries object, not just disposing a JFrame.
As per the given information,
If you want to exit the application, its not a big deal use System.exit(0); :)
If you mean to dispose the frame, jframe.dispose();
If you want to remove a componet / all components you can use .remove(Component) / .removeAll() etc
If this did not help, please re-write your question with more information.