Changing Panel In Frame Using MouseListener On JLabel
Sep 5, 2014
My program has a MainFrame(extends JFrame)class and BasePanel(extends JPanel) class,and many other panels.I had used Images with JLabel in BasePanel.My current panel in MainFrame is BasePanel.I had used MouseListener so that when I click on any of the label it should show Other panel in same MainFrame and hide the BasePanel.But I am getting some problem with that.When I click on the Label,It hides the BasePanel but do not show the new Panel(Dictionary Panel in my code). In short when label is clicked I want to make DictionaryPanel visible and BasePanel invisible,both in MainFrame. I am using the following code on Label:
//constructor of BasePanel class
public BasePanel() {
dict=new Dictionary();
mf=new MainFrame();
dictLabel.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
I'm trying to build a program which sets a fill color for a rectangle. Each time the mouse is clicked the fill color changes according to the position of the mouse's X and Y. I succeeded to create a JFrame with a changing background according to the Mouse's position, but I can't seem to change the color of the rectangle (defined in MyPanel class).
I have a JFrame with four JPanels, SensorLL, SensorLC, SensorCR and SensorRR. They are instances of the class SensorUI. Each has a JLabel called Sensor.
The class SensorUI has a method writeSensor that writes to the JLabel "Sensor". I will have four temperature sensors. I have not started the code to read the temperature sensors yet. I will have a timer that will run once a second. When it fires I will read the sensors and display the result in the
JLabel "Sensor" in each jPanel.
I use MyMain to call FrameDemo and create the JFrame and a method
in FrameDemo to add the JPanels using addObject.
I can use SensorLL.writeSensor will write text to JLabel Sensor while in Mymain.
Then I start the timer ReadSensor.
When it times out it calls SampleSensors
For an experiment, while in SampleSensors I try to write to the JLabel in the instance SensorLL with
SensorLL.writeSensor("xx_");
I can't because it says it can't find the symbol variable SensorLL in SampleSensors class.
How can I "get to" that JLabel text when ever I need to?
Java Code:
public class MyMain { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here //SensorUI SensorLL = null; System.out.println("Start of main"); //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. FrameDemo.createAndShowGUI();
My program compiles and runs ok how ever when i open my Jinternal frame and click on one of the JLabels i get a error on the Cmd ther error reads as follows
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at JLabelAssignment.mouseClicked(JLabelAssignment.jav a:429) at java.awt.Component.processMouseEvent(Component.jav a:6508) at javax.swing.JComponent.processMouseEvent(JComponen t.java:3320)
Create a dnd (dungeons and dragons) character creator and back ground generator, have it display and run on a gui, to start with i decided to creator the gui as i go so i can see the progress, first i tried eclipse and windows builder, well after 1 day of reinstalling windows builder in about 5 different ways from multiple guides and sometimes getting it to partly work ...
After doing some more research I have figured out how to get the main program to display the gui . Heres what i have:
package com.mrgreaper; import javax.swing.*; public class MainWindow { private JPanel mainWindow; private JLabel playerlbl; private JLabel playerNamelbl;
[Code] .....
Now this works fine and the gui displays when the program is run but I can't change the text of any of the jlabels or textfield. If I try to do it in the main class i need to change them from private to public static but then in the form builder it says "cannot bind to static field *name of field*" but if i take static off then i cant change its value!
From what i understand this is because the window is an instance, so how to i change the value in that instance? I could put all my code in the one class, the one that creates the gui, but i really want it seperate, i would like the gui to update as the code runs... So how do i do it, how do i change the contents of jlabels on the fly, read the contents of text boxes on the fly etc....
I tried adding a getter setter
public void setCharFirst(JLabel charFirst) { this.charFirst = charFirst; } in the MainWindow.class
Then i tried to set it from my main function
MainWindow.setCharFirst("test");
but it cant access it as my main function is static and it is not now can i make it static...
I don't get what I missed in the code. I used the example of changing the text of a button by clicking on it, but instead I also added a JLabel and tried to make the button change its text instead, but it didn't work. What did I do wrong?
I've been trying to draw a panel on a frame with simple drawString drawRect and oval but when i run it the frame shows up with the exit on close stuff but nothing will show inside . this is my code :
import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.JFrame; class Draw extends JPanel{ public void paintcomponents(Graphics g){ super.paintComponent(g); g.drawLine(10,15,20,30);
[Code] .....
I'm not sure what's wrong , probably something to do from this pc ? since in the lab work at uni it worked fine .
In the image, the color of the frame of the window/JFrame is black. My question is that can we set the color of this frame, or is it just provided by the operating system and whatever color is default color of windows in an operating system will appear in our application?
I am talking about the frame, the boundary in the JFrame container which contains the close, maximize, and minimize buttons.
I want to switch panel on click of a button and code for switching panels is written in update method (Which gets called from notify observer of another class as part of Observer pattern.
playGame in Panel 1 of class 1 calls another class (class 2)and data comes in update method of class 1(Observer interface).
Now for switching panels code is in gameControllerDataProcessing . I am not able to switch the panels when this functionality runs.
However if i dont call the unoGameController.playGame(playerFirst.getText(), playerSecond.getText() and directly call the update method or directly write the switching panel code . then it works fine.
When i click on the button from bottom panel, top panel need to be redrawn. But redrawing is not happening. It happens only when resizing the applet.
public class Graphics_Chart(){ public init(){ JScrollPane topPane = new JScrollPane(new ImagePanel2()); topPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); topPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
I want to make a simple game and learn java by experimenting with it.I don't know how to increase a value by clicking it.I know how to make my objects move in my small game with mouse or keys, but I also got a score var that I want to increase per click. I can increase it over time, but after 2 hours of trying I couldn't progress any further.This is the piece of code I used, I imported the class, got getters n setters and all of that...
if(e.getButton() == MouseEvent.BUTTON3) hud.setScore(hud.getScore() + 555); Whenever I click I get an error, if I remember right "NullPointerException" is when something is missing I'm probably not targeting the value correctly... Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.tutorial.main.MouseListens.mouseClicked(MouseListens.java:22) at java.awt.Component.processMouseEvent(Unknown Source)
is any simple way to apply the same code on both ActionListener and MouseListener, so that the same code is executed when the user either presses a JButton or presses Enter when a JTextField is focused.I know that I can define a method, while it is not preferred as I may use break; in my MouseListener.
A simple example:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TwoListener extends JFrame {
[code]....
Although I can define a new method that prints out the three numbers, I have to add certain break; code in different cases. So I just wonder whether there is another way to do things like this.
I have a JList<Object> in my code (it actually takes JLabels) called imageList, and I've given it a custom CellRenderer using the following code:
cellRenderer = new DefaultListCellRenderer() { private static final long serialVersionUID = 1L; @Override public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
[Code] .....
However, when I click anywhere on the JList, nothing gets printed to the console, which suggests that the mousePressed() method isn't being invoked. I think it's a problem with the CellRenderer, because I have similar code elsewhere that works, the only difference being I don't use a custom CellRenderer.
I am trying to Make A GUI which has a Jcombo box i also want it to have a jlabel that up dates depending on which option the user selects from the JcomboBox
for exampl if i select lion i want it to say you chose lion on the Jlabel and if i choose ostrich i want it to say ostrich and so on
I'm having problems getting my program to display a JLabel. The program draws shapes of random size and color across a panel. I have this part working. (There is more code for the drawing of shapes, I left it out for easier reading). When I try to add a JLabel in the main method, it does not display.
import javax.swing.JFrame; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestDraw {
I am trying to move a JLabel in a JPanel, but can't seem to make it work. I can't provide a compiling code, but a few fragments. I have a JLabel [] array. i want to move with the mouse elements of this array. Let's say i want to move JLabel[i]. I implemented this:
Java Code:
static int labelY; static Point labelX; static Boolean flag = false; jLabel[i].addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent evt){ labelY=evt.getY();
[Code]...
I used the bool flag because i can't make changes to the label when it is inside an anonymous class. I also tried using setLocation but that did not work also. I don't get any errors when compiling. But when i run it and try to drag the JLabel[i] i get a long list of errors:
Java Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at puzzle.hw$1$2.mouseDragged(hw.java:276) at java.awt.Component.processMouseMotionEvent(Unknown Source) at javax.swing.JComponent.processMouseMotionEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source)
I read many posts online and watched tutorials on YouTube. I can't seem to get this work. Notice I am not using any drawing/graphics capabilities - I'd like to keep it this way as we have not gotten that far yet in my studies.The image is in a source folder titled Images under/in my project.
Java Code:
private JPanel jpImage = new JPanel(); private JLabel lblImage; private ImageIcon image; private Image img; // All those above defined prior to method // Within method (relevant to code above) ...