Dispose elements in Swing application
I what to ask what is the best way to dispose UI elements in Swing?
I tried different layouts (BorderLayout
, FlowLayout
...) but I could dispose elements quickly and at exactly that point of the screen what I need. I used method setLocation(int x,int y)
but it isn't working.
I mean position. I tried setLayout(null) it helped me,but JTable column headers aren't visible. Here is my code(it is compilable)
public class Test1 extends JFrame{
public JTable tbGoods=new JTable();
public JScrollPane pane;
DefaultTableModel aModel;
public Test1(){
setTitle("Make buy");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
setVisible(true);
DisplayAllGoods(null);
}
private void DisplayAllGoods(List<Object> objectList) {
aModel = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return true;
}
};
//setting the column name
Object[] tableColumnNames = new Object[3];
tableColumnNames[0]="User";
tableColumnNames[1]="Good";
tableColumnNames[2]="Price";
aModel.setColumnIdentifiers(tableColumnNames);
tbGoods.setAutoCreateRowSorter(true);
tbGoods.setBounds(93, 293, 388, 143);
pane=new JScrollPane(tbGoods);
getContentPane().add(tbGoods);
getContentPane().add(pane);
this.tbGoods.setModel(aModel);
}
public static void main(String[] args) {
开发者_运维百科JFrame gf=new Test1();
gf.setLocationRelativeTo(null);
gf.setVisible(true);}}
Well the easiest way would be to just say:
mySwingElement = null
If you want to remove elements from panels, you can try the method:
remove(int index);
also you can use:
mySwingElement.dispose()
It will release all resources related to it
If you want to just make them invisible:
setVisible(false);
If you want to have absolute positioning of swing components you may want to use the null
layout (see here).
please read
1/ JTable tons of examples http://www.java2s.com/Code/Java/Swing-JFC/CatalogSwing-JFC.htm
2/ put your JTable to the JScrollPane
3/ and at the beginning start to place JComponets by using BorderLayout
I solved my problem. I downloaded Jigloo GUI editor and used AbsolutLayout component(to place elements where I want) and into JScrollPane I placed JTable.
public class NewJFrame2 extends javax.swing.JFrame {
private JScrollPane jScrollPane1;
private JTable jTable1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame2 inst = new NewJFrame2();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame2() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(30, 37, 339, 176);
{
TableModel jTable1Model =
new DefaultTableModel(
new String[][] { { "One", "Two" }, { "Three", "Four" } },
new String[] { "Column 1", "Column 2" });
jTable1 = new JTable();
jScrollPane1.setViewportView(jTable1);
jTable1.setModel(jTable1Model);
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
}
精彩评论