I am using a JTable and after it is filled with data, I have a search button at the bottom with a text input. Once I type in "Joe" it brings up all of joes details. However, these are displayed in one row going left to right (much like a spreadsheet row).
I was wondering if there was a way I can make it display this data in a different way? More like a table rather than going across in one individual row...
Please tell me if I need to show parts of my code as it is quite a bit and I'm unsure if I should paste it all in or not. (New to this site).
Thanks.
Code:
private void makeframe()
{
frame = new JFrame("Search");
frame.setPreferredSize(new Dimension(300, 300));
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
}
DefaultTableModel model = new DefaultTableModel();
model.addColumn("User");
model.addColumn("first name");
model.addColumn("surname");
//this goes on for a while
model.addRow(data);
JTable mainTable = new JTable(model);
contentPane.add(new JScrollPane (mainTable));
frame.setPreferredSize(new Dimension(1300, 200));
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
You can try to create your panel in format you prefer, then use table with only 1 column. Place your pannel inside this column.
Related
I'm having trouble displaying a scrollable JTable in my contentPane, the table should be underneath the label, but nothing shows up. However, when I remove the scrollPane, the table is displayed.
public void populateJTable(int resultCount, String[] closeNames, String[] closeCities) {
String[] columnTitles = {"Brewery", "City"};
String[][] data = new String[resultCount][columnTitles.length];
for(int i = 0; i < closeNames.length; i++) {
String tempBrewery = closeNames[i];
String tempCity = closeCities[i];
data[i][0] = tempBrewery;
data[i][1] = tempCity;
}
table = new JTable(data, columnTitles);
table.setBounds(20, 95, 580, 250);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
contentPane.add(scrollPane);
}
However, when I remove the scrollPane, the table is displayed.
table.setBounds(20, 95, 580, 250);
Based on the above code it looks like you are using a null layout since you see the table.
However, when you add the table to the scrollpane, the scrollpane doesn't have a size so there is nothing to paint.
Don't use a null layout!!!
Swing was designed to be used with layout managers. Learn how to use them and the code will be easier to code and work better.
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
contentPane.add(scrollPane);
So now when you start using layout managers and you add a component to a visible GUI you need to revalidate() and repaint()` the panel that changed to invoke the layout manager on the panel. So you need to add:
contentPane.revalidate();
contentPane.repaint();
However, a better solution is to NOT keep creating a new JTable and JScrollPane. Instead you can create those components when you create the frame and add them to the frame.
Then when you do the search you just create a new TableModel with the search results and then you update the table using:
table.setModel( .... );
The table will repaint itself automatically.
I've spent hours online and I'm afraid I can't quite figure out how to make my JTable show up next to my JButton on a JFrame, if anyone has a simple or comprehensive way to explain why and how to fix the problem Id really appreciate it.
Extensive online research including downloading samples and applying various suggestions; also reached out to my teacher for help but she doesn't have much experience with java.
public class canvas extends JTable{
static void displayJFrame(){
//set variables
int i = 0;
String[][] strArray = new String[3][i];
String[] labelArray = new String[3];
labelArray[0] = "Name: ";
labelArray[1] = "Subject: ";
labelArray[2] = "Average: ";
//create JFrame
JFrame f = new JFrame("Test Average");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setBackground(Color.WHITE);
f.setPreferredSize(new Dimension(600, 600));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
//create JButton
JButton button=new JButton("Enter New Grade");
button.setBounds(450,15,140, 40);
button.addActionListener(e -> average(strArray, i));//gets info from user and adds to strArray
button.addActionListener(e -> count(i));//increases counter
f.add(button);
//create JTable
JTable j = new JTable(strArray, labelArray);
JScrollPane scrollPane = new JScrollPane(j);
j.setBounds(30, 40, 200, 300);
j.setSize(500, 200);
j.setVisible(true);
}
}
all of my code runs as expected except there is no table, I've also tried so many things that didn't work so this basically where I started so that its not crowded by tons of incorrect stuff
You have several problems. First, by default a JFrame uses a BorderLayout. Using the default add(component) method places that component in the center. Using the same add() method again just replaces the new component at the center.
Second, do not pack or set the frame visible until AFTER you have created the entire GUI.
You would do better to create a JPanel and add the components to that panel, then add the panel to the frame. The default layout manager for JPanel is a FlowLayout.
Somehow I don't the scrollpane to show up. What do I need to change?
bigP = new JLabel();
setLayout(new BorderLayout());
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
helper.setPreferredSize(new Dimension(500,600));
helper.add(new JScrollPane(bigP, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
picPane = new JPanel(new BorderLayout());
picPane.add(helper,BorderLayout.CENTER);
picPane.setMaximumSize(new Dimension(500, 600));
picPane.setVisible(true);
add(picPane, BorderLayout.CENTER);
After an image is chosen this line is called:
bigP.setIcon(img);
I figured out that I most certainly will need the helper-panel as the BorderLayout would only take one component (as far as I understood).
Unfortunately my scrollpane won't show up at all though the picture does.
helper.setPreferredSize(new Dimension(500,600));
Don't hardcode a preferred size. The panel will determine its own preferred size based on the components added to the panel.
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
sc = new JScrollPane(bigP,JScrollPane
Also a component can only have a single parent. In the above code you attempt to add "bigP" to "helper". But then in the next statement you add it to the scrollpane, so "bigP" is removed from the "helper" panel and will only appear in the scrollpane.
//pic.add(bigP,BorderLayout.CENTER);
pic.add(helper,BorderLayout.CENTER);
Also you never add the scroll pane to the "pic" panel. The code should be:
//pic.add(bigP,BorderLayout.CENTER);
//pic.add(helper,BorderLayout.CENTER);
pic.add(sc, BorderLayout.CENTER);
So now you should have a structure that looks like:
- pic
- sc
- bigP
It would also help if you use more descriptive names so everybody knows what those variable are.
I mean the following:
JFrame frame = new JFrame("Hello swing");
JPanel panel = new JPanel();
panel.add(new JButton("A"));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
But this code displays this
Is there a way to display something like this right after starting up?
I mean so that the whole title can be seen.
Use the length of the title and apply it to the new dimension used. However I haven't tested it, you have to play with the numbers yourself. Just giving you the idea.
JFrame frame = new JFrame("Hello swing");
JPanel panel = new JPanel();
int len = frame.getTitle().length();
Dimension dimension = new Dimension(200 + 7*len, 200);
panel.setPreferredSize(dimension);
I guess using the number 7 as the constant would be the best. However be aware of using wide (wwww..) or narrow letters (iiii...) mostly, because they appear in different widths. To get the exact length, you have to use method of such as Graphics.getFontMetrics and FontMetrics.stringWidth are.
Take a look at the example acording my code on the top:
Use:
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
to expand your frame
I have a really weird problem with a JScrollPane and a BorderLayout. For short explaination: i have a JTable which is inside the JScrollPane and this is with a JPanel and the JTableHeader on a JTabbedPane. Very Simple Layout. If i add just the JTable to my JPanel, the buttons are working. If i add the JScrollPane, the Buttons are not working anymore, so i cant click them! The ActionLister is never reached and i cant see the click-animation.
Some Sample code to explain:
d_pane = new JPanel();
d_button = new JPanel();
d_pane.add(table.getTableHeader(), BorderLayout.PAGE_START);
dl_scroll = new JScrollPane(table);
d_pane.add(dl_scroll, BorderLayout.CENTER);
// d_button is ridLayouted with 3 Buttons in there
d_pane.add(d_button, BorderLayout.PAGE_END);
1) The JScrollPane takes care of the table header itself. Don't add it to the pane.
2) the button does not seem to get the mouse events, probably because another component is above it - do you have other components/code in the setup?