• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

java swing noob

eon

2[H]4U
Joined
Oct 11, 2003
Messages
2,218
I'm trying to create a simple GUI where a list of data objects will be displayed. Then there will be various buttons to modify a selected object from this list or change to a different screen.
So far I planned to have a global Jframe window while components are added and removed on screen changes.

So far I cant even display have one button where if clicked, the JFrame clears all components from the window. When the button is clicked, it just freezes. Also its very buggy as I run it in Eclipse. Sometimes the button is sized properly. Other times the button is the size of the entire JFrame.

Code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.URL;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

//import UI.AppWindow;

//import statements
//Check if window closes automatically. Otherwise add suitable code
public class gu extends JFrame {

	JFrame window = new JFrame("Test");
	JButton b;
	
	public static void main(String args[]) {
		gu testrun = new gu();
		testrun.more();
	}

	public void more()
	{
		
		window.setSize(800, 600);
		window.show();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		b = new JButton("CLICK THIS");
		b.setSize(100,70);
		b.setOpaque(true);
		window.setContentPane(b);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearframe();
			}
		});
		
	
	}
	
	public void clearframe()
	{
		window.removeAll();
		window.validate();
		window.repaint();	
	}
}
 
first, why extend JFrame and then create a JFrame member? do one or the other, it doesn't necessarily matter which.

i believe your main problem is from your trying to setContentPane(b). don't do that. instead, window.add(b). you would use setContentPane if you, for example, have two JPanels that have different components on each and you want to switch from one to the other. a JPanel is a container and holds components, like buttons. setContentPane expects a container but you provide a button.

you're going to need to become familiar with layout managers and how they work but try reading through the swing tutorial first and ask specific questions. i don't want to type up a broad overview that's already available.
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
 
thanks, adding a flowlayout helped fix that problem

Do know of the best way to display a list of data with different attribute columns along with the ability to select a data object in the list?
such as a list of students that has a column for name, id, and grade.
 
Ok I think JTable is what im looking for, reading into it now. Sorry for the weak questions but im pressed for time so Im trying to wing it without spending too much time reading documentation.
 
Jtable is decent, its a little complicated though. You will want to start
with using a DefaultTableModel and pass in a String[][] with your data.

java swing tutorial - How to use tables. Although you can probably ignore most of that page.

If you are trying to setup a window that has the table take up most of it, and buttons along the bottom, try doing something like this.

Create a panel, have it use BorderLayout.
Add the table as BorderLayout.CENTER to that panel
create another panel, you can probably use flowlayout, but i like to use BoxLayout
add the buttons to that panel
then add that panel to the first panel, with constraint BorderLayout.SOUTH

The swing tutorial should have information on how to do all of this.

For your buttons I prefer to add Action's to them, but you could use an action listener instead.

Now that we've shoved you into the deepend of the pool, hope you can swim :)

ps. The java naming convention is to start the name of classes with a capital letter, to differentiate them from variables you use which start with a lower case letter. "Gu" instead of 'gu'

edit:
Bonus tip - pack() your frame to fit it to the size of your components.
Bonus tip2 - Put your table in a JScrollPane if it is too big.
 
i modified some demo code and just added everything to flowlayout: first my buttons and check boxes then the table which was automatically put on the next line so now i have a list of buttons on top and a table below.
Now im trying to figure out:
How to disallow users from clicking on the table and manually editing it. The only way to edit the table should be by selecting a data row, and the press a control button on top which modifies that row based on which check box on top was checked.
 
Ok i think I got found out how to disable table editing. Now Im trying to figure out how to extract the row # from a user's cell row selection.
Again I apologize for asking stuff before exhaustively trying to figure it out myself, I rarely do this but Im feeling the pressure of time big time.
 
You'll want to do something like setSelectionMode to ListSelectionModel.SINGLE_SELECTION
then you can use
int selectedRow = table.getSelectedRow();

since you have set it so only one row can be selected.

for the full information, read the jtable tutorial thats been linked to.
 
thanks for that, i was just about to look for single selection mode

Any idea how to allow rows to expand vertically if the data in a cell is too much to fit in the default cell size?
 
Back
Top