Swing/AWT/SWT :: Adding ActionListeners To JButtons
Mar 11, 2009
I have an application that reads through a ResultSet... while I'm reading that ResultSet, I am creating new JButtons..... Now I need to add an ActionListener to those JButtons...... but they are not visible to the actionPerformed method. So I thought I'd try to add an ActionListener as I created the buttons with the following code:
final File file = new File(rs.getString(3));
SKPictureButton spb = new SKPictureButton(url,file.getName());
spb.addActionListener (
new ActionListener() {
public void actionPerformed( ActionEvent e )
[Code] ....
However this seems a bit excessive to me..... I don't think I really want to be creating a new ActionListener class for each JButton do I ? Is there a better way to make the JButtons created in this method visible to the actionPerformed method ? I'm just gettin' back into the "Swing" of things in Java.... haven't coded it for a while.
1 class which creates an instance of my GUI class class and initializes it which works fine.
1 class with all GUI stuff and another class which I'm trying to add all my actionsListeners to (which I can't seem to figure out).
Here is my GUI class: public class GUI extends JFrame{ Container container; CardLayout cl; public GUI(){ super("Game Finder");
[Code] .....
Using my new class "AllActionListeners" I want it to have the bulk of the actionListener code, in order to make the program more manageable. However, after looking around I don't seem to be any closer to a solution. How to move the action listeners to a different class?
I am in the middle of creating a chess application, and am currently working on laying out a list of moves on the right of the actual board. Specifically, I want to have a JLabel that says "moves" on top of a JList that contains the moves, on top of two buttons, labeled "<" and ">" respectively, to which I will eventually add functionality to take back and un-take back moves. Currently I have the first two parts down, but when I add my buttons,the other components are getting messed up.
I have all of these things in a vertical BoxLayout inside of a JPanel with a fixed width of 70, and a height that varies according to the size of the window (I overrode the getPreferredSize method of the JPanel). The two buttons are within their own JPanel with a grid layout with one row and two columns. Then I basically just add the moves list label, the movesList, and the panel housing the buttons to the larger panel, in that order.
However, I am encountering two problems. One, the buttons display the text "... " instead of "<" and ">" as if there is not enough room, although I'm pretty sure three periods take up more space than one less than or more than sign. The next is that, without the buttons at the bottom, the label reading "moves" is appropriately centered (or left aligned, I can't tell which) over the movesList, and you can read its whole text. However, when I add in the buttons at the bottom, the label of the top shifts over to the right and then reads "mov..." because it has run out of room.
Here is my code:
//move list movesListLabel = new JLabel("Moves"); //the components for the actual movesList come from another class with an object called pen, but I don't think this is the problem listModel = pen.getListModel(); JList movesList = pen.getMovesList(); JScrollPane listScroller = pen.getMovesListScrollPane();
Am trying to dynamically insert buttons (which will be presenting card in a frame) using grid layout.As shown image is getting inserted but its not fit in button.I tried Darryl's Stretch icon as well but of no support.
I'm having issues with JButtons and painting methods, whenever I click a grid on the board, the buttons in the JPanel disappear. Here is the board class:
package GUI; public class Board extends JPanel implements Observer { /** * */ // private static final long serialVersionUID = 1L; private final int boardSizeX; private final int boardSizeY; private final int L = 20;
[Code] ....
I know the code is messy in places, this will be fixed later, just need some pointers on how to solve the JButton issue.
I'm trying to learn how to use ActionListeners. I want a button to hold a int and everytime it's pressed, the int gets + 2. Basically the button is a product and everytime it's pressed, you buy it, the int is your total, at least for that button. I want to have several buttons, each a different price, so maybe button 2 is +3 everytime. At the end then I want to add all the ints together for a total cost and then write that to a file. The only thing is I'm having trouble.
Here's my code
import javax.swing.*; import java.awt.*; public class Shop { public static void main(String [] args) { Frame frame = new Frame(); Button button = new Button(frame.getPanel());
[code]...
The problem is in my Button class, I think it's because my actionPerformed is set to void, but when I change it to int, it says my class must be abstract. For the moment I only have the info going to a label, this is just for testing. Whenever I run it, it says Juice is set to 0, probably because like I said earlier, it's set to void.
I am new to swing and I wanted to know how to be able to make a second row of buttons. Right now I have a text pane at the top and at the bottom one row of buttons. But I need to add another row or two of buttons. I've only been coding in Java.
public class TextPane { public static void main(String[] args) throws IOException { ///////////// //I/O FILES// ///////////// //Create the input file FileWriter inputFile = new FileWriter("userInputStory.txt");//story user creates with the <nouns>, <verbs>, etc PrintWriter inputFileWriter = new PrintWriter(inputFile);
I am currently creating a Twitter Application within Java Swing using JSON. I have the JList populating with Home feeds but i also want to get Images displaying next to the Text ...
currently all i am getting is [URL] ....
So a am able to get the URL Link but getting that to a Image seems to be the problem
/ArrayLists final ArrayList<String> TweetArray = new ArrayList<String>(); //Array List for Users Tweets.. final static ArrayList<String> incomingTweets = new ArrayList<String>(); //ArrayList for Incoming Tweets. ArrayList<String> arrayImages = new ArrayList<String>();//ArrayList for incoming Tweet Profile Images private Map<String, ImageIcon> imageMap;
So I'm doing a basic MVC layout for a pretty basic game that I am making in order to understand the whole MVC layout. The game requires the user to move up/down/left/right via JButtons on the GUI. Since I'm using an MVC layout and my buttons are in a different class than the ActionListeners, I was wondering what the best way to add the action listeners are?
I'm starting to use AbstractTableModel to customize a JTable model. The problem appears when you add a record from a ResultSet to JTable object.
I've managed to insert records, but empty, I need that for every column I can see the data is there.
Leave some code I have been writing ... the method to insert records in JTable is called "insertEmptyRow" (for now).
I need some opinion that can make changes to the code of this method so you can get to see all the data in a record.
MyFirstModel:
public class TablaModeloPrincipal extends private String algo; private int numRegTabla; //Agrego las columnas que quier tener en el model private String[] columnNames = {"DNI", "Nombre", "Edad", "Dirección", "Empresa", "Contacto"}; Vector<String[]> clientes = new Vector<String[]>() ; public ResultSet buscarResultset() throws SQLException{ ResultSet rs=null;
The program loads, but nothing shows. I did some research and saw that I needed to set the JFrame to visible at the bottom of the frame section. I made the change, but it still does not show anything. Do I need to add everything that I added to the content pane to the frame? From my understanding you add elements to the panel, the panel to the contentPane, then the pane to the frame...
implementing a swings application. I have a JFrame containing 2 JPanels : Top & Bottom panel. Top panel has a Jcombobox, Botton panel contain a Graph plot. On changing value in combo box, graph plot should get updated. I thought of adding actionlistener to Jcombobox. Not sure what to implement in its actionPerformed()
Can I add actionListener to a button component without creating a reference to it? Look at my code below:
public void init() { setBackground(Color.red); //Create the layout setLayout(new BorderLayout(20, 5)); //Add buttons add("North", new Button("Red")); add("South", new Button("Yellow")); add("East", new Button("Cyan")); add("West", new Button("Magenta")); add("Center", new Button("White")); }
Or should I just do the usual instance.addActionListener(this) like myButton.addActionListener(this)?
I'm writing a Java Swing program for my software development class to allow a user to create a map for a side scroller video game. I'm stuck on a particular part of my GUI where I'm trying to create a properties box for specific tiles. When I draw the components onto the JPanel and display it as it is, it shows two check boxes per row. On the ScrollPane however, all the check boxes go straight onto one line and draw out the scroll pane. Right now I am only concerned with just making the basic GUI.
Here is what I have so far:
Within the main class, I am creating the JFrame and adding components to it. I am attempting to create a JScrollPane which will hold the properties check boxes...
Here is the method where I am implementing the code. It works, but it doesn't display correctly.
private void createPropertiesBox() { PropertiesBoxPanel pbp = new PropertiesBoxPanel(); propertiesBox = new JScrollPane(pbp); propertiesBox.setViewportView(pbp); frame.add(propertiesBox);
[Code] .....
I've been tinkering with this code for quite some time now and I cannot seem to make a breakthrough on how to fix this.
I'm trying to fill my jtable with an arraylist. The problem is the jtable is in an extended class and the arraylist in the mainGUI. Now how can I fill the jtable with the arraylist?
That's the arraylist in my MainGUI
BufferedReader in = null; ArrayList<String> data = new ArrayList<String>(); try { in = new BufferedReader(new FileReader("1.dat.txt")); String str; while ((str = in.readLine()) != null) { data.add(str);
I like to adding a scrollbar to a jpanel with flowlayout but is imposible, i don't see the scrollbar. I've tried a thousand different ways but I have not accomplished anything.
Here is my code:
//Creamos el panel que contendra los botones de cada producto diferente package com.foolsrecords.tpv.tablaproductos.vista; //Hacemos todas las importaciones necesarias import com.foolsrecords.tpv.modelo.Producto; import com.foolsrecords.tpv.modelo.eventos.ControladorEventListener;
I made a game but i didn't add imports, i have been told i need to have imports and it needs to be in a normal swing format or it will not pass . How to change my code to swing format?
My code is
public class TextTwist extends javax.swing.JFrame implements java.awt.event.ActionListener { // hard code, should be picked from a Problem class private String[] letters = {"F","O","C","I","E","F"}; // hard code, should be picked from a Problem class private String[] solutions = {"ICE","OFF","FOE","FOCI","OFFICE"};
[Code] ....
How do i get the program to move on to the next one String?
I want to add a button who refresh the tree the problem that i have not "DefaultTreeModel" in my class to do this ((DefaultTreeModel) jTree1.getModel()).reload(); and i try this jtree.updateUI(); but not work ....
I need to take the value of what's typed in my JTextField, and once the submit button is clicked, I want to add that string to my JComboBox list items.
Depending which RadioButton they selected, I need to put the team in HOME or AWAY teams, and so on and so on, until they continue clicking submit.
I need to change the value only when I press one of the arrows in a JSpinner component.How can I add a listener to the arrow buttons. I tried ChangeListener but it was not what I needed
I am trying to create a GUI interface in swing which consists of four classes: a GUI class, which creates a main JPanel, a label, and a JList, which it takes from the second class, a MovesList class that contains a JList and the stuff needed to interface with it. It also has a main class, which basically just creates an object of the GUI class for the main window, and an Other class from which I would like to be able to add an item to the JList. I created methods in the MovesList class to get each component (like getMoveslist, or getMovesListScrollPane), which I then used to create the JList in the GUI class. I also created an addMove method so that I can add an item to the JList from any class through a MovesList object. However, this addMove method only works when called from the GUI or MovesList classes -- it does nothing when I collect from the Other class.
Here is my code:
//Main class public class TestProject { public static void main(String[] args) { GUI mainWindow = new GUI(); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
[Code] ....
When I run this code, I get a window with a JLabel that says "Moves," and a JList that contains five elements -- test 1-test 5, but not a sixth "test from other class." ( using the add move method ) However, when I click on the window, the addMove method is also called, and successfully adds "test 6" to the list.