JavaSwing (FileReader) read txt.file and write into JList - randomly works Oo
Hi I'm working on a JavaSwing application but there's a problem with... I don't know exactly but I think it's maybe a (re)paint-problem :S - anyway here's my code:
MAIN:
public class QickSort {
protected static ArrayList<String> input;
private static File file = new File("C:/Users/save.txt");
public static void main(String[] args) throws Exception {
BufferedReader reader;
String line = null;
try {
input = new ArrayList<String>();
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
input.add(line);
}
reader.close();
} catch (Exception ex) {
System.out.println("Can't load file on path.. - " + ex);
ex.printStackTrace();
}
System.out.println(input.size());
JFrame.setDefaultLookAndFeelDecorated(false);
MasterWin win = new MasterWin(input);
}
}
UI:
public class MasterWin {
private JFrame frame;
private JTextField txtFieldPath;
private JButton btnBrowse;
private JButton btnAddPathTo;
private JLabel lblChosenFolderpaths;
private JButton btnRemove;
private JButton btnNext;
protected static ImageIcon icon = new ImageIcon(MasterWin.class.getResource("/View/logo_sml.gif"));
private JScrollPane scrollPane;
private JList linkList;
private List<String> test;
/**
* Create the application.
*/
public MasterWin(ArrayList<String> fileInput) {
test = fileInput;
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setIconImage(icon.getImage());
frame.setTitle("QickSort - Start");
frame.setResizable(false);
frame.setBounds(100, 100, 645, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{480, 127, 0};
gridBagLayout.rowHeights = new int[]{135, 60, 0, 0, 0, 115, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
开发者_如何学运维 frame.setVisible(true);
JLabel logo = new JLabel("");
logo.setIcon(new ImageIcon(MasterWin.class.getResource("/View/Logo.gif")));
GridBagConstraints gbc_logo = new GridBagConstraints();
gbc_logo.fill = GridBagConstraints.HORIZONTAL;
gbc_logo.gridwidth = 2;
gbc_logo.insets = new Insets(0, 0, 5, 0);
gbc_logo.anchor = GridBagConstraints.SOUTH;
gbc_logo.gridx = 0;
gbc_logo.gridy = 0;
frame.getContentPane().add(logo, gbc_logo);
JLabel lblChosePathYou = new JLabel("Choose paths you want to use:");
GridBagConstraints gbc_lblChosePathYou = new GridBagConstraints();
gbc_lblChosePathYou.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblChosePathYou.insets = new Insets(0, 60, 5, 5);
gbc_lblChosePathYou.gridx = 0;
gbc_lblChosePathYou.gridy = 1;
frame.getContentPane().add(lblChosePathYou, gbc_lblChosePathYou);
txtFieldPath = new JTextField();
txtFieldPath.setEditable(false);
GridBagConstraints gbc_txtFieldPath = new GridBagConstraints();
gbc_txtFieldPath.fill = GridBagConstraints.HORIZONTAL;
gbc_txtFieldPath.insets = new Insets(0, 60, 5, 5);
gbc_txtFieldPath.gridx = 0;
gbc_txtFieldPath.gridy = 2;
frame.getContentPane().add(txtFieldPath, gbc_txtFieldPath);
txtFieldPath.setColumns(10);
btnBrowse = new JButton("Browse...");
btnBrowse.setMinimumSize(new Dimension(89, 25));
btnBrowse.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
gbc_btnBrowse.anchor = GridBagConstraints.WEST;
gbc_btnBrowse.insets = new Insets(0, 10, 5, 0);
gbc_btnBrowse.gridx = 1;
gbc_btnBrowse.gridy = 2;
frame.getContentPane().add(btnBrowse, gbc_btnBrowse);
lblChosenFolderpaths = new JLabel("Chosen folderpaths:");
GridBagConstraints gbc_lblChosenFolderpaths = new GridBagConstraints();
gbc_lblChosenFolderpaths.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblChosenFolderpaths.insets = new Insets(0, 60, 5, 5);
gbc_lblChosenFolderpaths.gridx = 0;
gbc_lblChosenFolderpaths.gridy = 3;
frame.getContentPane().add(lblChosenFolderpaths, gbc_lblChosenFolderpaths);
btnAddPathTo = new JButton("Add to list");
GridBagConstraints gbc_btnAddPathTo = new GridBagConstraints();
gbc_btnAddPathTo.anchor = GridBagConstraints.WEST;
gbc_btnAddPathTo.insets = new Insets(5, 10, 5, 0);
gbc_btnAddPathTo.gridx = 1;
gbc_btnAddPathTo.gridy = 3;
frame.getContentPane().add(btnAddPathTo, gbc_btnAddPathTo);
btnRemove = new JButton("Remove");
btnRemove.setToolTipText("Delete selected path");
btnRemove.setPreferredSize(new Dimension(89, 25));
btnRemove.setMinimumSize(new Dimension(89, 25));
btnRemove.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnRemove = new GridBagConstraints();
gbc_btnRemove.anchor = GridBagConstraints.NORTHWEST;
gbc_btnRemove.insets = new Insets(5, 10, 5, 0);
gbc_btnRemove.gridx = 1;
gbc_btnRemove.gridy = 4;
frame.getContentPane().add(btnRemove, gbc_btnRemove);
btnNext = new JButton("Accept");
btnNext.setPreferredSize(new Dimension(89, 25));
btnNext.setMinimumSize(new Dimension(89, 25));
btnNext.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnNext = new GridBagConstraints();
gbc_btnNext.anchor = GridBagConstraints.SOUTHWEST;
gbc_btnNext.insets = new Insets(0, 10, 25, 0);
gbc_btnNext.gridx = 1;
gbc_btnNext.gridy = 5;
frame.getContentPane().add(btnNext, gbc_btnNext);
scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 60, 25, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 4;
frame.getContentPane().add(scrollPane, gbc_scrollPane);
//JList I copy the array
linkList = new JList(test.toArray());
scrollPane.setViewportView(linkList);
}
}
The Problem: It's really strange! Sometimes the text is shown on my JList - but if I start the program once again there's just a empty ScrollPane without the JList or the inputs. Its more or less random that the text appears.
I tried a various kinds of Array(List)s, to impl. - with AbstractModel() oder just toArray(). Always same result..
Does someone know this problem?
Change:
protected static ArrayList<String> input;
To:
protected static final ArrayList<String> input = new ArrayList<String>();
Remove the assignment to input
in main.
Wrap the calls to swing code in an invokeLater
:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(false);
MasterWin win = new MasterWin(input);
};
});
After setting your list as the viewport view you should call invalidate()
on the JList
which will tell the underlying Swing graphics system that it needs to check the component as well as any possibly affected components whether or not these require updating.
Do not call repaint()
because that will trigger a repaint of the component as it is, it will not ensure that other affected components are properly updated as well -- and because this JList
is wrapped in a JScrollPane
it will not behave as expected.
user268396's answer about layout validation is correct, but you should never need to be in this situation in the first place. Your layout is invalidated because you're adding components after you've made your JFrame
visible. Simply by moving the call to frame.setVisible(true)
to the end of the initialize()
method, you should see the problem goes away, because the layout is now finalized and it will validate and render correctly first time.
I solved this issue:
I kicked out my Scrollpane
and used only a JList
. But that didn't work either.
I call now updateUI()
after I create the JList - now it works.
精彩评论