I am trying to add a model to a JTable, which was created using IntelliJ Forms. As of right now, the main method has to be static, and if I make the JTable static as well, then IntelliJ says it cannot bind the JTable. I am confused on how I can add the model in this case.
public class DisplaySettings {
private JTable resolutionsTable;
private JPanel displaySettings;
public static void main(String[] args) {
JFrame frame = new JFrame("Display Settings");
frame.setContentPane(new DisplaySettings().displaySettings);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
String[] columns = {"Resolution Size"};
DefaultTableModel model = new DefaultTableModel(columns, 0);
resolutionsTable.setModel(model);
}
}
When you are dealing with IntelliJ Forms, they are automatically handled and allocated for by IntelliJ, by default. If you select the component you are working with in the ComponentTree, in the .form GUI Editor, there is an option called Custom Create. Check that.
Once that is checked, IntelliJ will automatically create a method called createUIComponents(). There you can allocate your JTable and set the model, since this method is not in a static context. This method will be automatically called when creating the UI.
Using the following works for me -
JTable resolutionsTable = new JTable(); // instances of both JTable and JPanel
JPanel displaySettings = new JPanel();
... // you can set the above component with diff attributes
frame.setContentPane(displaySettings);
... // and use them further
resolutionsTable.setModel(model);
Related
I have created a form with IntelliJ's GUI builder, it has a working main() method, the form works properly and has some listeners attached.
In addition to that I have a custom class where I want to call that GUI I created with IntelliJ's GUI builder. I can accomplish this by copying the code within the "main" method in the GUI's class and placing it in my custom class and if I run my custom class the form is indeed displayed.
But thats about all I can do with the created GUI, I can only call it. I can't do other things like dispose that GUI form instance (frame.dispose()) and open another form because I don't know how to get access to the frame instance from my custom class.
Can someone please assist me with this? I thought it would save me a lot of time if I used the GUI builder as opposed to writing the GUI code from scratch for several forms.
I solved the problem by creating a method in the GUI form class called load() which contains the JFrame setup
GUI Form Class
public void load()
{
JFrame frame = new JFrame( "Login Form" );
frame.setContentPane( new LoginForm().mainPanel );
frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
and then in my main class I called it with new LoginForm().load();.
In order to dispose of the initial GUI form and open another one I created a helper method inside the GUI form class called getMainFrame()
private JFrame getMainFrame()
{
return (JFrame) SwingUtilities.getWindowAncestor( this.mainPanel );
}
after that inside the GUI form class constructor there is logic to dispose of the frame when a condition is met
if (age.equals("42"))
{
//load the appropriate form
new MainForm().load();
//dispose of this current form
this.getMainFrame().dispose();
}
First, give a name to your root panel:
Then create a getter for it, and you can use it in a JFrame by
JFrame f = new JFrame();
f.add(new YourGuiClass().getMainPanel());
f.setVisible(true);
When you want to dispose it, dispose the JFrame instance should work.
Edit
You said you want to dispose the JFrame in your GUI form class logic, try this:
class YourGuiClass {
private JFrame f = new JFrame();
private JPanel mainPanel;
public void load() {
f.add(mainPanel);
f.setVisible(true);
}
public void dispose() {
f.dispose();
}
}
By this you can operate the GUI form class without knowing anything related to Swing in the main function:
public static void main(String... args) {
YourGuiClass myGuiClass = new YourGuiClass();
myGuiClass.load(); // it now shows itself
if (someLogic()) myGuiClass.dispose(); // you can
// also call this elsewhere as you like
}
I am trying to refresh my Jtable shown in the UI whenever I query the mysql database. The idea was to show whatever new data updated in the UI JTable.
The UI class is below.
public class DBView {
private JFrame frame = new JFrame();
private JScrollPane tableScrollPane = new JScrollPane();
private DefaultTableModel dbTable = new DefaultTableModel();
public void setDbTable(DefaultTableModel dbTable) {
this.dbTable = dbTable;
//this.dbTable.repaint();
paintDBTable();
}
public DefaultTableModel getDbTable() {
return dbTable;
}
public DBView() {
initializeFrame();
paintDBTable();
}
private void paintDBTable() {
tableScrollPane.setBounds(20, 350, 400, 80);
frame.getContentPane().add(tableScrollPane);
JTable DBTable = new JTable(dbTable);
tableScrollPane.add(DBTable);
DBTable.setFillsViewportHeight(true);
tableScrollPane.setViewportView(DBTable);
}
/**
* Initialize the contents of the frame.
*/
private void initializeFrame() {
frame.setVisible(true);
frame.setBounds(100, 100, 451, 525);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setTitle("MySQL Database");
}
From another Model class I am calling the setDbTable() method. I input a new Jtable object to the setDbTable() method with data read from the database input into the new Jtable object.
The issue is inside the setDbTable() method, I am using paintDBTable() method again.
I tried using dbTable.fireTableDataChanged() method to refresh the view, didnt work.
The way it is now, it is working. But using the setDbTable() method to refresh seems like a very inefficient way to do it.
Question is Do you see anyway I could use another method defined for use of refreshing Jtables?
P.S. I am very new to java and programming in general. Sorry if the code is messy and the question is unclear. I can give all the code if its helpful. I removed most of the methods and other classes in the original code to make the question clearer.
tableScrollPane.add(DBTable);
JScrollPane isn't designated as container, you have to add child to JViewport
there are two options
a) tableScrollPane = new JScrollPane(myTable);
b) tableScrollPane.setViewportView(myTable);
DefaultTableModel dbTable = new DefaultTableModel();
DefaultTableModel is model that hold value for presentations layer for the JTable
rename this local variable (that make the sence) to dbTableModel instead of dbTable
you have to create a JTables view, f.e. two basics options
a) JTable myTable = new JTable(dbTableModel)
b) myTable.setModel(dbTableModel)
dbTable.fireTableDataChanged() is implemented in DefaultTableModel and correctly, not reason to call this method, nor outside of models definition (class, void, interface that returns XxxTableModel)
more informations in linked Oracle tutorials, ... for working code examples in SSCCE / MCVE form too
refresh data for JTable by
removing all rows in dbTableModel.setRowsCount(0);, then add a new row(s) to dbTableModel.addXxx
re_creating dbTableModel, note then must be added back to JTable e.g. myTable.setModel(dbTableModel)
It is not so confusing to refresh the JTable data and refreshing the UI after that, because:
Swing components implemented MVC and Observer in a very fantastic way. That means whenever you change the data in TableModel, the UI will be notified and repainted as you wanted.
So you should change you code in a way that you keep the JTable variable not the TableModel variable in your class. After that in setDbTable call the setModel method of the JTable, it means:
public class DBView {
private JTable jtable = new JTable();
public void setDbTable(DefaultTableModel dbTable) {
this.jtable.setModel(dbTable);
//this.dbTable.repaint();
//paintDBTable();
}
.
.
.
}
Hope this would be helpful,
Good Luck.
New to the forum and to Java. I am trying to have my JList respond when double-clicked, which I have accomplished. The JList is being populated by a SQL query which is ran when a button in the GUI is pressed. Based on the SQL query, the JList is populated, this is also working.
The issue comes about if I try to update the JList by clicking the button to query SQL again. When I click that, the change initially shows up in the JList, however when I click on that option in the JList it immediately switches back to what it was initially. When I double-click on what appears to be the incorrect name, the value that I have printing in the console reports correctly. So it has the value correct in the console but the rendering in the JList is not correct.
I appreciate any responses, I have combed the forums without any luck. I am new to Java so I'm sure there is quite a bit that isn't perfect with my code. Code is below please let me know if you need more. Thank you.
public JPanel results(StringBuilder message)
{
StringBuilder[] options = {message};
showOption = new JList(options);
showOption.setLocation(300, 50);
showOption.setSize(140,100);
showOption.setVisibleRowCount(10);
textPanel.add(showOption);
showOption.revalidate();
showOption.repaint();
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//JList showOption = (JList) mouseEvent.getSource();
if (e.getClickCount() == 2) {
int index = showOption.locationToIndex(e.getPoint());
Object o = showOption.getModel().getElementAt(index);
System.out.println("Double-clicked on: " + o.toString());
}
}
};
showOption.addMouseListener(mouseListener);
return totalGUI;
}
public static void main ( String args[] )
{
//JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] JTextField of Dreams [=]");
GUI_TextField demo = new GUI_TextField();
frame.setContentPane(demo.createContentPane());
//frame.setContentPane(demo.results(message));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(510, 400);
frame.setVisible(true);
}
Three things jump out at me immediately.
You're creating a new JList each time
You're manually setting the size and position of the JList
You're not removing the previous JList
For example...
public JPanel results(StringBuilder message)
{
StringBuilder[] options = {message};
// Create new JList
showOption = new JList(options);
// This is ill advised
showOption.setLocation(300, 50);
showOption.setSize(140,100);
showOption.setVisibleRowCount(10);
// What about the last JList?
textPanel.add(showOption);
This raises a number of possibilities, the likely one is that you are covering over the previous list, which is being brought to the front when textPanel is validated and painted.
Swing follows (loosly) the MVC paradigm (and for more details)
So instead of re-creating the view each time, you should simply re-create the model, for example...
public JPanel results(StringBuilder message)
{
DefaultListModel model = new DefaultListModel();
model.addElement(message);
showOption.setModel(model);
If showOption isn't created initially before this method is called, you should consider putting in a if statement to detect when showOption is null and initialise it appropriately.
You should also avoid using setLocation and setSize. Swing has being designed to operate with the use of layout managers, these make it possible to define workflow and general layout that can be used across multiple platforms.
Take a look at How to use lists and Laying Out Components Within a Container
Im using a JPanel with propertyChangeListener and want it to rerender itself based on whenever a particular variable model changes. My code for the same is as follows --
public class LabelMacroEditor extends JPanel implements PropertyChangeListener {
private static final long serialVersionUID = 1L;
private LabelMacroModel model;
public LabelMacroEditor(LabelMacroModel bean) {
this.model = bean;
model.addPropertyChangeListener(this);
setupComponents();
validate();
setVisible(true);
}
public void setupComponents()
{
Box allButtons = Box.createVerticalBox();
JScrollPane macroModelScroller = new JScrollPane(allButtons);
macroModelScroller.setPreferredSize(new Dimension(300, 200));
for(MacroModel macroModel : model.getMacroModelList())
{
LabelMacroEditorEditableEntity macroEditorEntity = new LabelMacroEditorEditableEntity(macroModel);
Box entityBox = Box.createHorizontalBox();
entityBox.add(macroEditorEntity.getUpButton());
entityBox.add(Box.createHorizontalStrut(15));
entityBox.add(macroEditorEntity.getMacroDetailsButton());
entityBox.add(Box.createHorizontalStrut(15));
entityBox.add(macroEditorEntity.getDownButton());
allButtons.add(entityBox);
}
add(macroModelScroller);
}
#Override
public void propertyChange(PropertyChangeEvent arg0) {
revalidate();
repaint();
}
}
When i use the debug mode in eclipse i can see that whenever there is a change to model it triggers off the call propertyChange and it also runs over revalidate and repaint but only the JPanel display remains the same. It does not seem to be rerendering itself.
Anything fundamental that I'm missing here ?
EDIT :
An example snippet of a property im changing is as follows --
labelMacroModel.addMacroModel(addedMacroModel);
where labelMacroModel is of the type LabelMacroModel and addedMacroModel is of the type Macro
Now the relevant part of LabelMacroModel class that fires off the property change is as follows --
private List<MacroModel> macroModelList;// this is the list of all MacroModels
public void addMacroModel(MacroModel macroModel) {
macroModelList.add(macroModel);
pcs.fireIndexedPropertyChange("LabelMacroModel", macroModelList.size(), null, macroModel);
}
Its not clear how you are changing the components in the panel. If panel is not updated then repaint/revalidate will have no effect. I think you should not need revalidate/repaint to be called explicitly if you are not modifying the way components are laid out. JButton.setText should for example change the label of the button without need of calling repaint.
To expand on the answer by AKJ above, I think you should be reconstructing your components on property change. So doing a remove all then readding is one way to do this. Once you get this working you could be more selective about pushing the model update into the GUI eg if a new entry has been added then just add a new component to reflect this. The remove all / readd is fine for a lot of cases though. HTH.
Using the example TableFilterDemo, I'm trying to create a RowFilter in NetBeans, but I can't. I tried the code in JCreator; I need help.
I understand that I do not need to create class MyTableModel extends AbstractTableModel because I already manually did this in NetBeans GUI tools. Now, I face a problem in defining the model with RowSorter.
MyTableModel model = new MyTableModel();
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
Above is the example, and I change it to this:
private TableRowSorter<javax.swing.table.DefaultTableModel> sorter;
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
javax.swing.table.DefaultTableModel model = new DefaultTableModel();
sorter = new TableRowSorter<javax.swing.table.DefaultTableModel>(model);
JTable jTable = new JTable(model);
jTable.setRowSorter(sorter);
Is it correct? I can't get this to work. I suspect is the sorter is not added into the table. Which maybe because of defining model part. Please give advice.
I think it is not nessesary to you create your abstract table model .
you just use defaultModel and use following code
voterTable.getColumnModel().getColumn(0).setPreferredWidth(65);
I supposed you create the JTable in the GUI Builder. You have too add the model to this table and not to a new one you create.
Replace the method with :
public NewJFrame() {
javax.swing.table.DefaultTableModel model = new DefaultTableModel();
sorter = new TableRowSorter<javax.swing.table.DefaultTableModel>(model);
initComponents();
}
And add the rowsorter from the GUI Builder (will be added in initComponent()):
Right click on the JTable -> Properties -> Pane "Code"
In Custom Creation code write -> new JTable(model);
In Variable name you will see the variable name ([table_name] in following point)
In Post Creation Code write -> [table_name].setRowSorter(sorter);