I'm creating a top-down parser, that checks the syntax of a given source code according to a given grammar, then displays the result, within a simple GUI.
Basically, the user selects the grammar and source files, clicks "Check Syntax" and the result is displayed like in the picture...
And it initially works fine. However, when selecting different files or updating a selected file, then clicking the "Check Syntax" button again, some graphical issues start to occur after interactions such as scrolling, resizing, or clicking rows:
The issues are more apparent when resizing the window:
Here is the structure, of the frame:
JFrame frame
JPanel headerPanel
...
JPanel bodyPanel
JLabel tableTitle
JScrollPane tableScrollPane
JTable table
JLabel tableSummary
I've tried methods such as revalidate() and repaint() on components where issues happen, but they only fixed those of labels...
I've also tried using a SwingWorker but it didn't solve the issue.
Here is the code portion that might be relevant:
...
var tableScrollPane = new JScrollPane(table);
tableScrollPane.setBorder(new MatteBorder(0,1,0,1, palette.get("strongTeal")));
tableScrollPane.setBackground(palette.get("strongTeal"));
tableScrollPane.revalidate();
tableScrollPane.repaint();
...
var bodyPanel = new JPanel();
bodyPanel.setLayout(new BorderLayout());
bodyPanel.add(tableTitle, BorderLayout.NORTH);
bodyPanel.add(tableScrollPane, BorderLayout.CENTER);
bodyPanel.add(tableSummary, BorderLayout.SOUTH);
bodyPanel.setBorder(new EmptyBorder(45,45,45,45));
bodyPanel.revalidate();
bodyPanel.repaint();
frame.add(bodyPanel, BorderLayout.CENTER);
frame.setVisible(true);
...
Any ideas on how to force the frame or target components to redraw completely (that is to forget about previous state, and only show the newest information when scrolling, resizing, etc...), ultimately overcoming these issues? Thank you.
. However, when selecting different files or updating a selected file, then clicking the "Check Syntax" button again, some graphical issues start to occur
Don't keep creating new components. The old components are still added to the frame. So now you have two sets of components.
Instead, you can replace:
the data in the JTable by using table.setModel( yourTableModel )
the component in the JScrollPane by using scrollPane.setViewportView( yourTable )
Related
I have a button called btnDisplay on a JPanel called TempPanel. When the button is clicked, it should display a JTable that is created manually.
However the table is only visible to me after I resize the panel manually with a mouse. Even if I make the panel smaller than it originally was, it shows the table, otherwise it doesn't.
What is the reason for this? And how can I fix it?
I'm writing the comment as an answer for clarity purpose:
Call revalidate and repaint on the container to which the JTable is been added
I'm using a JDialog to create a customized dialog box for my Java project. I'm having issues with the layout at the moment. It seems each JLabel I add to the dialog goes over the existing one. Do I need to add some sort of JPanel?
I also seem to have a issue with the size. I set it too 500x500 but why does it only goes as large as the text width?
JDialog processData = new JDialog(f1, "TItle goes here");
JLabel centretext = new JLabel("Look at me im centre!");
JLabel leftext = new JLabel("LOok at me im left");
JLabel righttext = new JLabel("LOok at me im right");
processData.setVisible(true);
processData.add(centretext);
processData.add(lefttext);
processData.add(rightext);
processData.toFront();
processData.setSize(500,500);
processData.setLocation(500,500);
processData.pack();
JDialog uses a BorderLayout by default, which means, it will only show a single component in any of the five available positions, all the others get ignored.
Consider using a different layout manager. See Laying Out Components Within a Container for more details
So I have a program that at the start only contains an 'add movie' button at the bottom of the frame.
Above it I inserted a scrollpane.
I also made a seperate JPanel form which contains labels and textfields where you have to input the data of the movie.
Every time I click the 'add'-button I want a form to appear inside the scrollpane (next to previously made forms).
So I figured I just needed to do this:
private void AddMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {
MovieForm movie = new MovieForm();
MovieScrollPane.add(movie);
}
But nothing new appears.
I tried validate() and repaint(), but so far these don't seem to work.
I made the interface in Eclipse btw.
Anyone who can help me?
Thanks anyway!
MovieScrollPane.add(movie);
Don't add components directly to the scrollpane. Normally a JPanel is added the the viewport of the scrollpane.
Then, whenever you add a component to a visible GUI the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
This makes sure the layout manager is invoked to the preferred size can be recalculated.
Also, follow Java naming conventions. Variable names should NOT start with an upper case characters.
I'm using NetBeans, and I've a JFrame where I added a JPanel to it using the NetBeans's palette.
I want to add a JRadioButton manually to that JPanel, so this is the code I tried in the constructor :
ButtonGroup group = new ButtonGroup();
JRadioButton btn1 = new JRadioButton("btn1 ");
JPanel1.add(btn1);
But when I run that JFrame I don't see that JRadioButton anywhere, but it works when I add it using the NetBens's palette.
How can I solve this problem ?
Make sure that the JPanel is not using GroupLayout. Most any other layout would work well, but likely for the moment, JPanel's default FlowLayout will work best.
Be sure to call revalidate() and repaint() on the JPanel after adding a component, if you are adding the component after the GUI has been rendered, such as on a button push.
If still having problems, show your code.
General advice: avoid using code generation utilities until after you understand the underpinnings of the GUI library, here Swing. You won't regret doing this.
The problem with NetBeans GUI Builder is that it initializes everything for you, where you can't alter the code unless you open the file on some other platform. In which case you have the risk of totally messing up the code.
What I can suggest is to maybe attempt something like this
Create an empty JPanel with a preferred size that you set in the property pane. You may also want to set the layout also, depending on your requirements.
After the initComponent() then add the JRadioButtons
public MyGUI(){
initComponents();
ButtonGroup group = new ButtonGroup();
JRadioButton btn1 = new JRadioButton("btn1 ");
jPanel1.add(btn1);
jpanel1.revalidate(); // as #Hovercraft Full Of Eels suggested
jPanel1.repaint();
}
I am using three JButtons in my swing application. When I click on each button, the corresponding data (formatted in JTable with JScrollPane) will display on JPanel.
Problem: when I resize the JFrame, the JPanel is replacing with default button (the button which i was clicked first) information instead of current JButton information.
My sample code:
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actioPerformed(ActionEvent e){
if(e.getActionCommand.equals("button1"))
JPanel.add(table1);
}
if(e.getActionCommand.equals("button2"))
JPanel.add(table1);
}.......
Resizing the JPanel will not suddenly replace components or add other components to the panel.
My best guess (and it is a guess due to the limited information in the question) is that none of your buttons actually work and just show the wrong information.
The code you posted only contains an add without any revalidation of the layout. Consult the javadoc of the Container#add method. When you resize, the layout gets revalidated and you see what is actually contained in the JPanel.
Possible solutions:
Call invalidate and repaint on your panel as well in your ActionListener
Use a CardLayout to switch between the different components
I personally prefer the CardLayout option, but it might depend a bit on the situation.
Note that in the code you posted, you add table1 for both buttons. Might be a copy-paste problem, or a problem with your actual code.
I was unable express problem clearly.Sorry for your inconvenience.
JPanel.removeAll() method has fixed my problem.
I added this method before adding any new component to JPanel. That fixes JPanel unexpected behavior.